39

我正在尝试将 Jenkins 文件用于我们在 Jenkins 中的所有构建,但我遇到了以下问题。我们基本上有 3 种构建:

  • pull-request build - 它将在代码审查后合并到 master,如果 build 有效
  • 手动拉取请求构建 - 与上述相同的构建,但可以由用户手动触发(例如,如果我们有一些不稳定的测试)
  • 一个初始的持续交付管道——这将构建代码、部署到存储库、从目标服务器上的存储库安装工件并在那里启动应用程序

我应该如何将上述所有构建包含到单个 Jenkinsfile 中。现在我唯一的想法是做一个巨人,如果它会检查它是哪个分支并会做这些步骤。

所以我有两个问题:

1. 在 Jenkinsfile 中这样做是否合适?

  1. 如何获取多分支作业类型中当前正在执行的分支的名称?

作为参考,这是我当前的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

编辑:删除了基于意见的问题

4

7 回答 7

28

如果要根据分支跳过多个阶段,可以为多个阶段添加 If 语句,如下所示:

if(env.BRANCH_NAME == 'master'){
     stage("Upload"){
        // Artifact repository upload steps here
        }
     stage("Deploy"){
        // Deploy steps here
       }
     }

或者,您可以将其添加到单个阶段,如下所示:

stage("Deploy"){
  if(env.BRANCH_NAME == 'master'){
   // Deploy steps here
  }
}
于 2017-08-03T17:15:39.837 回答
9

使用这篇文章,这对我有用:

        stage('...') {
            when {
                expression { env.BRANCH_NAME == 'master' }
            }
            steps {
                ...
            }
        }

于 2019-06-18T01:22:26.393 回答
7

1)我不知道它是否合适,但如果它解决了你的问题,我认为它足够合适。

2)为了知道分支的名称,您可以使用 BRANCH_NAME 变量,其名称取自分支名称。

${env.BRANCH_NAME}

答案是: Jenkins Multibranch 管道:分支名称变量是什么?

于 2016-04-21T20:41:10.150 回答
3

我们遵循fabric8用于构建的模型,根据需要对其进行调整,其中Jenkinsfile用于定义分支和部署处理逻辑,以及release.groovy用于构建逻辑的文件。

这是我们Jenkinsfile从主分支持续部署到 DEV 的管道的样子:

#!groovy
import com.terradatum.jenkins.workflow.*

node {

  wrap([$class: 'TimestamperBuildWrapper']) {
    checkout scm

    echo "branch: ${env.BRANCH_NAME}"
    def pipeline = load "${pwd()}/release.groovy"

    if (env.DEPLOY_ENV != null) {
      if (env.DEPLOY_ENV.trim() == 'STAGE') {
        setDisplayName(pipeline.staging() as Version)
      } else if (env.DEPLOY_ENV.trim() == 'PROD') {
        setDisplayName(pipeline.production() as Version)
      }
    } else if (env.BRANCH_NAME == 'master') {
      try {
        setDisplayName(pipeline.development() as Version)
      } catch (Exception e) {
        hipchatSend color: 'RED', failOnError: true, message: "<p>BUILD FAILED: </p><p>Check console output at <a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p><p><pre>${e.message}</pre></p>", notify: true, room: 'Aergo', v2enabled: false
        throw e; // rethrow so the build is considered failed
      }
    } else {
      setDisplayName(pipeline.other() as Version)
    }
  }
}

def setDisplayName(Version version) {
  if (version) {
    currentBuild.displayName = version.toString()
  }
}

注意:您可以在此处找到我们的全球管道库的代码。

于 2017-03-07T16:32:00.720 回答
0

不知道这是不是你想要的。我更喜欢它,因为它看起来更有条理。

詹金斯文件

node {
    def rootDir = pwd()

    def branchName = ${env.BRANCH_NAME}

    // Workaround for pipeline (not multibranches pipeline)
    def branchName = getCurrentBranch()

    echo 'BRANCH.. ' + branchName
    load "${rootDir}@script/Jenkinsfile.${branchName}.Groovy"
}

def getCurrentBranch () {
    return sh (
        script: 'git rev-parse --abbrev-ref HEAD',
        returnStdout: true
    ).trim()
}

詹金斯文件。我的分支.Groovy

echo 'mybranch'
// Pipeline code here
于 2017-12-31T13:11:41.310 回答
0

在我的场景中,只有当分支是主分支(webhook Gitlab)时,我才需要运行一个阶段 Deploy Artifactory,否则我无法执行部署。

在我的 jenkinsfile 的代码下面:

stages {

    stage('Download'){

        when{
            environment name: 'gitlabSourceBranch', value: 'master'

        }

        steps{
            echo "### Deploy Artifactory ###"

            }       
    }

}

于 2018-11-28T12:09:10.467 回答
0

对于问题 2,你可以做

sh 'git 分支 > GIT_BRANCH' def gitBranch = readFile 'GIT_BRANCH'

因为你正在从 git 签出

于 2016-04-22T14:21:45.453 回答