Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.jfrog.bamboo.configuration;

import com.atlassian.bamboo.collections.ActionParametersMap;
import com.atlassian.bamboo.task.TaskDefinition;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jfrog.bamboo.context.JfrogCliDeploymentContext;

import java.util.Map;

/**
* Task configurator for the JFrog CLI Deployment task.
* Populates the UI context with the configured Artifactory servers and the saved jf command.
*/
public class JfrogCliDeploymentConfiguration extends AbstractArtifactoryConfiguration {

@Override
public void populateContextForCreate(@NotNull Map<String, Object> context) {
super.populateContextForCreate(context);
context.put("serverConfigManager", serverConfigManager);
context.put("selectedServerId", -1);
context.put(JfrogCliDeploymentContext.JF_COMMAND, "");
}

@Override
public void populateContextForEdit(@NotNull Map<String, Object> context,
@NotNull TaskDefinition taskDefinition) {
super.populateContextForEdit(context, taskDefinition);
populateContextWithConfiguration(context, taskDefinition, JfrogCliDeploymentContext.getFieldsToCopy());

String selectedServerId = taskDefinition.getConfiguration().get(JfrogCliDeploymentContext.SERVER_ID);
context.put("serverConfigManager", serverConfigManager);
context.put("selectedServerId", StringUtils.defaultIfBlank(selectedServerId, "-1"));
context.put(JfrogCliDeploymentContext.JF_COMMAND,
StringUtils.defaultString(taskDefinition.getConfiguration().get(JfrogCliDeploymentContext.JF_COMMAND)));
}

@NotNull
@Override
public Map<String, String> generateTaskConfigMap(@NotNull ActionParametersMap params,
@Nullable TaskDefinition previousTaskDefinition) {
Map<String, String> taskConfigMap = super.generateTaskConfigMap(params, previousTaskDefinition);
taskConfiguratorHelper.populateTaskConfigMapWithActionParameters(
taskConfigMap, params, JfrogCliDeploymentContext.getFieldsToCopy());
return taskConfigMap;
}

@Override
public boolean taskProducesTestResults(@NotNull TaskDefinition taskDefinition) {
return false;
}

@Override
protected String getKey() {
return JfrogCliDeploymentContext.PREFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public String getBuildNumber(BuildContext buildContext) {
return String.valueOf(buildContext.getBuildNumber());
}

/**
* Get build name from context without falling back to a build plan.
* Used by deployment-project tasks where no {@link BuildContext} is available.
*/
public String getBuildName() {
return env.get(BUILD_NAME);
}

/**
* Get build number from context without falling back to a build plan.
* Used by deployment-project tasks where no {@link BuildContext} is available.
*/
public String getBuildNumber() {
return env.get(BUILD_NUMBER);
}

public String getOverriddenUsername(Map<String, String> runtimeTaskContext, Log log, boolean deployer) {
switch (StringUtils.defaultString(deployer ? getDeployerOverrideCredentialsChoice() : getResolverOverrideCredentialsChoice())) {
case CVG_CRED_NO_OVERRIDE:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jfrog.bamboo.context;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Set;

/**
* Configuration context for the JFrog CLI Deployment task.
* Holds the server selection and the free-form jf CLI command entered by the user.
*/
public class JfrogCliDeploymentContext {

public static final String PREFIX = "artifactory.jfrogCli.";
public static final String SERVER_ID = PREFIX + "serverId";
public static final String JF_COMMAND = PREFIX + "command";

private static final Set<String> FIELDS_TO_COPY = Set.of(SERVER_ID, JF_COMMAND);

private final Map<String, String> env;

public JfrogCliDeploymentContext(Map<String, String> env) {
this.env = env;
}

public long getSelectedServerId() {
String serverId = env.get(SERVER_ID);
if (StringUtils.isBlank(serverId)) {
return -1;
}
try {
return Long.parseLong(serverId);
} catch (NumberFormatException e) {
return -1;
}
}

public String getJfCommand() {
return StringUtils.trimToEmpty(env.get(JF_COMMAND));
}

public static Set<String> getFieldsToCopy() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getFieldsToCopy() allocates a new HashSet on every call.
Should be a static constant: private static final Set FIELDS_TO_COPY = Set.of(SERVER_ID, JF_COMMAND);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed it.

return FIELDS_TO_COPY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.jfrog.bamboo.task;

import com.atlassian.bamboo.deployments.execution.DeploymentTaskContext;
import com.atlassian.bamboo.task.CommonTaskContext;
import com.atlassian.bamboo.task.TaskException;
import com.atlassian.bamboo.task.TaskResult;
import com.atlassian.bamboo.task.TaskResultBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jfrog.bamboo.admin.ServerConfig;
import org.jfrog.bamboo.admin.ServerConfigManager;
import org.jfrog.bamboo.context.JfrogCliDeploymentContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Deployment-project variant of the JFrog CLI task.
*
* <p>Allows running any {@code jf} CLI command from a Bamboo Deployment Project environment.
* The selected JFrog server configuration (URL, credentials) is injected into the process
* environment via the standard JFrog CLI environment variables so that the user-supplied
* command does not need to embed credentials.
*
* <p>The {@code jf} binary must be pre-installed and available on the build agent's PATH.
*/
public class ArtifactoryDeploymentJfrogCliTask extends ArtifactoryDeploymentTaskType {

/** Standard JFrog CLI environment variables for server authentication. */
private static final String JF_ENV_URL = "JF_URL";
private static final String JF_ENV_USER = "JF_USER";
private static final String JF_ENV_PASSWORD = "JF_PASSWORD";

private JfrogCliDeploymentContext cliContext;
private ServerConfig serverConfig;

@Override
protected void initTask(@NotNull CommonTaskContext context) throws TaskException {
super.initTask(context);
cliContext = new JfrogCliDeploymentContext(context.getConfigurationMap());

ServerConfigManager serverConfigManager = ServerConfigManager.getInstance();
serverConfig = serverConfigManager.getServerConfigById(cliContext.getSelectedServerId());
if (serverConfig == null) {
throw new TaskException(
"JFrog CLI Deployment task: no Artifactory server is configured. " +
"Please select a server in the task configuration.");
}
}

@NotNull
@Override
protected TaskResult runTask(@NotNull DeploymentTaskContext deploymentTaskContext) {
String jfCommand = cliContext.getJfCommand();
if (StringUtils.isBlank(jfCommand)) {
buildInfoLog.error("JFrog CLI Deployment task: the 'JFrog CLI command' field is empty.");
return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
}

try {
int exitCode = executeJfCommand(jfCommand, deploymentTaskContext.getWorkingDirectory());
if (exitCode != 0) {
buildInfoLog.error("JFrog CLI command exited with code " + exitCode);
return TaskResultBuilder.newBuilder(deploymentTaskContext).failed().build();
}
return TaskResultBuilder.newBuilder(deploymentTaskContext).success().build();
} catch (InterruptedException e) {
// Restore interrupt status so callers / Bamboo can react to the cancellation.
Thread.currentThread().interrupt();
buildInfoLog.error("JFrog CLI command was interrupted: " + e.getMessage(), e);
return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
} catch (IOException e) {
buildInfoLog.error("Exception while running JFrog CLI command: " + e.getMessage(), e);
return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
}
}

/**
* Executes the user-supplied jf CLI command as a subprocess.
*
* <p>Auth is injected through environment variables so the user never has to embed
* credentials in the command string.
*/
private int executeJfCommand(String jfCommand, File workingDir)
throws IOException, InterruptedException {

List<String> commandParts = buildCommandParts(jfCommand);
buildInfoLog.info("Running: " + String.join(" ", commandParts));

ProcessBuilder pb = new ProcessBuilder(commandParts);
pb.directory(workingDir);
pb.redirectErrorStream(true);

Map<String, String> env = new HashMap<>(pb.environment());
env.put(JF_ENV_URL, serverConfig.getUrl());
if (StringUtils.isNotBlank(serverConfig.getUsername())) {
env.put(JF_ENV_USER, serverConfig.getUsername());
}
if (StringUtils.isNotBlank(serverConfig.getPassword())) {
env.put(JF_ENV_PASSWORD, serverConfig.getPassword());
}
pb.environment().putAll(env);

Process process = pb.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
logger.addBuildLogEntry(line);
}
}
return process.waitFor();
}

/**
* Splits the raw command string into tokens, prepending the "jf" binary.
* The user enters the command WITHOUT the leading "jf", e.g. "rt dl --spec=spec.json".
* If the user already prefixed "jf", the binary is still only added once.
*/
private List<String> buildCommandParts(String jfCommand) {
List<String> parts = new ArrayList<>();
parts.add("jf");

String trimmed = jfCommand.trim();
// Strip redundant leading "jf" if the user included it
if (trimmed.startsWith("jf ")) {
trimmed = trimmed.substring(3).trim();
}

// Simple whitespace split; respects quoted args via shell on each OS
for (String token : trimmed.split("\\s+")) {
if (!token.isEmpty()) {
parts.add(token);
}
}
return parts;
}

@Override
protected ServerConfig getUsageServerConfig() {
return serverConfig;
}

@Override
protected String getTaskUsageName() {
return "deployment_jfrog_cli";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.jfrog.bamboo.task;

import com.atlassian.bamboo.deployments.execution.DeploymentTaskContext;
import com.atlassian.bamboo.task.CommonTaskContext;
import com.atlassian.bamboo.task.TaskException;
import com.atlassian.bamboo.task.TaskResult;
import com.atlassian.bamboo.task.TaskResultBuilder;
import com.atlassian.bamboo.variable.CustomVariableContext;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jfrog.bamboo.admin.ServerConfig;
import org.jfrog.bamboo.admin.ServerConfigManager;
import org.jfrog.bamboo.configuration.BuildParamsOverrideManager;
import org.jfrog.bamboo.context.PublishBuildInfoContext;
import org.jfrog.bamboo.util.TaskUtils;
import org.jfrog.build.extractor.builder.BuildInfoBuilder;
import org.jfrog.build.extractor.ci.BuildInfo;
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryManagerBuilder;
import org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager;

import java.util.Date;
import java.util.Map;

/**
* Deployment-project variant of {@link ArtifactoryPublishBuildInfoTask}.
* <p>
* A Bamboo Deployment Project does not provide a {@link com.atlassian.bamboo.v2.build.BuildContext}, so
* this task reads the build name / number directly from its configuration and publishes a minimal
* BuildInfo (without the in-plan build-info aggregation that the build-time task performs).
*
* <p>This task addresses customer requests for the JFrog plugin to be selectable from the Deployment
* Projects task picker, where previously only "Artifactory Download" / "Artifactory Deployment" were
* available.
*/
public class ArtifactoryDeploymentPublishBuildInfoTask extends ArtifactoryDeploymentTaskType {

public static final String TASK_NAME = "artifactoryDeploymentPublishBuildInfoTask";

private CustomVariableContext customVariableContext;
private ServerConfig publishServerConfig;
private PublishBuildInfoContext publishBuildInfoContext;

@Override
protected void initTask(@NotNull CommonTaskContext context) throws TaskException {
super.initTask(context);
publishBuildInfoContext = new PublishBuildInfoContext(context.getConfigurationMap());
publishServerConfig = resolvePublishServerConfig(context);
}

@NotNull
@Override
protected TaskResult runTask(@NotNull DeploymentTaskContext deploymentTaskContext) {
String buildName = publishBuildInfoContext.getBuildName();
String buildNumber = publishBuildInfoContext.getBuildNumber();
if (StringUtils.isBlank(buildName) || StringUtils.isBlank(buildNumber)) {
String message = "Build name and build number must be configured for the deployment Publish Build Info task.";
buildInfoLog.error(message);
return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
}

ArtifactoryManagerBuilder clientBuilder = TaskUtils.getArtifactoryManagerBuilderBuilder(publishServerConfig, buildInfoLog);
try (ArtifactoryManager client = clientBuilder.build()) {
BuildInfo build = new BuildInfoBuilder(buildName)
.number(buildNumber)
.startedDate(new Date())
.build();
client.publishBuildInfo(build, "");
buildInfoLog.info("Published build-info for '" + buildName + "/" + buildNumber + "' to " + client.getUrl());
} catch (Exception e) {
buildInfoLog.error("Exception occurred while publishing build-info: " + e.getMessage(), e);
return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
}
return TaskResultBuilder.newBuilder(deploymentTaskContext).success().build();
}

private ServerConfig resolvePublishServerConfig(@NotNull CommonTaskContext context) {
ServerConfigManager serverConfigManager = ServerConfigManager.getInstance();
ServerConfig selectedServerConfig = serverConfigManager.getServerConfigById(publishBuildInfoContext.getArtifactoryServerId());
if (selectedServerConfig == null) {
throw new IllegalArgumentException("Could not find Artifactory server. Please check the Artifactory server in the task configuration.");
}
Map<String, String> runtimeContext = context.getRuntimeTaskContext();
return TaskUtils.getDeploymentServerConfig(
publishBuildInfoContext.getOverriddenUsername(runtimeContext, buildInfoLog, true),
publishBuildInfoContext.getOverriddenPassword(runtimeContext, buildInfoLog, true),
serverConfigManager, selectedServerConfig, new BuildParamsOverrideManager(customVariableContext));
}

@Override
protected ServerConfig getUsageServerConfig() {
return publishServerConfig;
}

@Override
protected String getTaskUsageName() {
return "deployment_publish_build_info";
}

public void setCustomVariableContext(CustomVariableContext customVariableContext) {
this.customVariableContext = customVariableContext;
}
}
Loading
Loading