Jenkinsfile 和不同的分支策略 [英] Jenkinsfile and different strategies for branches
问题描述
我正在尝试将 Jenkins 文件用于我们在 Jenkins 中的所有构建,但我遇到了以下问题.我们基本上有 3 种构建:
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 - 它将在代码审查后合并到 master,如果构建有效
- 手动拉取请求构建 - 与上述相同的构建,但可以由用户手动触发(例如,如果我们有一些不稳定的测试)
- 一个初始的持续交付管道 - 这将构建代码、部署到存储库、从存储库安装工件到目标服务器并在那里启动应用程序
我应该如何将上述所有构建包含到单个 Jenkinsfile 中.现在我唯一的想法是做一个巨人,如果它会检查它是哪个分支并会执行步骤.
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.
所以我有两个问题:
1.在 Jenkinsfile 中这样做是否合适?
- 如何获取多分支作业类型中当前执行分支的名称?
作为参考,这是我当前的 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
删除基于意见的问题
推荐答案
如果要根据分支跳过多个阶段,可以为多个阶段添加If语句,如下所示:
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
}
}
这篇关于Jenkinsfile 和不同的分支策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!