Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle nightly build in Jenkins declarative pipeline

I have a multibranch pipeline with a Jenkinsfile in my repo and I am able to have my CI workflow (build & unit tests -> deploy-dev -> approval -> deploy-QA -> approval -> deploy-prod) on every commit. What I would like to do is add SonarQube Analysis on nightly builds in the first phase build & unit tests. Since my build is triggerd by Gitlab I have defined my pipeline triggers as follow :

pipeline {
    ...
    triggers {
        gitlab(triggerOnPush: true, triggerOnMergeRequest: true, branchFilterType: 'All')
    }
    ...
}

To setup my nightly build I have added

triggers {
    ...
    cron('H H * * *')
}

But now, how to execute analysis step if we are only building the job triggered by the cron expression at night ?

My simplified build stage looks as follow :

stage('Build & Tests & Analysis') {
    // HERE THE BEGIN SONAR ANALYSIS  (to be executed on nightly builds)
    bat 'msbuild.exe ...'
    bat 'mstest.exe ...'
    // HERE THE END SONAR ANALYSIS (to be executed on nightly builds)
}
like image 564
Vincent Avatar asked Apr 20 '17 09:04

Vincent


3 Answers

There is the way how to get build trigger information. It is described here: https://jenkins.io/doc/pipeline/examples/#get-build-cause

It is good for you to check this as well: how to get $CAUSE in workflow

Very good reference for your case is https://hopstorawpointers.blogspot.com/2016/10/performing-nightly-build-steps-with.html. Here is the function from that source that exactly matches your need:

// check if the job was started by a timer
@NonCPS
def isJobStartedByTimer() {
    def startedByTimer = false
    try {
        def buildCauses = currentBuild.rawBuild.getCauses()
        for ( buildCause in buildCauses ) {
            if (buildCause != null) {
                def causeDescription = buildCause.getShortDescription()
                echo "shortDescription: ${causeDescription}"
                if (causeDescription.contains("Started by timer")) {
                    startedByTimer = true
                }
            }
        }
    } catch(theError) {
        echo "Error getting build cause"
    }

    return startedByTimer
}
like image 94
Olia Avatar answered Apr 02 '23 20:04

Olia


This works in declarative pipeline

when {
    triggeredBy 'TimerTrigger'
}
like image 32
Jeremy Webb Avatar answered Apr 02 '23 19:04

Jeremy Webb


For me the easiest way is to define a cron in build trigger and verify the hour on the nightly stage using a when expression:

pipeline {
    agent any
    triggers {
        pollSCM('* * * * *') //runs this pipeline on every commit
        cron('30 23 * * *') //run at 23:30:00 
    }

    stages {
        stage('nightly') {
            when {//runs only when the expression evaluates to true
                expression {//will return true when the build runs via cron trigger (also when there is a commit at night between 23:00 and 23:59)
                    return Calendar.instance.get(Calendar.HOUR_OF_DAY) in 23
                }
            }

            steps {
                echo "Running the nightly stage only at night..."
            }
        }
    }
}
like image 38
rmpestano Avatar answered Apr 02 '23 20:04

rmpestano