darrensunley
darrensunley

Reputation: 49

Cron trigger only kicks in after first manual run of pipeline

I've got a bunch of JCASC jobs that work as desired and everything seems good... other than the fact that in the job I've got something like triggers{ cron('H/2 * * * *') } which works fine BUT only after the first manual run (i.e. when it seems to "create" the cron entry, I'm guessing).

If I update the code (or for that matter any of the jobs in our JCASC repo), then push it, then run the seed job to create the pipelines, they sit there dormant until I run it manually... then afterwards it runs as desired every 2 mins.

Is there any way to set a flag that means I don't have to do the first manual run?

Upvotes: 0

Views: 59

Answers (1)

D4LI3N
D4LI3N

Reputation: 394

Issue with Cron Triggers in JCASC Jobs

It sounds like your cron triggers only kick in after a manual run. This happens because Jenkins doesn't start the cron schedule until the job runs at least once.


Workarounds: (3 examples)

1. Use a Groovy script in the Script Console to trigger jobs right after creation.

import jenkins.model.*
import hudson.model.*

def jobNames = ["yourJobName1", "yourJobName2"]
jobNames.each { jobName ->
    def job = Jenkins.instance.getItem(jobName)
    if (job) {
        job.scheduleBuild2(0)
        println "Triggered job: ${jobName}"
    }
}

2. If you're using Job DSL, add a step to automatically trigger the job after it's created.

job('example-job') {
    triggers {
        cron('H/2 * * * *')
        // Trigger the job after creation
        scm('H/2 * * * *')  // Poll SCM as an alternative
    }
}

3. You could also try to add a polling trigger to your job configuration, which checks for changes in your source control repository and triggers the job accordingly. Here’s how you might configure it in a declarative pipeline:

pipeline {
    agent any
    triggers {
        pollSCM('H/2 * * * *') // Polling every 2 minutes
    }
    stages {
        stage('Build') {
            steps {
                // Your build steps here
            }
        }
    }
}

Note:

If you’re using other specific plugins or have more details, feel free to share. It could help refine the solution!

Upvotes: 0

Related Questions