Automated Processes in GitHub

3 min read

In this article I'll show you how to automate processes with GitHub actions and save time and money.

What are GitHub Actions?

Actions are a GitHub tool that facilitates the automation of all software workflow processes. What does this mean? You can automate practically anything, like build processes, unit and integration test execution. To start using GitHub actions you just need to create a folder in your repository called .github and inside that folder another folder called workflows. This folder will contain all the actions or workflows you create, resulting in the following structure:

.
├── .github
│ └── workflows
└── my_script.py
└── .gitignore
└── LICENSE
└── README.md

Inside the .github/workflows directory we're going to create a file called hello.yml. Inside this file we're going to copy this code:

name: My first action

# Run this workflow every time a new commit is pushed to the repository
on: push

jobs:
  # Set the job key. This key is displayed as the
  # job name when the job name is not provided
  first-job:
    name: My First Test
    # Configure the machine type to run on
    runs-on: ubuntu-latest
    steps:
      # Run a Hello world
      - name: Print a message
        run: echo Hello world

All the code shown here can be found on my GitHub.

Actions Structure

Each workflow is composed of jobs and each job is composed of steps. Now we'll see what each thing is.

First we have name which specifies the name that will be displayed for the action you created on GitHub. If we go to our repository and click where it says Actions we'll see the following:

Next comes on: push which specifies when the workflow will be executed. Here there are several options. You can specify a specific event (push, pull) and you can specify one or more branches, tags, etc. More info here.

After that come the jobs which are the jobs that this workflow will contain. In our case we only have one job called first-job. Next we define the job name My First Test and tell it which virtual machine we want this job to run on. In our case we want it to run on the latest version of Ubuntu. If we go inside the commit that executed the action we'll see the following:

In steps the steps that this job will perform are defined. In our example we only have one step called print message that will only display text on screen. To see it we go inside the job. Once inside we can see each step performed and expand to see its output:

Conclusions

As we can see, GitHub actions are a powerful tool to automate processes. One of its main uses is CI/CD (continuous integration and continuous delivery) where you can create automatic releases of your software. In the case of GitLab these are called GitLab Runners and have a very similar syntax. I hope this has been helpful and you can take your first steps towards cleaner and more agile development. For more information you can visit the GitHub documentation.