Github Actions

Image souce : https://www.google.com/url?sa=i&url=https%3A%2F%2Fdev.to%2Fcchacin%2Fmaven-build-in-20-lines-of-yaml-2bg9&psig=AOvVaw2IvMuSo4aw63jaXmPqoVJt&ust=1644657722376000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCMDyq8yp9_UCFQAAAAAdAAAAABAN

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

Response of the GET request

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.

Click on the Actions tab

There are various workflow templates available already, but we will create our own

Click on setup a workflow yourself
The default workflow yml file

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 :

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

Run a workflow manually using workflow_dispatch

On visiting the Actions tab now, all the listed workflows show up.

Existing workflows
If the workflow_dispatch event is mentioned, this shows

The workflow can be triggered using Run workflow

Successful dispatch of the workflow
Result of the workflow run

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 :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store