How to fix ClassCastException: org.jenkinsci.plugins.workflow.job.WorkflowJob cannot be cast to com.cloudbees.hudson.plugins.folder.computed.ComputedFolder

Issue

  • We are using a PeriodicFolderTrigger in our pipeline and are observing the following exception in the Jenkins logs:
2019-12-07 23:46:00.945+0000 [id=198] WARNING hudson.triggers.Trigger#checkTriggers: com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger.run() failed for org.jenkinsci.plugins.workflow.job.WorkflowJob@3099f29a[myPipeline]
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.job.WorkflowJob cannot be cast to com.cloudbees.hudson.plugins.folder.computed.ComputedFolder
at com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger.run(PeriodicFolderTrigger.java:192)
at hudson.triggers.Trigger.checkTriggers(Trigger.java:280)
at hudson.triggers.Trigger$Cron.doRun(Trigger.java:227)
at hudson.triggers.SafeTimerTask.run(SafeTimerTask.java:72)
at jenkins.security.ImpersonatingScheduledExecutorService$1.run(ImpersonatingScheduledExecutorService.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
  • How to fix this issue and properly declare a Periodic Trigger on a Pipeline?

Environment

Resolution

The error you see is due to the usage of a PeriodicFolderTrigger in a Pipeline.
As hinted by the exception, you are trying to use a trigger (PeriodicFolderTrigger) designated for a Computed Folder on a Pipeline job.
A Pipeline job is not a Computed Folder and therefore the exception is raised.

For a Pipeline job, the property you are looking for is the pipelineTriggers taking as argument an array of triggers.
The triggers available are depending on your installation type and as such you probably want to look into the Pipeline Snippet Generator to find the one appropriate for you.
Please refer to Pipeline: Best Practices if you are unsure how to access the Snippet Generator.

As an example, let us suppose you are looking for a Cron trigger to trigger the job every day, then you will use: properties([pipelineTriggers([cron('H H * * *')])]).
For a declarative Pipeline, the same example would be:

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

Refer to Declarative Pipeline: triggers for more details.

References

Have more questions?

0 Comments

Please sign in to leave a comment.