How To Build Your First GitHub Actions Workflow

How To Build Your First GitHub Actions Workflow

Posted on March 20, 2020 0 Comments

In this tutorial, you’re going to learn how to get started with GitHub Actions by creating an Actions workflow to invoke tasks in a job.

If you’re in the DevOps space, you’ve undoubtedly heard the term continuous integration and continuous deployment/delivery (CI/CD). CI/CD is at the heart of all successful software build and release strategies. One service that provides end-to-end automation to create a CI/CD pipeline is GitHub Actions.

GitHub Actions (Actions), as you might expect, is a service built into the popular GitHub software development platform. Actions is a service that automates many common processes to build, release, and deploy software in the form of a workflow.

 

Basic GitHub Actions Terminology

Actions are all built around the concept of a workflow. A workflow is a set of steps that automatically run based on a given trigger. A trigger can be when a commit is made to the GitHub repository (repo), a pull request is issued, a branch changes, and more. For more information, refer to Events that trigger workflows.

A workflow file contains all of the instructions that dictate what happens when the workflow runs. You can think of the workflow file as a workflow. The workflow file is a YAML file based on a defined schema.

A step is where the rubber meets the road. The step is what actually performs the necessary tasks. Steps run scripts, checkout code or run actions among others. Steps are executed on runners, which is the compute resources running Windows or Linux.

An action is a set of defined tasks that perform some specific function. Actions are self-contained units a step can call upon. An action could be running a Git command, create a Docker container image, deploying Python code and more.

Creating Your First GitHub Actions Workflow

Now that you have a basic understanding of the core components of a workflow, let’s get started building one. Assuming you have already created a GitHub repo, create a directory called .github/workflows in the root of the repo.

Next, create a workflow file in the workflows directory with a name of your choice. This tutorial will use actions-demo.yml. Inside of the actions-demo.yml file, add a few common components. For this tutorial, you will use:

  • A trigger that runs the workflow when code is pushed to the repo.
    on: [push]
  • A job with an id of build and a name of Greeting that runs on the latest version of Ubuntu available.
    jobs:

        build:

          name: Greeting

          # This job runs on Linux

          runs-on: ubuntu-latest
  • A step that checks out all of the code in the repo which downloads it to the runner (Ubuntu)
    - uses: actions/checkout@v2
  • A step named script run that runs a Bash script
    - name: script run

          run: |

            echo Add other actions to build,

            echo test, and deploy your project.

The following YAML snippet is what the entire workflow looks like.

on: [push]

jobs:
  build:
    name: Greeting
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Run a multi-line script
      run: |
        echo Add other actions to build,
        echo test, and deploy your project.

Save the actions-demo.yml workflow file.

Testing the Workflow

Once the YAML workflow file has been committed, let’s test it out. Since the current workflow defines the trigger to execute when code is pushed (on [push]), push a change to any file in the repo.

When code is pushed to the repo, navigate to the GitHub repo and click on the Actions button. At the top of the report, you will see your commit message which invoked the workflow. Depending on how fast you checked this page, the workflow may be the Queued state as you can see below.

github-workflows-1

In a few minutes, it should complete. When it does, click on the commit message. In the following screenshot, you can see the job name on the left and everything completed successfully.

github-workflows-2

Click on the job (Greeting) and you will see the output for each step inside of that job. In the following screenshot, you can see the output the Bash script created.

github-workflows-3

Congratulations! You’ve successfully created and ran your first GitHub Actions workflow!

Summary

In this tutorial, you learned how to get started with GitHub Actions. After first understanding some of the common terminology, you then took action and created a workflow.

You saw that by creating a YAML file in a specific directory in your GitHub repo, you can automatically kick off a series of automation steps.

This article just scratched the surface on what’s possible with GitHub Actions. Dig in and see what else you can do with Actions!

Adam Bertram

Adam Bertram is a 25+ year IT veteran and an experienced online business professional. He’s a successful blogger, consultant, 6x Microsoft MVP, trainer, published author and freelance writer for dozens of publications. For how-to tech tutorials, catch up with Adam at adamtheautomator.com, connect on LinkedIn or follow him on X at @adbertram.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation