Jenkinsfile and different strategies for branches

前端 未结 7 1657
星月不相逢
星月不相逢 2021-01-31 03:23

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 me
相关标签:
7条回答
  • 2021-01-31 03:25

    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
      }
    }
    
    0 讨论(0)
  • 2021-01-31 03:30

    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.

    0 讨论(0)
  • 2021-01-31 03:30

    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

    0 讨论(0)
  • 2021-01-31 03:34

    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?

    0 讨论(0)
  • 2021-01-31 03:45

    Using this post, this worked for me:

            stage('...') {
                when {
                    expression { env.BRANCH_NAME == 'master' }
                }
                steps {
                    ...
                }
            }
    
    
    0 讨论(0)
  • 2021-01-31 03:45

    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
    
    0 讨论(0)
提交回复
热议问题