Skip to the content.

Lab: Learn GitHub Actions

Introduction

In this lab, you will create a GitHub Actions workflow that checks the installed version of Bats (Bash Automated Testing System) on a virtual environment. This lab introduces you to using shell commands and third-party tools inside workflows.

Estimated Duration: 15–20 minutes


Instructions

Step 1: Set up your repository

  1. Navigate to the GitHub repository where you want to create the workflow.

    • If you don’t have a repository, create a new one on GitHub.

Step 2: Create the workflow file

  1. In your repository, click the Code tab (if not already there).

  2. Navigate to the .github folder. If it doesn’t exist, create it:

    • Click Add file > Create new file.
    • Name the new folder path: .github/workflows/intro-learn-github-actions.yml.
  3. Paste the following YAML content into the editor:

    name: Intro - Learn GitHub Actions
    
    run-name: $ is learning GitHub Actions
    
    on:
      workflow_dispatch:
      repository_dispatch:
        types: [trigger-from-api]
    
    jobs:
      check-bats-version:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
    
          - name: Install Bats
            run: sudo apt-get update && sudo apt-get install -y bats
    
          - name: Check Bats Version
            run: bats --version
    
  4. Scroll down and click Commit new file to save the workflow.

Step 3: Understand the workflow

  1. The workflow is named Intro - Learn GitHub Actions.
  2. It is triggered by two events:

    • Manually via workflow_dispatch
    • Remotely via repository_dispatch using the custom event type trigger-from-api.
  3. It runs on ubuntu-latest.
  4. It includes:

    • A step to install Bats using apt-get.
    • A step to check and print the installed version of Bats.
  5. The workflow uses $ to print the GitHub username or token owner that triggered the run.

Step 4: Trigger the workflow manually

  1. Go to the Actions tab in your repository.
  2. Select the workflow titled Intro - Learn GitHub Actions.
  3. Click the Run workflow button.
  4. Wait for the workflow to complete.

Step 5: Trigger the workflow via API

  1. Use the following curl command from your terminal, replacing the placeholders with your actual values:

    curl -X POST \
      -H "Authorization: token <YOUR_PERSONAL_ACCESS_TOKEN>" \
      -H "Accept: application/vnd.github.v3+json" \
      https://api.github.com/repos/<OWNER>/<REPO>/dispatches \
      -d '{"event_type":"trigger-from-api"}'
    
  2. After a few seconds, return to the Actions tab to view the run.

    • The run-name will show something like: your-username is learning GitHub Actions.

Step 6: View the results

  1. Click on the workflow run to open its details.
  2. Click on the check-bats-version job.
  3. Expand the step titled Check Bats Version.
  4. You should see the Bats version, such as Bats 1.7.0, printed in the logs.

Summary

In this lab, you created a GitHub Actions workflow that installs and checks the version of Bats on a Linux runner. You also learned how to trigger workflows manually or via API, and how GitHub identifies the triggering actor.


Additional Resources

  1. GitHub Actions Documentation
  2. Bats-core on GitHub
  3. Repository Dispatch Event