Jenkinsfile和分支的不同策略

时间:2016-04-19 19:29:40

标签: jenkins jenkins-pipeline

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

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

我应该如何将所有上述构建包含在一个Jenkins文件中。 现在我唯一的想法就是创造一个巨人,如果它将检查它是哪个分支并将执行这些步骤。

所以我有两个问题:

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

  1. 如何获取多分支作业类型中当前正在执行的分支的名称?
  2. 供参考,这是我目前的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
    

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

7 个答案:

答案 0 :(得分:25)

如果要根据分支跳过多个阶段,可以为多个阶段添加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
  }
}

答案 1 :(得分:7)

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

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

${env.BRANCH_NAME}

以下是答案: Jenkins Multibranch pipeline: What is the branch name variable?

答案 2 :(得分: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()
  }
}

注意:您可以找到我们的全局管道库here的代码。

答案 3 :(得分:0)

问题2你可以做到

sh&#39; git branch&gt; GIT_BRANCH&#39; def gitBranch = readFile&#39; GIT_BRANCH&#39;

因为您要从git

退房

答案 4 :(得分:0)

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

Jenkinsfile

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()
}

Jenkinsfile。的 mybranch .groovy作为

echo 'mybranch'
// Pipeline code here

答案 5 :(得分:0)

在我的场景中,仅当分支为master(webhook Gitlab)时,才需要运行Deploy Artifactory阶段,否则我将无法执行部署。

在我的jenkinsfile的代码下面:

stages {

    stage('Download'){

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

        }

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

            }       
    }

}

答案 6 :(得分:0)

使用this post,这对我有用:

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