Reputation: 2759
I have a jenkinsfile that contains all the steps to build and deploy the newest code to our staging server. Now I need to modify this to support building to production. But building to production can ONLY be done manually and Jenkins cannot start it automatically.
So I have two different branches: develop and master. The develop branch will be used to build to staging and the master branch to production. For the master branch I should need to start the pipeline manually and for the develop branch it can be done fully automatically (on fixed times of the day).
For making it easy to upgrade my code to production I want to have only one jenkinsfile that is a copy of each other in the different branches (so the production jenkinsfile cannot be overwritten with the jenkinsfile of develop branch after merging).
How can I achieve this in jenkinsfile?
My current jenkinsfile looks like:
pipeline {
agent any
stages {
stage('Build') {
steps {
bat 'mvn clean package -Dmaven.test.skip=true'
}
}
stage('Unit/Integration Test') {
parallel {
stage('All Tests') {
steps {
bat 'mvn verify'
}
}
}
}
stage('SonarQube analysis') {
when {
branch 'develop'
}
steps {
withSonarQubeEnv('SonarQube') {
bat 'mvn sonar:sonar'
}
}
}
stage("SonarQube Quality Gate") {
when {
branch 'develop'
}
steps {
timeout(time: 10, unit: 'MINUTES') {
script {
sleep 120
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
stage('Deploy Staging') {
parallel {
stage('Deploy APAC') {
when {
branch 'develop'
}
steps {
script {
build job: 'APAC Staging', parameters: []
}
}
}
stage('Deploy NASA') {
when {
branch 'develop'
}
steps {
script {
build job: 'NASA Staging', parameters: []
}
}
}
stage('Deploy EMEA') {
when {
branch 'develop'
}
steps {
script {
build job: 'EMEA Staging', parameters: []
}
}
}
}
}
}
post {
always {
cleanWs()
}
}
}
Upvotes: 0
Views: 4810
Reputation: 5451
I believe you are trying to consolidate both the Jenkinsfile for deploying for both staging and production to simplify the pipeline.
First define the steps/stages for production environment and enable them only if the branch is master.
if env.BRANCH_NAME == "master" {
EXECUTE STEPS
}
By this you can achieve having same Jenkinsfile for both master and develop branches. These steps/stage will not be executed for other branches apart from master branch.
Along with that you can have specified submitter to promote to production environment like this.
input message: ''Deployment looks good?', ok: 'deploy to Production', submitter: 'USERID'
Hope this helps. Thanks!
Upvotes: 1