Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Task 03 - Set up a GitHub Actions workflow (20 minutes)

Introduction

Automation is a critical part of an effective DevOps process, incorporating elements like CI/CD but also processes like IssueOps. GitHub Actions provides a platform for GitHub to respond to various event triggers to execute workflows.

Workflows are broken down into jobs that contain the automation steps to complete these jobs. These steps could be a simple CLI command or a pre-defined piece of automation in the form of a GitHub Action from GitHub’s extensive Marketplace.

Jobs can run on Linux, macOS, or Windows. They can run in the cloud on a hosted runner or run on your own machines through a self-hosted runner. We will use hosted runners for this training but your requirements after today may include the need for self-hosting or a mix of both.

Jobs can run sequentially or in parallel. Sequential jobs provide the ability to define your workflow sequences. An example of this is a sequence that includes building an application, running tests against that application, and then deploying the application artifacts to a remote server for hosting. By contrast, parallel jobs allow the ability to scale out sections of your workflow. For example, you could build your application on x64 at the same time as ARM or test the application on Chrome at the same time as Firefox.

There are common workflow patterns for CI/CD processes, but the flexibility of GitHub Actions allows us to respond to an extensive range of GitHub events. This enables many other possibilities, such as the ability to execute a workflow whenever anybody creates an issue in your project.

Description

The IT team at Munson’s Pickles and Preserves would like to see how GitHub Actions workflows operate and what is necessary to create one. The workflow you create will start with a single job triggered by manual event, but then you will expand upon it to include a second job and an additional event trigger.

  1. Create a GitHub workflow in a .github/workflows directory and name it first-workflow.yml. Include a trigger to execute this workflow manually (that is, not triggered by an automated action such as a push into a branch or pull request).
  2. Add a job called job1 to the workflow. The job should include two steps. Each step will be a simple CLI command to echo out the phrases “Step 1 complete!” and “Step 2 complete!” respectively. You may also wish to use GitHub Copilot to assist you with this step and subsequent GitHub Actions workflow-related steps in this task.
  3. Manually trigger the workflow you created. Check the log file for the workflow run and ensure that your job succeeded, emitting the echo statements as expected.
  4. Add a second job to this workflow and call it job2. In this job, add a single step. This step should call a GitHub Action from the GitHub Marketplace. Find the “Cowsays” action in the marketplace and configure this action to emit the text “Ready for prod–ship it!” in magenta font color.

    When you run this job, the log output may print in some other color instead of magenta. GitHub logs are not required to honor font color specifications.

  5. Execute your workflow again, ensuring that both jobs run as expected and that the two jobs run in parallel.
  6. Modify first-workflow.yml to ensure that job2 does not run until job1 completes.
  7. Execute the workflow again to test the sequence.
  8. Add an additional trigger to first-workflow.yml which fires when somebody creates an issue.
  9. Create a new issue and ensure that the workflow ran as expected.

Success Criteria

  • You can execute your first workflow from a manual trigger or upon somebody creating an issue.
  • You can see output from all the steps in the two jobs.
  • The two jobs now run sequentially, with job2 running after job1 completes.
  • job2 successfully runs the “Cowsays” GitHub Action from the marketplace.

Learning Resources

Solution

Expand this section to view the solution

Each GitHub Actions workflow has a certain structure to it, following the GitHub Actions workflow syntax. The following is an annotated breakdown of this structure.

# The name of the job is what will display on the GitHub repository in the Actions tab.
name: First Workflow

# The 'on' section tells GitHub under what conditions we want to run this workflow.
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows
# Common scenarios include:
  # workflow-dispatch (manual execution)
  # issues
  # push
  # pull_request
  # schedule
on:
  workflow_dispatch:
  issues:
    types: [opened]

# This section covers the work to perform.
# We include one or more jobs in this section.
jobs:
  # Each individual job will include details like execution order,
  # pre-requisite jobs, and execution platform.
  job1:
    # We can run jobs on GitHub hosted VM runners in Windows, Ubuntu, and Mac OS.
    # We can also run jobs on self-hosted hardware.
    runs-on: ubuntu-latest

    # Each job contains one or more steps. A step needs to have at least a name and a command.
    steps:
    - name: Step one
      # The 'run' command executes a shell or command script. Because this is Ubuntu, the
      # default run command will be /bin/bash
      run: echo "Log from step one"
      # This section does not appear in the solution file but demonstrates how to set
      # custom variables that will be available in the run script.
      env:
        VARIABLE_NAME: value
    - name: Step two
      run: echo "Log from step two"
      
  job2:
    # Job 2 will only run after job 1 completes.
    # Removing this 'needs' section would make the jobs run simultaneously.
    needs: job1
    runs-on: ubuntu-latest

    steps:
    - name: Cowsays
      # The 'uses' command executes a remote GitHub action.
      # A command like mscoutermarsh/cowsays-action means you can
      # find this code at https://github.com/mscoutermarsh/cowsays-action
      uses: mscoutermarsh/cowsays-action@master
      # The 'with' block includes parameters that the workflow will pass
      # to this action. Parameters are all in key-value format.
      with:
        text: 'Ready for prod--ship it!'
        color: 'magenta'

Advanced Challenges (optional)

In the event that you have completed Exercise 02 before the alloted time, please try the following challenges.

  1. In your workflow file, define an environment variable for your workflow or job. Pass the contents of the environment variable into one of your echo steps in job1.
  2. In your workflow file, pass the value from an environment variable into your “Cowsays” action in job2.