rss

How to Jenkins : chain jobs with parameters

This post describes How To chain jobs with parameter(s).
- Choose optionA if you're using pipeline plugin.
- OptionB is a little bit deprecated, but usefull if you would like to script your build execution or trigger it remotely.

(option A) Define a post action in first job

First recommended and simple way if your are using "Build Pipeline". Go to the first job configuration to append a post-action :

(option B) Chain it using shell scripts


NB: this option will make loosing your pipeline chain.
I recommend to use 2 small dedicated temp jobs to test it first. That would avoid to run unnecessarely long build...

Steps :
  • Install Build Token Root Plugin
  • Define first job with parameter
  • Define second job ("follower") with parameter
  • Define a trigger for the follower
  • Append a shell script to chain the two jobs
  • Try!

Install Build Token Root Plugin

Ask to your Jenkins administrator to install this plugin : Build Token Root Plugin
Wait that Jenkins reboot (required).

Define first job with parameter

Add "branch" parameter to your first job.

Add a shell script to echo the variable.

Define follower job with parameter

Add "branch" parameter to your second job (same as previously).
Add a shell script to echo the variable(same as previously).

Define a trigger for the follower

Add a trigger for the second job. Set a secret token here.

Append a shell script to chain the two jobs

Open the first job configuration to add the following shell script.
You will need to adapt the jenkins host value corresponding to your environment.

# show job parameter
echo branch=${branch}

#  resources
#  https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
#  https://wiki.jenkins-ci.org/display/JENKINS/Build+Token+Root+Plugin
#  http://curiositedevie.blogspot.nl/2016/02/how-to-jenkins-chain-jobs-with.html
export JENKINS_HOST=https://myjenkins.domain.net
export JOB_TOKEN=THISISWONDERFULL
export JOB_NAME=git_config_follower
export JOB_PARAM1=branch=${branch}
export JOB_CAUSE=Launching_follower_with_branch_set_to_${branch}
export JOB_PING="$JENKINS_HOST/buildByToken/buildWithParameters?job=$JOB_NAME&token=$JOB_TOKEN&$JOB_PARAM1&cause=$JOB_CAUSE"
echo $JOB_PING
curl --verbose --insecure $JOB_PING

Try!

Launch the first job with a parameter.

Go to the Job console to check curl http status.
Go to the follower (second) job logs.
Check the follower result!

Aucun commentaire:

Enregistrer un commentaire