Adesh Khanna
Adesh's Blog

Follow

Adesh's Blog

Follow
Mastering Github Actions: How to Create a CRON Job in Minutes

Mastering Github Actions: How to Create a CRON Job in Minutes

Adesh Khanna's photo
Adesh Khanna
ยทMar 20, 2023ยท

2 min read

As a developer, you may find yourself performing repetitive tasks regularly. Whether it's backing up your database or sending out weekly reports, it can be time-consuming and tedious to perform these tasks manually. That's where CRON jobs come in handy. CRON jobs are automated tasks that run on a predetermined schedule, saving you time and increasing efficiency. In this article, we will walk you through the process of creating a CRON job using GitHub Actions.

What are Github Actions?

GitHub Actions is a powerful tool that allows developers to automate workflows within their repositories. With GitHub Actions, you can set up tasks to run on a schedule, in response to events like pull requests, issue comments, and more. By leveraging the power of Github Actions, you can automate your development workflow, freeing up time to focus on more important tasks.

Creating a CRON Job in Github Actions

Now that you know what Github Actions are, let's dive into the steps to create a CRON job in Github Actions.

Step 1: Create a new workflow

The first step is to create a new workflow file in your repository. To do this, navigate to the .github/workflows directory in your repository and create a new file. You can name this file anything you like, but for this example, we will name it cron.yml

Step 2: Define the schedule

Next, you need to define the schedule for your CRON job. To do this, add the following code to your cron.yml file:

name: first github action
on:
  schedule:
    - cron: '0 0 * * *'

This code sets the name of the workflow to CRON Job and defines a schedule to run the workflow every day at midnight.

Step 3: Define the job

Now that you have defined the schedule, you need to define the job that will run at the scheduled time. To do this, add the following code to your cron.yml file:

jobs:
  run-job:
    runs-on: ubuntu-latest
    steps:
       - run: echo "Hello, World!"

This code defines a job named "run-cron" that runs on the latest version of Ubuntu. The job contains a single step that runs the command "echo 'Hello, World!'" when the job is triggered.

Step 4: Commit and push changes

Finally, you need to commit and push your changes to Github. Once you have done this, your CRON job will be set up and running on schedule.

Thanks for scrolling ๐ŸŽ‰

Did you find this article valuable?

Support Adesh Khanna by becoming a sponsor. Any amount is appreciated!

See recent sponsors |ย Learn more about Hashnode Sponsors
ย 
Share this