7
votes

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)
}
6

6 Answers

7
votes

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
}
5
votes

This works in declarative pipeline

when {
    triggeredBy 'TimerTrigger'
}
3
votes

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..."
            }
        }
    }
}
2
votes

You could check the build cause like so:

stage('Build & Tests & Analysis') {
    when {
        expression {
            for (Object currentBuildCause : script.currentBuild.rawBuild.getCauses()) {
                return currentBuildCause.class.getName().contains('TimerTriggerCause')
            }
        }
        steps {
            bat 'msbuild.exe ...'
            bat 'mstest.exe ...'
        }
    }
}

However, this requires the following entries in script-approval.xml:

<approvedSignatures>
  <string>method hudson.model.Run getCauses</string>
  <string>method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild</string>
</approvedSignatures>

This can also be approved via https://YOURJENKINS/scriptApproval/.
Hopefully, this won't be necessary after JENKINS-41272 is fixed.

Until then, a workaround could be to check the hour of day in the when expression (keep in mind that these times refer to to the timezone of Jenkins)

when { expression { return Calendar.instance.get(Calendar.HOUR_OF_DAY) in 0..3 } }
0
votes

I've found a way, which does not use "currentBuild.rawBuild" which is restricted. Begin your pipeline with:

startedByTimer = false
def buildCauses = "${currentBuild.buildCauses}"
if (buildCauses != null) {
    if (buildCauses.contains("Started by timer")) {
        startedByTimer = true
    }
}

Test the boolean where you need it, for example:

stage('Clean') {
   when {
      anyOf {
         environment name: 'clean_build', value: 'Yes'
         expression { (startedByTimer == true) }
     }
  }
  steps {
      echo "Cleaning..."
      ...
0
votes

Thanks to this you can now do this without needing to the the use the non-whitelisted currentBuild.getRawBuild().getCauses() function which can give you Scripts not permitted to use method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild depending on your setup:

@NonCPS
def isJobStartedByTimer() {
    def startedByTimer = false
    try {
        def buildCauses = currentBuild.getBuildCauses()
        for ( buildCause in buildCauses ) {
            if (buildCause != null) {
                def causeDescription = buildCause.shortDescription
                echo "shortDescription: ${causeDescription}"
                if (causeDescription.contains("Started by timer")) {
                    startedByTimer = true
                }
            }
        }
    } catch(theError) {
        echo "Error getting build cause"
    }

    return startedByTimer
}