A Student's Guide to Software Engineering Tools & Techniques »

GitHub Actions

Author: Alfred Yip

Reviewers: James Pang, Tan Yuanhong, Tejas Bhuwania, Daryl Tan

What is GitHub Actions

GitHub Actions is a workflow automation solution that is tightly integrated into the GitHub ecosystem. It is commonly used to provide end-to-end continuous integration and deployment (CI/CD) directly in a repository. Beyond that, users also have the potential to automate more than just CI/CD with GitHub Actions through GitHub's webhooks API, including workflows such as labelling issues and tracking bundle size.


Why GitHub Actions

1. Easy to Use

Setting up a workflow in a repository is easy and hassle-free. Starter workflows for popular languages are available and the built-in editor offers code completion and documentation at a glance.

2. Reusable Actions

Actions are small, reusable units in a workflow. When setting up a workflow, we can reuse actions written by the community. These actions are available at the marketplace.

3. Cross OS Support

GitHub Actions gives you the option to run workflows on Ubuntu, Windows and macOS.

4. Docker Support

GitHub Actions provides the ability to run docker images for projects that use a dockerized environment.

5. Powerful

Integration with the GitHub ecosystem allows GitHub Actions to do more than just build/test/deploy. You can automate everything related to your GitHub project, including bug triaging, labeling, automated dependency updating etc. Workflows can be triggered by events that happen on GitHub and will have access to the context of the issue/PR/commit that triggered it.

6. Affordable

Like many of its competitors, GitHub Actions provides free unlimited runner time for public repositories.

For private repositories, the runner time you get with a free plan is limited, but still ample for hobby projects.


Concepts

  • Workflow A workflow describes a sequence of jobs to be run when a trigger event occurs. Each job consists of a series of steps, which may be actions or shell commands. Each workflow is defined as a .yml file in the .github/workflows directory of a repository.

  • Action An action is a piece of code that performs an atomic task. For example, checking out a repository, or caching dependencies.

  • Triggers Workflows can be triggered by events on the GitHub repository, ranging from opening pull requests or issues, making commits to scheduled events, or even from an external event by calling GitHub's REST API endpoint.


Set Up a Simple Workflow for a Node.js App

GitHub has made it extremely easy to set up a simple workflow without even leaving the browser.

  1. Head to the Actions tab on your repository
  2. You can choose to set up a workflow from a template or from scratch. We will use the Node.js template in this example
  3. The online editor is powerful and easy to use, boasting features such as inline documentation and autocomplete
  4. The template workflow will
    1. Begin when a commit is pushed or a pull request is opened to master
    2. Checkout the current repo (using actions/checkout)
    3. Install dependencies npm ci
    4. Build npm run build --if-present
    5. Run tests npm run test
    6. Report the status of the workflow via a status badge
  5. Since we set the workflow up on the master branch, when we commit the newly-created workflow file, we can see GitHub Actions in action by navigating to the Actions tab

Where to Go From Here

The official documentation is a good resource to learn more about GitHub Actions. The starter workflows repository contains good examples of CI workflows for popular languages as well as automation workflows for GitHub. Here is a list of awesome actions that you can use when writing your workflow.

A powerful extension to GitHub Actions is to use the github-script action to run JavaScript within the workflow using GitHub's Octokit API. The Octokit API allows you to create workflows that span across GitHub's ecosystem of services.

Here is a compiled list of popular repositories that use GitHub Actions that you can use as a reference. Do note that this is far from a comprehensive list and is more of a sample of how organizations are leveraging GitHub Actions in their development and production workflow.

Examples of different ways GitHub Actions can be used