-
Notifications
You must be signed in to change notification settings - Fork 60
RTECO-858: Add JFrog CLI Task support for Bamboo Deployment Projects #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/org/jfrog/bamboo/configuration/JfrogCliDeploymentConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/org/jfrog/bamboo/context/JfrogCliDeploymentContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { | ||
| return FIELDS_TO_COPY; | ||
| } | ||
| } | ||
153 changes: 153 additions & 0 deletions
153
src/main/java/org/jfrog/bamboo/task/ArtifactoryDeploymentJfrogCliTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
src/main/java/org/jfrog/bamboo/task/ArtifactoryDeploymentPublishBuildInfoTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed it.