krookshankey
krookshankey

Reputation: 11

Jenkins - Can you trigger a stage/step after working hours and only once (not everyday)?

I have a pretty large repository and need to include static analysis, since it takes far too long (about 4 hours) for a normal working day we have a cron set to trigger the build automatically every morning purely to perform the static analysis stage and the way we have it set up it only does the static analysis if there has been a change in the code otherwise the static analysis becomes redundant.

The problem lies with the cron, because it is set every day we are now losing build history as well as artifacts.

I've tried to conditionally set the cron but that didn't seem to work since it should have triggered the build this morning and didn't.

triggers {
    cron ( checkBuildStatus() )
}

def checkBuildStatus(){
    if (currentBuild.changeSets.size() > 0){
        return '0 4 * * 1-5 ' 
    }
    else  {
        return ''
    }
}

Even if the above implementation worked I'm still not convinced that it would solve my problem of "empty" builds.

Is there a way to trigger a stage/step after working hours and only once (not everyday)?

Any advice will be greatly appreciated.

Upvotes: 0

Views: 993

Answers (2)

krookshankey
krookshankey

Reputation: 11

I decided to change my approach and found the best solution. I created a method, it first checks if there has been a change in the code between the current build and the one before it, and also checks that the branch name is 'master'. I then went ahead and checked the day of the week and set the cron to the following day (e.g if the build is on a Monday the cron will be set to Tuesday morning where it will then perform the static analysis stage). Now the cron will only be set if there has been a change in code and always only for the next day (of course until the next week but I'm hoping there will be changes on the master branch at least every week therefore resetting the cron)

def setCronTrigger(){
    if (currentBuild.changeSets.size() > 0 && BRANCH_NAME.equals('master')){
        Calendar calendar = Calendar.getInstance()
        def day = calendar.get(Calendar.DAY_OF_WEEK)
        cron = '* 4 * * ' + day
        return cron
    }
    else {
        return ''
    }
}

Upvotes: 1

max.ivanch
max.ivanch

Reputation: 349

well we can try to solve that problem in another way.

  1. let's create one more job, e.g. cronTrigger. That jobs only triggers external job with static analysis.
if (currentBuild.changeSets.size() > 0) {
    build(job: externalJobName, parameters: []) // run another job
}
  1. You can keep more build. That possible to changes as well

Upvotes: 0

Related Questions