For reasons which are not important we recently needed to pass parameters between separate build steps in a Jenkins job.

The internet(TM) recommended various variants of the create-a-file meem but this wouldn’t help us as we need to pass the parameter to a pre-build build script.

So…

Ensure that Jenkins is using ssh for login

  • See GitHub article on how to check.
  • Install via:
    /home/USERNAME/.ssh/id_dsa.pub in the configuration of USERNAME user in Jenkins.

Add the parameter you want to use to your Jenkins job

This was the key stage I missed

Write to the parameter

#!/bin/bash -l
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

echo "JIRA_COMMENT is initially ${JIRA_COMMENT}"

export JIRA_COMMENT="Set JIRA_COMMENT from shell 1"

echo "JIRA_COMMENT is ${JIRA_COMMENT}"

java -jar ${JENKINS_HOME}/war/WEB-INF/jenkins-cli.jar  -s ${JENKINS_URL} set-build-parameter JIRA_COMMENT "${JIRA_COMMENT}"

echo "JIRA_COMMENT is now ${JIRA_COMMENT}"

Parameter is now available in later Jenkins build step’s

#!/bin/bash -l
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

echo "Second shell [${JIRA_COMMENT}]"

Output

Started by user Jenkins CI User
Building in workspace /Users/ci/.jenkins/workspace/temp
[temp] $ /bin/bash -l /var/folders/5x/kwy9lj794rs_rx98c0gy9fmr0000gn/T/hudson2638950130997721776.sh
JIRA_COMMENT is initially I am JIRA_COMMENT, hello.
JIRA_COMMENT is Set JIRA_COMMENT from shell 1
JIRA_COMMENT is now Set JIRA_COMMENT from shell 1
[temp] $ /bin/bash -l /var/folders/5x/kwy9lj794rs_rx98c0gy9fmr0000gn/T/hudson4959012422667277787.sh
Second shell [Set JIRA_COMMENT from shell 1]
[temp] $ /bin/sh -xe /var/folders/5x/kwy9lj794rs_rx98c0gy9fmr0000gn/T/hudson6909970364064911906.sh
+ exit
Finished: SUCCESS

Aside

This method has the side effect of updating the parameter for any future builds as well.