gitjenkinsnightly-build

Declarative Jenkins pipeline with Nightly deploy for master branch


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

  1. CI --> PollSCM every 5 min (only master), build and run tests.
  2. NIghtly --> Run every night (build, test, integration test and deploy on nightly server)
  3. Sprintly --> It is a parameterized job that runs with manually created tags every Thursday. (build, test, integration test and deploy on sprintly server)

For this purpose, I have a small project in spring with maven that will be the best example for me to start (Simple, easy and fast to be built).

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

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 runs every branch when something is pull to Git and also Run around 1 oclock in the night (but still running all the branches).

Any idea or hint to do that? The code to do the deployment is not required I only want to know how to filter/split this branches being in the same JenkinsFile

Many thanks to all!

Edit: I can also use but It will run all the branches in the night (can I made this filter only for the Cron job?)

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

Solution

  • After reading my own question four months later I realize that I was totally wrong in how I try setup the triggers and the jobs. We should have 3 different jobs:

    1. Multibranch pipeline that will run the maven build in every branch (Can be configured to poll the scm every n minutes or started by a webhook in the repository.
    2. Pipeline(called nightly) that will be configured to be trigger at night (in the job configuration, not in the pipeline) and will deploy to nightly system and using only the master branch (also configured in the Jenkins job)
    3. Pipeline(called sprintly) but parameterized to run with a certain tag and using only the master branch

    The pipeline should be kept at simple like :

    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 will be happy to help :)