Jenkinsfile and different strategies for branches

无人久伴 提交于 2019-12-03 03:34:11

问题


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 - it will be merged to master after code review, and if build works
  • manual pull-request build - a build that does the same as above, but can be triggered manually by the user (e.g. in case we have some unstable test)
  • an initial continuous deliver pipeline - this will build the code, deploy to repository, install artifacts from repository on the target server and start the application there

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.

So I have two questions:

1. Is that appropriate way to do it in Jenkinsfile?

  1. How to get the name of currently executing branch in multi-branch job type?

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

EDIT: removed opinion based question


回答1:


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



回答2:


1) I don't know if it is appropriate, but if it resolves your problem, I think is appropriate enough.

2) In order to know the name of the branch you can use BRANCH_NAME variable, its name is taken from the branch name.

${env.BRANCH_NAME}

Here is the answer: Jenkins Multibranch pipeline: What is the branch name variable?




回答3:


We followed the model used by fabric8 for builds, tweaking it as we needed, where the Jenkinsfile is used to define the branch and deployment handling logic, and a release.groovy file for build logic.

Here's what our Jenkinsfile looks like for a pipeline that continuously deploys into DEV from master branch:

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

Note: you can find the code for our global pipeline library here.




回答4:


Using this post, this worked for me:

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




回答5:


Don't know if this what you want.. I prefer because it's look more structured.

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



回答6:


for questions 2 you may be able to do

sh 'git branch > GIT_BRANCH' def gitBranch = readFile 'GIT_BRANCH'

since you're checking out from git




回答7:


In my scenarium, I have needed run a stage Deploy Artifactory only if the branch was master(webhook Gitlab), otherwise I couldn't perform the deploy.

Below the code of my jenkinsfile:

stages {

    stage('Download'){

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

        }

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

            }       
    }

}



来源:https://stackoverflow.com/questions/36727721/jenkinsfile-and-different-strategies-for-branches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!