Scheduling your GitHub Actions cron-style
Scheduling tasks can be a handy way to automate work, and Cron Jobs is the way to do it. We all have that one recurring task we wish could be done automatically without manual intervention. Not to forget, "That is also for FREE".
Cron jobs are meant for performing regularly scheduled actions such as backups, report generation, and so on. Each of those tasks should be configured to recur indefinitely (for example: once a day/week/month); you can define the point in time within that interval when the job should start.
How to set up a Cron job?
There are a lot of ways to set up Cron Jobs
At the OS level
- You can use the inbuilt Windows scheduler
- If you are on Ubuntu, you can use the Cron Utility
But most of the time, you would want to keep it away from OS, and closer to your application for that, we can use
At the Application level
- use packages like Cron to create a cron job in your NodeJS application, but this cannot run on a serverless stack
- use a serverless solution like AWS Lambda, but it requires a lot of setup and credit card
- use Firebase cloud functions with scheduling, it is a lot simpler than AWS but requires a credit card.
- use a SAAS tool like EasyCron
Our Winner (GitHub actions)
While all of the above are good solutions, the simplest way to do this for hobby projects would be to use GitHub actions because it
- is easy to set up through code and coupled with the application code itself
- is free for public repos
- provides up to 4000 minutes/month for private repos (extremely hard to consume)
- allows us to create unlimited jobs and schedules
How do GitHub Actions help?
GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want. You can read more about them here.
Now to set up your own Cron job using GitHub actions,
-
Go to your GitHub repository (create one if required).
-
Create file yml: .github/workflows/cron_job.yml
-
Add your Cron schedule and task to it. Commit the change.
name: Cron Job on: # Triggers the workflow every 5 minutes schedule: - cron: "*/5 * * * *" jobs: # This workflow contains a single job called "cron" cron: runs-on: ubuntu-latest # Steps represent a sequence of tasks that will be executed as part of the job steps: - name: Run a one-line script run: echo "Hello, world!"
This will run a job to echo "Hello, world!" every 5 minutes.
Go to the Actions tab and wait for the log to show up. When you click on any execution you'll see details like this
That is how you can schedule a Cron job using GitHub actions.
Note: The scheduled event can be delayed during periods of high loads of GitHub Actions workflow runs. High load times include the start of every hour. To decrease the chance of delay, schedule your workflow to run at a different time of the hour.
See how the job scheduled to run every 5 minutes was delayed a couple of times. Please be mindful of how you use the free resources.
The last and most important thing is that you need to wait patiently. Because GitHub action only allows schedule to run at least once every 5 minutes