0

The tl;dr:

I have a Jenkins job (Job #1). I want to call a second job using the Promoted Build Plugin (Job #2). Job #2 is parameterized and needs an artifactory URL generated by Job #1 - how can I pass a parameter from Job #1 to Job #2? Using an Approval Parameter doesn't seem to work, since this parameter is composed of environment variables. I literally just want to create an environment variable in Job #1 and then pass it as a param to Job #2 - surely there is a way to do this?


Use Case:

I have a Jenkins job that uploads an artifact to artifactory. I am trying to use the Promoted Build Plugin to trigger a second job that takes the artifactory URL as a parameter. However, I can't seem to find any way to pass it the artifactory URL - in the job itself, I create the URL from $DEPLOY_URL/$DEPLOY_URL-$BUILD_ID.tar.gz - but, when the promotion script runs, $BUILD_ID has a different value, since the promotion technically takes place inside a different job (as far as I can tell, the way the plugin works is it creates a little mini-job that handles build promotions, and they have their own numbering). I tried just saving the full URL into a variable called $ARTIFACTORY_URL in a shell script, but again, the promotion job doesn't seem to have access to it. How can I pass a variable from my Jenkins job to the promotion job?

Nicole Stein
  • 925
  • 1
  • 9
  • 23

2 Answers2

1

In your build you could add a shell step which creates a properties file in the workspace where it can be picked up by the parameterized trigger at promotion time. An example shell step could look like this:

# Create a properties file for the promotion step
cat > $WORKSPACE/artifactory.properties <<EOF
ARTIFACTORY_URL=$DEPLOY_URL/$DEPLOY_URL-$BUILD_ID.tar.gz
EOF

# Verify the properties file contents
cat ${WORKSPACE}/artifactory.properties

Then in the promotion step add a Trigger parameterized build on other project action, set the job you want to trigger then under Add parameters choose Parameters from properties file and enter the name of the properties file. This should provide the parameters required to the triggered job.

Adam Reid
  • 11
  • 1
  • Thanks Adam. This solution works fine for the use case. The limitation I found for my case with this solution is that the artifactory.properties file is overidden as soon as a new build is completed and the url is not valid for the previous builds. My use case is to be able to deploy any build not the latest. – gaelperret Aug 30 '16 at 14:04
1

I worked on a solution which solves the limitation of Adam Reid's solution.

In Job 1:

a. Add a build step 'Execute shell' (Adam's solution):

# Create a properties file for the promotion step
cat > $WORKSPACE/artifactory.properties <<EOF
ARTIFACTORY_URL=$DEPLOY_URL/$DEPLOY_URL-$BUILD_ID.tar.gz
EOF

# Verify the properties file contents
cat ${WORKSPACE}/artifactory.properties

b. Add a post-build action 'Archive the artifacts' with:

'Files to archive' = artifactory.properties

c. In the 'Trigger/call buid on nother projects' of the promotion, add 'Predefined parameters' with:

PROMOTED_JOB_NAME=${PROMOTED_JOB_NAME}
PROMOTED_NUMBER=${PROMOTED_NUMBER}

In Job 2:

a. Add a build step 'copy artifacts from another project' with:

'Project name' = $PROMOTED_JOB_NAME

'which build' = specific build

'build number' = $PROMOTED_NUMBER

'Artifacts' = artifactory.properties

b. Add a build step 'Inject environment variables' with

'Properties File Path' = artifactory.properties

gaelperret
  • 318
  • 1
  • 11