How to directly test pipeline scripts in Jenkins
I was recently looking for a way to develop continuous integration scripts, without ‘polluting’ the git base of the project with temporary commits.
My goal was to find a way to write scripts directly into Jenkins’ web UI. I was aware of the fact that it’s possible to include bash scripts directly into a pipeline: however, I think that finding the correct syntax to escape correctly the apostrophes and the ‘$’ is incredibly hard.
The easiest way to proceed seems to me to be the following:
- Install the [Config File Provider] plugin (https://wiki.jenkins.io/display/JENKINS/Config+File+Provider+Plugin)
- Create any script: Go to “Administer Jenkins” => “Configuration files” Then, click on the top left on “Add a new config” Then choose the type of file you want to add, and copy and paste the value of the ‘ID’ field somewhere.
- Use this script in a Jenkins job or pipeline Here is an example of a basic pipeline that will allow you to view the contents of this script … and also run it!
pipeline { agent any stages { stage('Build') { steps { configFileProvider([configFile(fileId: '9148c22f-0691-4ff2-a717-e687d7486ba3', variable: 'custom_script')]) { sh("cat $custom_script") } } } } }
The value of $custom_script will be replaced here by the absolute link to the script (this one is managed internally by Jenkins, in my example, the link was: /var/jenkins_home/workspace/test@tmp/config4279351128568850398tmp).
As it is a bash script, to execute it, you’ll just have to replace “cat” by “bash”.