Jenkins' Declarative Pipeline with Nightly Deployment for Master Branch

I wanted to translate some jobs to the new Jenkins 2.0 declarative pipelines. At the moment they have 3 different tasks:

  • CI -> PollSCM every 5 minutes (master only), build and run tests.
  • NIghtly -> Run every night (build, test, integrate and deploy to the night server).
  • Sprintly -> This is a parameterized job that runs with hand-created tags every Thursday. (build, test, integrate and deploy to sprintly server).

For this purpose I have a small project in spring with maven which will be the best example for me (simple, simple and fast to build).

At the moment I already have a Multibranch pipeline for building CI, but I want to integrate Nightly and Sprintly into this work.

  • Night: Run a Cron job at night over the master branch to be deployed to the night server.
  • Sprint: Create on top of the Sprintly_tag I generated in the master branch that will be deployed to the Sprintly server.

At the moment I have this JenkinsFile

pipeline {
agent {
    label 'master'
}
tools {
    maven "Apache Maven 3.3.9"
    jdk "Java JDK 1.8 U102"
}
triggers {
        cron ('H(06-08) 01 * * *')
        pollSCM('H/5 * * * *')
}
stages {
    stage('Build') {
        steps {
            sh 'mvn -f de.foo.project.client/ clean package'
        }
        post {
            always {
                junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
                archiveArtifacts allowEmptyArchive: true, artifacts: '**/target/*.war'
            }
        }
    }
    stage('Deploy') {
                sh 'echo "Deploy only master"'
    }
}

      

It starts each branch when something pulls up to Git, and also runs around 1am (but all branches are still running).

Any idea or hint on this? No code is required for deployment. I only want to know how to filter / split these branches in the same JenkinsFile.

Many thanks to everyone!

Edit: I can also use, but it will run all branches at night (can I make this filter for the Cron job only?)

        stage('DeployBranch') {
        when { branch 'story/RTS-525-task/RTS-962'}
        steps {
            sh 'echo "helloDeploy in the branch"'
        }
    }
    stage('DeployMaster') {
        when { branch 'master'}
        steps {
            sh 'echo "helloDeploy in the master"'
        }
    }

      

+3


source to share


1 answer


After reading my own question four months later, I realize that I was completely wrong about how I try to set up triggers and jobs. We should have 3 different tasks:

  • Multichannel pipeline that will run a maven build on each branch (can be configured to poll scm every n minutes or run a webhook on the repository.
  • A pipeline (called night) that will be configured to run at night (in the job configuration, not in the pipeline), and will deploy to the night system and only use the master branch (also set in the Jenkins job)
  • Pipeline (called sprintly), but parameterized to work on a specific tag and using only the master branch

The pipeline should be as simple as:



pipeline {
agent {
    label 'master'
}
tools {
    maven "Apache Maven 3.3.9"
    jdk "Java JDK 1.8 U102"
}
stages {
    stage('Build') {
        steps {
            sh 'mvn -f de.foo.project.client/ clean package'
        }
        post {
            always {
                junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
                archiveArtifacts allowEmptyArchive: true, artifacts: '**/target/*.war'
            }
        }
    }
    stage('Deploy') {
           when (env.JOB_NAME.endsWith('nightly')
                sh 'echo "Deploy on nighlty"'
            when (env.JOB_NAME.endsWith('sprintly')
                sh 'echo "Deploy only sprintly"'
    }
}

      

If you have any questions, let me know and I'll be happy to help :)

+2


source







All Articles