Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -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() {
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.

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();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

InterruptedException swallowed without restoring interrupt status

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.

}
}

/**
* 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";
}
}
44 changes: 44 additions & 0 deletions src/main/resources/atlassian-plugin.xml
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

atlassian-plugin.xml registers two non-existent classes (ArtifactoryDeploymentPublishBuildInfoTask, ArtifactoryDeploymentXrayScanTask)

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.
These registrations appear to be leftover/accidental and should be removed (or the classes need to be added — but the PR description makes no mention of them).

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.

Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,50 @@
<exporter class="org.jfrog.bamboo.exporter.ArtifactoryTaskExporter"/>
</taskType>

<!-- Deployment-project variant of "Artifactory Publish Build Info".
Implements DeploymentTaskType so it is selectable from the Deployment Projects task picker. -->
<taskType key="artifactoryDeploymentPublishBuildInfoTask" name="Artifactory Publish Build Info (Deployment)"
class="org.jfrog.bamboo.task.ArtifactoryDeploymentPublishBuildInfoTask">
<description>Publish build-info to Artifactory from a Deployment Project task.</description>
<configuration class="org.jfrog.bamboo.configuration.ArtifactoryPublishBuildInfoConfiguration"/>
<runtimeTaskDataProvider class="org.jfrog.bamboo.security.provider.SharedCredentialsDataProvider"/>
<category name="deployment"/>
<resource type="freemarker" name="edit"
location="templates/plugins/task/editArtifactoryPublishBuildInfoAction.ftl"/>
<resource type="download" name="icon" location="images/artifactory-icon.png"/>
<exporter class="org.jfrog.bamboo.exporter.ArtifactoryTaskExporter"/>
</taskType>

<!-- Deployment-project variant of "Artifactory Xray Scan".
Implements DeploymentTaskType so it is selectable from the Deployment Projects task picker. -->
<taskType key="artifactoryDeploymentXrayScanTask" name="Artifactory Xray Scan (Deployment)"
class="org.jfrog.bamboo.task.ArtifactoryDeploymentXrayScanTask">
<description>Scan a build with JFrog Xray from a Deployment Project task. The scanned build must be published to Artifactory prior to scanning.</description>
<configuration class="org.jfrog.bamboo.configuration.ArtifactoryXrayScanConfiguration"/>
<runtimeTaskDataProvider class="org.jfrog.bamboo.security.provider.SharedCredentialsDataProvider"/>
<category name="deployment"/>
<resource type="freemarker" name="edit"
location="templates/plugins/xray/editArtifactoryXrayScanAction.ftl"/>
<resource type="download" name="icon" location="images/artifactory-icon.png"/>
<exporter class="org.jfrog.bamboo.exporter.ArtifactoryTaskExporter"/>
</taskType>

<!-- JFrog CLI task for Deployment Projects.
Implements DeploymentTaskType so it appears in the Deployment Project task picker.
The jf binary must be pre-installed on the build agent. Server credentials are
injected via JF_URL / JF_USER / JF_PASSWORD environment variables at runtime. -->
<taskType key="artifactoryDeploymentJfrogCliTask" name="JFrog CLI Task"
class="org.jfrog.bamboo.task.ArtifactoryDeploymentJfrogCliTask">
<description>Run any JFrog CLI (jf) command from a Deployment Project task. The jf binary must be available on the agent PATH.</description>
<configuration class="org.jfrog.bamboo.configuration.JfrogCliDeploymentConfiguration"/>
<runtimeTaskDataProvider class="org.jfrog.bamboo.security.provider.SharedCredentialsDataProvider"/>
<category name="deployment"/>
<resource type="freemarker" name="edit"
location="templates/plugins/deployment/editJfrogCliDeploymentTask.ftl"/>
<resource type="download" name="icon" location="images/artifactory-icon.png"/>
<exporter class="org.jfrog.bamboo.exporter.ArtifactoryTaskExporter"/>
</taskType>

<component-import key="taskContextHelper" interface="com.atlassian.bamboo.task.TaskContextHelperService"/>
<component-import key="taskConfiguratorHelper" interface="com.atlassian.bamboo.task.TaskConfiguratorHelper"/>
<component-import key="applicationProperties" interface="com.atlassian.sal.api.ApplicationProperties"/>
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/i18n-jfrog.properties
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ artifactory.task.collectBuildIssues.header.username.description = Override the d
artifactory.task.collectBuildIssues.header.password = Override Default Password
artifactory.task.collectBuildIssues.header.password.description = The password of the user entered above.

#JFrog CLI Deployment Task
artifactory.task.jfrogCli.deployment.title = JFrog CLI Task
artifactory.task.jfrogCli.deployment.server = JFrog configuration to use
artifactory.task.jfrogCli.deployment.command = JFrog CLI command to run
artifactory.task.jfrogCli.deployment.command.description = Enter your JFrog CLI command after 'jf'. There is no need to provide the URL or credentials — they are injected automatically from the selected server configuration. Example: rt dl --spec=download_spec.json

#errors:
error.title=Artifactory error reporting
error.heading=An unexpected error has occurred
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]
Loading