GitHub Actions Overview
Introduction
Over the recent years, Continuous Integration and Delivery (CI/CD) has undergone notable transformations, with a particular emphasis on the automation of cloud infrastructure provisioning. This facet is widely acknowledged as an imperative undertaking for the majority of technology-focused enterprises, given its substantial impact on business continuity.
This automation framework plays a pivotal role in streamlining the processes involved in cloud infrastructure provisioning, software building, testing, and deployment. It is imperative that we grasp the nuances of establishing CI/CD pipelines, as they empower us to execute tests at increased frequencies and with diminished manual intervention, thereby yielding significant efficiency gains.
In this blog, we are going to explore one well-known CI/CD platform (GitHub Actions). CI/CD tools like Jenkins are available in the market, which are used by automation testers but these days, most people are using GitHub for version control. So, GitHub actions are becoming the favorite choice for automation testers/developers for CI/CD.
What are GitHub Actions?
GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) platform provided by GitHub that allows developers to automate tasks based on events within a repository. These tasks are defined as workflows. These workflows can help you build, test, deploy, and manage your code more efficiently, making your development process faster, more reliable, and more collaborative.
Why GitHub Actions?
-
Automation: it automates repetitive tasks in your development process.
-
Integration: GitHub supports connecting and utilizing various third-party tools.
-
Flexibility: GitHub Actions can be used to automate a wide range of tasks, from simple build and test processes to complex, multi-stage deployment pipelines (Customize workflows to suit your needs).
-
Community Contributions: The marketplace for GitHub Actions contains a huge collection of pre-built actions that you can use in your workflows.
The Main Components of GitHub Actions
You can set up a GitHub Actions workflow to be triggered whenever something happens in your repository. One or more jobs in your workflow can run in parallel or sequentially. Each job contains one or more stages that execute a script that you write or an action.
-
Workflows: A workflow is a configurable automated process defined in a YAML file that will run one or more jobs. They are triggered automatically by an event in your repository, or they can be triggered manually, or at a defined schedule. GitHub repository can have multiple workflows.
-
Events: An event is an activity in a repository that triggers a workflow to run. Events can be triggered by pushes to the repository, pull requests, issue comments, on a schedule, or triggered manually. You can define the conditions that trigger your workflows.
The following workflow will run on pull_request events for pull requests that target the main branch:
name: your project name
on:
pull_request:
push:
branches:
- "main"
-
Runners: A runner is a server that runs your workflows when triggered. Each runner can run a single job at a time. GitHub provides Ubuntu Linux, Microsoft Windows, and macOS runners to run your workflows. If you need a different operating system or require a specific hardware configuration, you can also host your self-hosted runners.
This is an example of a job that runs on Ubuntu OS:
jobs:
build:
runs-on: ubuntu-latest
This is an example of a job that runs on a self-hosted runner:
jobs:
build:
runs-on: self-hosted
-
Jobs: A job is a set of steps in a workflow that is executed on the same runner. Each workflow consists of one or more jobs. These jobs run in parallel by default and can be defined to run on different platforms or environments.
This is an example of a job to build a Docker image:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Build Docker image
uses: docker/build-push-action@v5
with:
tags: user/app:latest
-
Steps: A step is either a shell script that will be executed or an action that will be run. Steps are executed in order and are dependent on each other. Since each step is executed on the same runner, you can share data from one step to another.
This is an example of a step toward installing independence:
steps:
# As you see you can use actions such as Checkout and Setup-Python.
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
# OR you can run a code that will be executed to execute a specific function.
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
-
Actions: An action is a custom application for the GitHub Actions platform that performs a complex but frequently repeated task that automates the workflow process. Use an action to help reduce the amount of repetitive code that you write in your workflow files. You can write your own actions or find actions to use in your workflows in the GitHub Marketplace.
This is an example of one of the most used actions on Marketplace:
steps:
# This action checks-out your repository
- uses: actions/checkout@v4
Pricing Model
Each GitHub account receives some free minutes and storage for use with GitHub-hosted runners, depending on the account's plan. If you want to use GitHub Actions beyond the storage or minutes included in your account, you will be billed for additional usage.
-
Free: 2000 minutes/month & 500 MB artifact storage
-
Pro: 3000 minutes/month & 1 GB
-
Team: 3000 minutes/month & 2 GB
-
Enterprise: 50000 minutes/month & 50 GB
For more details about billing for GitHub Actions, check this documentation out.
Summary
GitHub Actions is a powerful automation tool that can simplify your software development workflow. It enables you to automate a wide range of tasks, from building and testing code to deploying applications, and it can be customized to meet your specific needs.
By understanding the basics of GitHub Actions and exploring real-world examples, you can leverage this technology to improve your development process, increase efficiency, and ensure code quality. Start automating your workflows with GitHub Actions today and see how it transforms your development experience.
About the Author
Mohammed Hassan - Cloud Consultant at Cloud Softway