Reputation: 77
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
Reputation: 14762
You can use a Multibranch Pipeline project and add at its Branch Sources → Git → Behaviours:
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)
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
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
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