What is GitHub Actions and how to use it...

What is GitHub Actions and how to use it...

Overview

In the world of Devops and SRE, GitHub is one of the most popular buzzword and you’ve most probably came across it on a git repository. In this beginner-friendly article for Devops folks, we are going to have our hands on GitHub Actions, learn what it is and use an action on a workflow.

Before we go any further, we need to define some key terms which are components of GitHub Actions.

Workflows

A workflow is an automated process that is made up of one or more multiple jobs and can be triggered by an event. Workflows are defined and configured in YAML(Yet Another Markup Language) file in the .github/workflows directory of your repository.

Actions

Actions are the building blocks of a workflow, they are automated processes which can combine to create a job.

Jobs

A job is a collection of workflow stages that executes in an orderly way and under the same runner. A job’s dependencies can be configured with other jobs and when a job takes another job’s dependencies, it will wait for the dependent job to complete before it can run… Each step in a job can either be a shell script or an action that will be executed.

Runners

A runner is a server that runs your workflows when they are triggered. Because each runner is limited to one job at a time, it waits for available jobs to execute and then runs the job’s actions and returns the results back to GitHub. Runners can either be hosted on GitHub or Self hosted on your own servers.

Step

A step is a set of tasks that can be executed by a job, it has the ability to run actions.

What are GitHub Actions

compositeAction.png

GitHub Actions is a CI/CD platform that allows you to automate your build, test and deployment pipeline with ease. GitHub Actions gives super-powers to your repo, imagine running a workflow that automatically creates labels whenever someone creates a new issue in your repository or when a pull request is made to your repository, an automated workflow can check if the pull request is eligible for merging into the main code or not. With GitHub Actions, when you code and push your project to GitHub, you can build a script to test every time you commit a change or someone creates a pull request and if any exception occurs, GitHub Actions will fail the build and prevent it from being merged.

You can create your own actions or use and customize actions or shared by the GitHub community. When developing an action for the public, storing an action in its own repository makes it easier for the GitHub community to discover the action and make contributions to it but if you don’t want to make it available to others, you can store the action in any location in your repository.

Types of Actions

There are two main types of publishable actions: JavaScript and Docker actions

Docker actions package an environment with GitHub Actions code as it provides reliable unit work. JavaScript actions are easier to write and faster than docker actions because they run directly on the runner machine and don’t have to worry about rebuilding the docker image at every instance.

Another type of action is Composite Run Steps, which helps you reuse code inside your project workflows and hides complexity when you don’t want to publish your action to the marketplace.

Creating a composite action

In this short tutorial, we will be creating our first GitHub Action together. We will create a composite action which you can use from one repository to another

reusablecode.png

Start by creating a public repo in GitHub and choose any name for the repository

create-repo.png

clone your repo to your computer and from your terminal, change into the cloned directory

cd composite-action-hello

In your repository, create a new file called hello.sh and add the code example:

echo Hello world

From your terminal, make hello.sh executable.

chmod +x hello.sh

From your terminal add your bash file

git add hello.sh
git commit -m “Added hello script”
git push

Let’s create an action metadata file with the details below:

name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
using: "composite"
steps:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "::set-output name=random-id::$(echo $RANDOM)"
shell: bash
- run: echo "${{ github.action_path }}" >> $GITHUB_PATH
shell: bash 
- run: hello.sh
shell: bash

From your terminal check in your action.yaml file

git add action.yaml
git commit -m “Added action file”
git push

From your terminal, add a tag.

git tag -a -m “First release ” v1
git push --follow-tags

Your repository should look like this finalpush.png

It is done 🎉!!

To test this action, create an empty action file in another repository, copy the workflow code into a .github/workflows/main.yml file but replace actions/composite-action-hello@v1 with the new repo and tag you created. From your repository, click Actions tab and select the latest workflow run...

As you can see, we easily created a composite action which sends us our Hello World greeting! Thanks for reading

You can check the repo for here