In the tech world that we live in today, automation is really a hot topic because everyone needs their jobs done automatically just to have sufficient time at hand. And As a programmer, automating scripts would defiantly come in handy sooner or later.
I have been programming for a few years now but learning scripts automation hasn't been a priority because how I learn and work is way different. here's what I do;
I don't learn technology in programming because it must be learned. Instead, I learn when needed. That is, I devote my time to learning things when I need to use them while building. This is why I haven't touched automation until last week.
How did it start?
Referring to my last blog post, I talked about a web3 project I'm working on and how I am beginning to discover new technologies on the fly. Good thing right? yea, because that's how it should be.
Although, It's now beginning to look like I notice more bugs as soon as I deploy the project. Sometime last week after I deployed, I hoped that three different functions I wrote would run automatically after deployment, but to my surprise, none of them ran. I began to ponder what would be the reason. At a time, I thought my functions weren't well written. But again, I remembered everything ran successfully in development. I later figured I would need to have the functions called from a script ( automation).
I found different ways to automate my functions which include Github Actions. But hold on! GitHub actions? that was exactly how I reacted while reading about it for the first time. I never knew GitHub has that type of functionality before now.
I had a few other options but I chose GitHub action simply because my project is deployed on GitHub already which I believed would make it work seamlessly. Never did I know Github actions wouldn't be the best option for my use case.
My Use-case
While reading on the use cases of cronjobs, the examples given were to schedule a system backup task, send emails to clients, and so on. but for my use case, I needed a scheduling technology that will run my functions once daily and without delays. Just for trials, I began setting up my actions.
GitHub action is really easy to set up. I created a *.github\workflows* inside my root directory. the workflows is where the action/scripts will go in. Each workflow file has a .yaml extension. For example, creating an action with the title start will look like this; start.yaml.
The code below is an example of a start script.
name: 10 mintues start cron job
on:
schedule:
- cron: "*/10 * * * *"
jobs:
cron:
runs-on: ubuntu-latest
steps:
- name: 10 mintues start cron job
run: |
curl --request POST \
--url 'https://example.come/api/start' \
--header 'Authorization: ${{ secrets.key }}'
Code Details
Name: Takes in the name of the cron job. This will be visible on the github repository.
On: Takes in the time this action will execute. */10 * * * * means this script will always run every 10mins.
on:
schedule:
- cron: "*/10 * * * *"
- URL is an API route to where the start function is located.
- header/authorization: is meant to protect the script from being called. it checks if the caller has the secret key.
curl --request POST \
--url 'https://example.come/api/start' \
--header 'Authorization: ${{ secrets.key }}'
That's all on the script side. One thing left to do is to provide Github with the secret key. Now, head to > repo settings > secretes>actions, then add the secrete as environment variable.
And that's all! After the script is pushed to GitHub, the start function executes every 10mins.
The Issue
A few times, I noticed that the start script doesn’t execute at the exact time it was scheduled. And some other times, it doesn't even run. This is why GitHub action is a bad option for me. While I checked online I found this response;
As I pen this, I'm yet to get a better option to automate my scripts. Help if you know any.