I'm trying to use Jenkins file for all our builds in Jenkins, and I have following problem. We basically have 3 kind of builds:
How should I contain all of the above builds into a single Jenkinsfile. Right now the only idea I have is to make a giant if that will check which branch it is and will do the steps.
So I have two questions:
1. Is that appropriate way to do it in Jenkinsfile?
For reference, here's my current Jenkinsfile
:
def servers = ['server1', 'server2'] def version = "1.0.0-${env.BUILD_ID}" stage 'Build, UT, IT' node { checkout scm env.PATH = "${tool 'Maven'}/bin:${env.PATH}" withEnv(["PATH+MAVEN=${tool 'Maven'}/bin"]) { sh "mvn -e org.codehaus.mojo:versions-maven-plugin:2.1:set -DnewVersion=$version -DgenerateBackupPoms=false" sh 'mvn -e clean deploy' sh 'mvn -e scm:tag' } } def nodes = [:] for (int i = 0; i < servers.size(); i++) { def server = servers.get(i) nodes["$server"] = { stage "Deploy to INT ($server)" node { sshagent(['SOME-ID']) { sh """ ssh ${server}.example.com <<END hostname /apps/stop.sh yum -y update-to my-app.noarch /apps/start.sh END""".stripIndent() } } } } parallel nodes
EDIT: removed opinion based question
The right branching strategy makes it easier to deliver the code especially for teams working in agile, which involves a multi-developer environment with shorter delivery time, without stepping over each other's code.
Step 1: From the Jenkins home page create a “new item”. Step 2: Select the “Multibranch pipeline” from the option and click ok. Step 3: Click “Add a Source” and select Github. Step 4: Under the credentials field, select Jenkins, and create a credential with your Github username and password.
A multi-branch pipeline project always includes a Jenkinsfile in its repository root. Jenkins automatically creates a sub-project for each branch that it finds in a repository with a Jenkinsfile . Multi-branch pipelines use the same version control as the rest of your software development process.
Declarative versus Scripted Pipeline syntax Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which: provides richer syntactical features over Scripted Pipeline syntax, and.
You can add If statement for multiple stages if you want to skip multiple stages according to the branch as in:
if(env.BRANCH_NAME == 'master'){ stage("Upload"){ // Artifact repository upload steps here } stage("Deploy"){ // Deploy steps here } }
or, you can add it to individual stage as in:
stage("Deploy"){ if(env.BRANCH_NAME == 'master'){ // Deploy steps here } }
Using this post, this worked for me:
stage('...') { when { expression { env.BRANCH_NAME == 'master' } } steps { ... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With