Creating a Simple Jenkinsfile Pipeline Script which Called Other Jenkinsfile from Git
Sometimes we want to update some part of our Jenkins job, but if i have like 50 jobs does it means that i have to change fifty pipeline script one by one?
The solution is actually pretty much straigh forward, i can extract most of jenkinsfile script and put it on Git so that i can change it dynamically. Here is my simple script which i put on my github page
stage('Build') { dir("../source") { sh "mvn -v" sh "mvn clean package -f pom.xml" sh "mkdir /tmp/app" def jarFile = sh(returnStdout: true, script: 'find target -maxdepth 1 -regextype posix-extended -regex ".+\\.(jar|war)\$" | head -n 1').trim() sh "cp ${jarFile} /tmp/app/app.jar" withCredentials([file(credentialsId:'Dockerfile', variable:'Dockerfile')]) { sh "cp ${Dockerfile} /tmp/app/Dockerfile" } } } stage('Deploy') { sh "oc new-build --name hello-world-3 --binary -n fuse-on-ocp-c8b3 || true" sh "oc start-build hello-world-3 --from-dir=/tmp/app/ -n fuse-on-ocp-c8b3 --follow --wait" }
I put that Jenkins script on Github, https://github.com/edwin/jenkinsfile-example. And i call on the fly from my existing project pipeline script,
node('maven') { stage('Clone Pipeline') { sh "git config --global http.sslVerify false" sh "git clone https://github.com/edwin/jenkinsfile-example.git" } stage('Clone Code') { sh "git config --global http.sslVerify false" sh "git clone https://github.com/edwin/hello-world.git source" } stage('Start Run from Jenkinsfile on SCM') { dir("jenkinsfile-example") { load 'simple.jenkinsfile' } } }
No Comments