Search code examples
jenkinsgroovyjenkins-pipelinepipeline

How i can trigger build from jenkinsfile using cron syntax?


im using multibranch jenkins style each branch has its own jenkinsfile, i have added triggers in jenkinsfile but it didnt trigger anything on the specified time (8pm), im not sure if im missing something

 agent {
    node {
      label 'master'
    }
  }

  triggers {
           cron(env.APP_NAME == 'DICTIONARY' ? '00 20 * * *' : '')
  }

  stages {
    stage('SCM Checkout') {
      steps {
        git(branch: 'test', url: 'https://gitlab.testral.ba/amramework.git', poll: true, credentialsId: 'GitlabCred')
      }
    }

Solution

  • For the change in cron to catch, you need to run your Jenkinsfile once manually in the correct branch. After that, check in "View Configuration" that your cron succeeded ("Build periodically" should be checked and contain the schedule).

    If it has not, it could be that, at the time when triggers are evaluated, your env.APP_NAME differs from 'DICTIONARY'.

    To debug the env, you may add the following:

    println "env.APP_NAME is ${env.APP_NAME}" // will run before pipeline
    
    pipeline { 
     agent {
        node {
    

    As a side-note, it's recommended using H instead of minutes, so not all hourly builds fall exactly on the hour:

    triggers {
               cron(env.APP_NAME == 'DICTIONARY' ? 'H 20 * * *' : '')
      }