Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile and different strategies for branches

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:

  • pull-request build - it will be merged to master after code review, and if build works
  • manual pull-request build - a build that does the same as above, but can be triggered manually by the user (e.g. in case we have some unstable test)
  • an initial continuous deliver pipeline - this will build the code, deploy to repository, install artifacts from repository on the target server and start the application there

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?

  1. How to get the name of currently executing branch in multi-branch job type?

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

like image 904
Krzysztof Krasoń Avatar asked Apr 19 '16 19:04

Krzysztof Krasoń


People also ask

What is branching strategy in Jenkins?

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.

How do I configure multiple branches in Jenkins?

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.

Can a Jenkinsfile have multiple pipelines?

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.

What are the 3 types of pipelines in Jenkins?

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.


2 Answers

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   } } 
like image 55
Sukhman Sandhu Avatar answered Sep 24 '22 11:09

Sukhman Sandhu


Using this post, this worked for me:

        stage('...') {             when {                 expression { env.BRANCH_NAME == 'master' }             }             steps {                 ...             }         }  
like image 44
Mickaël Avatar answered Sep 21 '22 11:09

Mickaël