简体   繁体   中英

jenkins configuration for building on different branches

I am doing code review with gerritcodereview and I need to create a jenkins pipeline for CI, CD. I am using the events triggered by gerrit trigger plugin.

I want to obtain this:

PastchSet Created

  • build start on refs/changes/**/**/** branch
  • report results to gerrit for code review

Change Merged(into develop) or Ref Updated(develop)

  • build start on origin/develop branch
  • deploy code to internal server

Ref Updated(master)

  • build start on origin/master branch
  • deploy code to external server

Questions for which I didn't find good answers:

  • do I need to use a simple pipeline or multibranch pipeline?
  • how do I start the build on the correct branch?
  • how can I checkout the correct branch using a Jenkinsfile instead of using the configuration page?

You should create multibranch pipeline, and write your declarative/scripted pipeline in Jenkinsfile

example pipeline

pipeline {
    agent any

    tools {
        maven 'maven-3.3.6'
        jdk 'jdk-11'
    }

    options {
        buildDiscarder(logRotator(numToKeepStr: '5'))
    }

    stages {
        stage('Build/Test') {
            when {
                changeRequest()
            }
            steps {
                sh "mvn clean verify"
            }
            post {
                success {
                    gerritReview labels: [Verified: 1], message: "Successful build, ${env.RUN_DISPLAY_URL}."
                }
                unstable {
                    gerritReview labels: [Verified: 0], message: "Unstable build, ${env.RUN_DISPLAY_URL}"
                }
                failure {
                    gerritReview labels: [Verified: -1], message: "Failed build, ${env.RUN_DISPLAY_URL}"
                }
            }
        }


        stage('Deploy') {
            when {
                branch 'develop'
            }
            steps {
                sh 'mvn deploy'
            }
        }
    }
}

stage build&test will run for any change in changeRequest, any new change, or patchset will trigger this stage stage deploy will be triggered for any change merged to develop. You could have multiple stages for one branch, they will be executed in sequence

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM