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
-
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
-
In your repository, click the Code tab (if not already there).
-
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
.
-
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
-
Scroll down and click Commit new file to save the workflow.
Step 3: Understand the workflow
- The workflow is named Intro - Learn GitHub Actions.
-
It is triggered by two events:
- Manually via workflow_dispatch
- Remotely via repository_dispatch using the custom event type
trigger-from-api
.
- It runs on ubuntu-latest.
-
It includes:
- A step to install Bats using
apt-get
. - A step to check and print the installed version of Bats.
- A step to install Bats using
- The workflow uses
$
to print the GitHub username or token owner that triggered the run.
Step 4: Trigger the workflow manually
- Go to the Actions tab in your repository.
- Select the workflow titled Intro - Learn GitHub Actions.
- Click the Run workflow button.
- Wait for the workflow to complete.
Step 5: Trigger the workflow via API
-
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"}'
-
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
.
- The
Step 6: View the results
- Click on the workflow run to open its details.
- Click on the check-bats-version job.
- Expand the step titled Check Bats Version.
- 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.