如何让 Jenkins 管道只为给定的分支构建
How to let Jenkins pipeline build only for given branches
在下面的管道中,我试图只检查开发分支来构建给定的项目。如何确保管道只有 运行 开发、掌握和发布分支?
我应该为主分支添加单独的阶段,为发布分支添加另一个阶段吗?相反,我试图仅在开发或主分支或发布分支发生变化时才构建此管道,而忽略任何其他分支的构建。
在 Jenkins > Freestyle 项目 > 源代码管理 > Git > 用户可以在 Branch specifier 中输入特定的分支。如何使用管道实现类似的功能?
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
'''
}
}
}
}
好吧,您可以在 groovy 中编写条件,使用 when 语句来执行此操作。像这样
stage('build'){
when {
expression {
env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master'
}
}
steps{
script{
sh 'mvn clean deploy '
}
}
}
您可以使用 Multibranch Pipeline 项目并添加其 Branch Sources → Git → 行为:
按名称过滤(使用正则表达式)
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
按名称过滤(带通配符)
- 包括
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
在下面的管道中,我试图只检查开发分支来构建给定的项目。如何确保管道只有 运行 开发、掌握和发布分支? 我应该为主分支添加单独的阶段,为发布分支添加另一个阶段吗?相反,我试图仅在开发或主分支或发布分支发生变化时才构建此管道,而忽略任何其他分支的构建。
在 Jenkins > Freestyle 项目 > 源代码管理 > Git > 用户可以在 Branch specifier 中输入特定的分支。如何使用管道实现类似的功能?
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
'''
}
}
}
}
好吧,您可以在 groovy 中编写条件,使用 when 语句来执行此操作。像这样
stage('build'){
when {
expression {
env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master'
}
}
steps{
script{
sh 'mvn clean deploy '
}
}
}
您可以使用 Multibranch Pipeline 项目并添加其 Branch Sources → Git → 行为:
按名称过滤(使用正则表达式)
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
按名称过滤(带通配符)
- 包括
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