In this blog, I will cover what a Github Action is and how to set up a basic Github Action.
What
As defined in the documentation, GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository or deploy merged pull requests to production.
The way I understand it, there can be many events like pushing code to the repository, pull request created from another branch, a new issue created by a contributor, a scheduled time of the day, etc. On the occurrence of such events, a certain action can be written. An action is a set of rules or instructions that need to be followed when any of these events occur.
The documentation is amazing, very beginner-friendly!
Definitely give this a read before looking at other resources: https://docs.github.com/en/actions
How
Setting up an action essentially consists of writing a yml
file stating the steps to be performed. I would recommend going through the basics of yml
and the syntax used.
Some resources I used :
https://www.youtube.com/watch?v=1uFVr15xDGg
https://circleci.com/blog/what-is-yaml-a-beginner-s-guide/
Example
To understand in detail, let’s consider that we are building an application to log hourly temperature data. Without any automation, this could be a dreadful task, running the same script on an hourly basis, phew 😅
One of the solutions to this could be to run a Github action, at the start of every hour, and write the latest recorded temperature to a file in your Github repository, and this is exactly what we will look at.
The code used, is on Github. Find it at: https://github.com/Namyalg/test-github-actions/
Step 1
Have a Github account and create an empty Github repository.
Step 2
Write a script to get the temperature of a place. I have used the https://weatherdbi.herokuapp.com/
API. Which does not require any authorization or API key.
A simple get request to the following endpoint is made.https://weatherdbi.herokuapp.com/data/weather/{location}
For a location Bangalore, the GET request looks like, https://weatherdbi.herokuapp.com/data/weather/bangalore
The following response is obtained
The temperature can be obtained by indexing into currentConditions
, and then temp
. The temperature in celsius or the key c
will be written into the file.
Step 3
Create an action on Github.
There are various workflow templates available already, but we will create our own
This is the default workflow, the file created, the contents can be replaced by the contents of the main.yml present in my repository. The file has comments and resource references where needed.
Other blogs/references :
- https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
- https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows
- https://www.youtube.com/watch?v=TLB5MY9BBa4
One important way by which the action can be debugged and run manually is using the workflow_dispatch
event in the on
input
One important thing to mention is that the time is based on the UTC or Coordinated Universal Time
On visiting the Actions tab now, all the listed workflows show up.
The workflow can be triggered using Run workflow
On each workflow run, the steps that were run are mentioned, along with the success or failure of the entire workflow. Clicking on each gives detailed logs of the steps executed.
This workflow is triggered to run at the start of every hour or can manually be run and the timestamp along with the temperature is recorded
Hope this gave a basic idea about Github actions and their usage, see you till next time :)