jenkins|October 07, 2022|2 min read

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

TL;DR

Use triggers { cron('H 8 * * *') } in your Jenkinsfile for scheduled runs, and add options { skipDefaultCheckout() } with a when condition to prevent the job from triggering on every SCM commit.

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction

In this post we will see following:

  • How to schedule a job on cron schedule
  • How not to run job on code commit

How to Schedule Job on Cron

Jenkin pipelines are very powerful and flexible, they provide lot of options for developers. One such option is triggers.

Complete Jekninsfile:

pipeline {
    agent {
        node {
            label "MyJenkinNode"
        }
    }
    triggers {
        //every 24 hours at 12 AM
        cron('0 0 * * *')
    }
    stages {
        stage('Install requirements') {
            steps {
                    ...
                }
            } 
        }
    }
}

Above file triggers job every 24 hours, 12 AM every day.

Problem - Why We Don’t Want to Run On Code-Commit

The nature of my job is to run only on cron schedule specified. Problem will be, even if I’m changing something in any file, lets say in README or any python file, the job will be triggered.

I do not want the job to run on any code commit.

How Not to Run Job on Code Commit

The idea is to use changeset pattern with a regex pattern.

Example:

when {
    not { changeset pattern: ".*", comparator: "REGEXP" }
}

Complete file:

pipeline {
    agent {
        node {
            label "MyJenkinNode"
        }
    }
    triggers {
        //every 24 hours at 12 AM
        cron('0 0 * * *')
    }
    stages {
        stage('Install requirements') {
            when {
                not { changeset pattern: ".*", comparator: "REGEXP" }
            }
            steps {
                    ...
                }
            } 
        }
    }
}

If you want job not to run on some specific file changes, you can provide that regex pattern.

Related Posts

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…