acm
acm

Reputation: 77

How to let Jenkins pipeline build only for given branches

In the following pipeline, I am trying to check out only develop branch to build the given project. How do I make sure that the pipeline only run develop, master and release branches? Should I add separate stages for master branch and another for release branch. Instead I am trying to let this pipeline build only when there are changes in develop or master or release branches and ignore building for any other branches.

In Jenkins > Freestyle project > Source code management > Git > User can enter specific branches in Branch specifier. How can I implement similar one using pipeline?

pipeline {
agent any
tools {
    maven "${mvnHome}"
    jdk  'jdk8'
}
stages {
stage('Checkout project') {
    steps {
        git branch: 'develop',
            credentialsId: 'someid',
            url: 'https://project.git'
    }
}

    stage('build') {
        steps {
            sh '''
             mvn clean deploy
            '''
            }
        }
    }
}

Upvotes: 0

Views: 12275

Answers (2)

Gerold Broser
Gerold Broser

Reputation: 14762

You can use a Multibranch Pipeline project and add at its Branch SourcesGitBehaviours:

  • Filter by name (with regular expression)

    A Java regular expression to restrict the names. Names that do not match the supplied regular expression will be ignored.
    NOTE: this filter will be applied to all branch like things, including change requests

  • Filter by name (with wildcards)

    • Include

    Space-separated list of name patterns to consider. You may use * as a wildcard; for example: master release*
    NOTE: this filter will be applied to all branch like things, including change requests

    • Exclude

    Space-separated list of name patterns to ignore even if matched by the includes list. For example: release alpha-* beta-*
    NOTE: this filter will be applied to all branch like things, including change requests

Upvotes: 1

shiva
shiva

Reputation: 434

Well, you can write conditions in groovy to do this using when statement. Something like this

stage('build'){
  when {
    expression {
      env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master'
      }
  }
 steps{
   script{
   sh 'mvn clean deploy '
   }
 }
}

Upvotes: 1

Related Questions