-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.jfrog.bamboo.context; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.HashSet; | ||
|
|
||
| /** | ||
| * 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 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() { | ||
| Set<String> fields = new HashSet<>(); | ||
| fields.add(SERVER_ID); | ||
| fields.add(JF_COMMAND); | ||
| return fields; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| 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 (IOException | InterruptedException e) { | ||
| buildInfoLog.error("Exception while running JFrog CLI command: " + e.getMessage(), e); | ||
| return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed it. |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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"; | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The diff includes registrations for these two task types, but neither class exists in this PR or anywhere in the codebase. This can cause the plugin to fail to load at startup in Bamboo.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| [@ui.bambooSection titleKey='artifactory.task.jfrogCli.deployment.title'] | ||
|
|
||
| [@ww.select | ||
| name='artifactory.jfrogCli.serverId' | ||
| labelKey='artifactory.task.jfrogCli.deployment.server' | ||
| list=serverConfigManager.allServerConfigs | ||
| listKey='id' | ||
| listValue='url' | ||
| emptyOption=true | ||
| toggle='true' | ||
| /] | ||
|
|
||
| <div id="jfrogCliCommandDiv"> | ||
| [@ww.textarea | ||
| name='artifactory.jfrogCli.command' | ||
| labelKey='artifactory.task.jfrogCli.deployment.command' | ||
| descriptionKey='artifactory.task.jfrogCli.deployment.command.description' | ||
| rows='4' | ||
| cols='80' | ||
| cssClass='long-field' | ||
| /] | ||
| </div> | ||
|
|
||
| [/@ui.bambooSection] |
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.