avatarOliver S

Summary

The provided content introduces GitHub Actions as a CI/CD platform for automating the build, test, and deployment pipeline of software projects.

Abstract

GitHub Actions is a powerful automation tool that enables software development teams to streamline their build, test, and deployment processes. As described in the content, Actions allows for the creation of workflows that are triggered by specific events, such as pull requests or pushes to the main branch. These workflows consist of jobs that run on virtual machines called runners, and they can include a series of steps involving bash scripts or pre-written actions. The article emphasizes the importance of GitHub Actions in professional software development, particularly for automating testing and safeguarding the integrity of the main branch. A sample workflow is provided to illustrate the setup of a Python environment and the execution of simple bash commands, demonstrating the practical application of GitHub Actions in a real-world scenario. The content also guides readers on how to trigger workflows manually and interpret the results within the GitHub interface.

Opinions

  • GitHub Actions is considered essential for modern software projects, as it automates repetitive yet crucial tasks.
  • The use of GitHub Actions is associated with industry best practices, particularly for automating testing to ensure code quality.
  • The article suggests that GitHub Actions can enhance the professionalism of software projects by providing a standardized platform for CI/CD.
  • The author implies that understanding GitHub Actions is a valuable skill for developers, as it is a commonly used tool in the industry.
  • By providing a sample repository, the author encourages readers to explore and apply GitHub Actions in their own projects.
  • The author expresses the utility of GitHub Actions in unblocking continuous integration pipelines that may have encountered issues.

Introduction to Github Actions

Automate your build and test pipeline

Github Actions, to quote the official documentation, “is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline”. In other words, it is a powerful tool to automatise repetitive and useful tasks for your repository, such as unit testing, formatting checks, and more. Basically any professional software project will employ this platform or a similar one, most commonly to automatise testing and protecting the main branch against wrong, untested code pushes.

In this post, we’ll introduce Github Actions, and in the next ones, build onto this to show how to create Python and C++ repositories following professional industry practises.

How do Github Actions Work?

Let’s use an image from the official docs for visualisation:

https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions

The overall concept not shown in this diagram is a workflow: this contains a list of jobs to run. It is triggered by an event, e.g. a pull request (PR) to the main branch. Each job of the workflow is then run in its own virtual machine, in a runner. A job can have several steps, each are either bash scripts, or actions. An action is a complex task which is used frequently, and which was written by Github or someone else (such as checking out a repository). If any of the scripts, actions, … fail, it will fail the full workflow (and usually prevent merging the PR to main).

Sample Workflow

Let’s dive into a sample workflow to apply these concepts in practice, and demonstrate core concepts and syntax. Workflows must be placed in the .github/workflows folder in your repository. In this, let’s create a sample_workflow.yml file.

We first define the name of the workflow:

name: Sample Workflow

Then, we define two events for which this workflow is run:

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]

Here, we are saying to start this workflow for all PRs pointing to main, and any pushes to main — i.e. when this workflow is merged eventually.

Next, we define one job sample_job in this workflow:

jobs:
  sample_job:
    runs-on: ubuntu-20.04

The attribute runs-on describes the platform to run this workflow on in the virtual machine, here we pick Ubuntu 20.

Then, we define several steps inside that job. The first will checkout the current state of the repository in the runner:

steps:
  - name: Checkout repo
    uses: actions/checkout@v3

Here we see a first example of an action, namely checkout@v3, which checks out our repository for us.

Following this, we use another action to setup Python:

- name: Set up Python 3.10.0
  uses: actions/setup-python@v3
  with:
    python-version: "3.10.0"

We observe that via the with keyword we specify additional attributes, in our case the right Python version.

To conclude, we add two steps show-casing the usage of bash scripts, printing a few lines to the terminal:

- name: Echo 1
  run: echo "Echo 1"

- name: Echo 2
  run: |
    echo "Echo 2a"
    echo "Echo 2b"

The pipe element in a step (|) denotes multi-line commands, and lets us run several commands over multiple lines in one step.

That defines your first workflow!

For completeness here is the full file:

name: Sample Workflow

on:
  pull_request:
    branches: [main]
  push:
    branches: [main]
jobs:
  sample_job:
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
      - name: Set up Python 3.10.0
        uses: actions/setup-python@v3
        with:
          python-version: "3.10.0"
      - name: Echo 1
        run: echo "Echo 1"
      - name: Echo 2
        run: |
          echo "Echo 2a"
          echo "Echo 2b"

You can also find this workflow in this sample repository.

Using our Workflow

There are two ways this workflow is run / we can trigger it to run: the first one is the one we designed in the workflow itself, namely raising a PR to main or merging a commit to main.

If you raise a PR, Github will automatically run our workflow, and notify you of the result. If you go to the PR, you will find a section “Checks”, looking something like this:

This shows us, that our workflow / check has passed successfully.

When clicking on the workflow, we see additional details, in particular outputs of every step — and we see that all our steps were run as intended, and e.g. we get the expected prints:

To manually trigger an action, select “Actions” from the main menu of your repository:

In the resulting view you will find an overview of ran workflows in the past — and when clicking on a desired workflow, you can re-run it. This is e.g. useful to unblock a broken / stuck CI (maybe there was an issue, a runner died, or similar, …) — or if you just want to run and test a workflow yourself.

This concludes this introduction about Github Actions. I hope this post provided some useful information for your own projects — stay tuned for Part 2, where we show how to setup a Python repository from scratch up to “industry standards”!

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

Github Actions
Github
Software Development
Continuous Integration
Recommended from ReadMedium