This repository illustrates how GitHub Actions can be used to automate software development processes.
GitHub Actions are scripts that run on a containerized platform hosted on GitHub. A GitHub action is defined by creating a .yml file in the .github/workflows directory of a repository (as done here). Such an action needs to follow a specific format as described in the GitHub documentation. An example is provided in the triage_issues.yml action file:
name: "Label Issues for Triage"
on:
issues:
types:
- reopened
- opened
jobs:
label_issues:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- run: gh issue edit "$NUMBER" --add-label "$LABELS"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
LABELS: triageAn action consists of the following basic components:
name: name of the action.events: theonkeyword specifies under what circumstance the action should run. In this example, the action will run when an issue is opened or reopened.jobs: specifies one or more things that should happen when the event is triggered. Here, we issue a command to GitHub to edit the modified issue by adding a label.
We will see other types of events and actions later on.
If you are familiar with containerized applications, you will notice familiar concepts. One key aspect of a container is to specify its base operating system. Here, we specify that we want to run the latest available version of Ubuntu, which is a Linux operating system. Jobs are similar to RUN commands in Docker but are tailored specifically to the task of automating processes and provide many capabilities related to interacting with GitHub.
Now that you understand the basic structure of a GitHub Action, we will see it in, well, action.
In this exercise, you will work with a small application called the Galactic Pizza Delivery Time Estimator. The program calculates how long it takes to deliver a pizza to different planets using several factors: the destination distance, the delivery mode (normal, turbo, or hyperjump), a surge-load multiplier, and a weather-delay component. Some planets involve long-distance fatigue penalties, while incorrect inputs or unexpected weather values trigger errors.
The application can be found in the spacedelivery folder. There are two versions:
python: the Python version is a single module (delivery.py) and just one function. That module will be used for illustrating building, testing, and releasing applications using GitHub Actions.web: the web version is a static web application of essentially the same functionality as the Python version. It also implements a user interface. This version is used to illustrate how to deploy a static web application to GitHub Pages via GitHub Actions.
The application itself is secondary and only used to illustrate CI/CD concepts.
The directory structure is as follows:
github-actions/
├── .github/ # GitHub-specific configuration
│ └── workflows/ # This is where GitHub will look for `.yml` Action files. You'll need to create this folder.
├── deployment_scripts/ # Python scripts that will be called by the *release* Action
├── spacedelivery/ # Main application package
│ ├── delivery.py # Python module for delivery calculations
│ └── web/ # Web version of the application to be deployed by the *deploy* Action
├── tests/ # Tests that will be executed by the *test* Action
├── __version__.py # Version information to be updated by the *release* Action
└── requirements.txt # Python dependencies that will be installed during the *build* ActionTo get started, fork this repository. You should only make changes in your own copy of the repository. Once you have forked the repository, go to the Settings tab of your forked repository and click on Pages on the left-hand side. There, click on the Source drop-down and select GitHub Actions. Note that you need to make the selection even if the value is already pre-populated:
Now, your fork is properly configured. To make changes you'll want to clone it to your local machine:
git clone git@github.com:<your_user_name>/github-actions.gitMake sure to replace <your_user_name> with your GitHub username. This should create a local folder named github-actions.
All modifications are performed in the development branch. Therefore, create that branch now by running the following command:
git checkout -b developmentAll Actions will reside in a folder named .github/workflows. Create that folder in the root of your project directory.
👏 You are now all set to start the exercises!
This lab gradually builds up a CI/CD pipeline using GitHub Actions. As you know, Continuous Integration (CI) and Continuous Deployment (CD) are practices that help teams deliver software quickly and reliably. CI focuses on automatically building and testing code every time changes are made, ensuring that bugs are caught early and that new work integrates smoothly. CD extends this idea by automatically releasing and deploying software after it passes tests. GitHub Actions provides an easy way to implement CI/CD directly within a GitHub repository. By defining simple workflow files, you can automate tasks such as running tests, checking code quality, updating version numbers, or deploying applications—all triggered whenever code is pushed or a pull request is opened.
The diagram below shows the steps of the CI/CD pipeline that we will build in this lab:
We will implement GitHub Actions that are automatically triggered by events in GitHub, namely:
pushevent: runs tests whenever a new commit is pushed to the repositorypull_requestevent: runs tests whenever a new pull request is opened against the repositorypushtomain: triggered whenever a PR is merged into themainbranch (or code is pushed directly to it) and deploys the application.
These events will trigger GitHub Actions to build, test (test.yml), release (release.yml), and deploy (deploy.yml) the application. You will create these Actions as you follow the exercise instructions.
One of the tasks that goes along with making changes to your code is testing. We have learned how to create test cases and even implemented unit tests that can be executed automatically. As part of a CI (Continuous Integration) pipeline, we can run tests automatically whenever a new commit is pushed to the repository. In this exercise, we will integrate that part of the CI pipeline using GitHub Actions.
In the .github/workflows directory, create a new file named test.yml and copy the following contents:
name: Build and test
on:
push:
branches: ["development"] # Triggered when a commit is pushed to the development branch
jobs:
build:
runs-on: ubuntu-latest # Run this code on a Linux Virtual Machine (VM)
steps: # Steps that are executed for this Action
# Checks out the code into the VM
- name: Checkout repository
uses: actions/checkout@v4
# Installs Python
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
# Installs the dependencies
- name: Install Dependencies
run: pip install -r requirements.txt
# Discovers and runs the tests
- name: Run tests
run: pytest -qSave the file and push the changes to the repository:
git add --all
git commit -m "Added test action"
git pushNow, go to your GitHub repository page in your browser and click on the Actions tab. You should see the test action running. Click on the action to reveal details about each step of the action (as defined in the steps section of the workflow file).
Is your Action failing? That's ok. That's what we expect. Let's fix it. Check the details of the failed test output. It should tell you what test was failing and where in the code that failure occurred. Go to the respective test in tests/test_delivery.py and update the test case to match the value that is returned by the function.
Once you update the test case, push your changes to the repository with the same commands as above. Again, navigate to the Actions tab in GitHub to see the output of the test run.
This exercise illustrates one of the most fundamental parts of a CI/CD pipeline: automated quality assurance by executing test cases. That provides developers early warning signs that something is broken and should be fixed immediately.
Imagine you have completed making changes to the Galactic Pizza Delivery Time Estimator and are ready to push a new release out to your users. There are a variety of steps that could be entailed in a release process. For this exercise, we will have our GitHub Action automatically update the version of our application and create release notes.
ℹ️ Semantic versioning is a standardized way of assigning version numbers so that users can understand the scope and impact of changes in a release. A semantic version has the form MAJOR.MINOR.PATCH (e.g., 1.8.16). The MAJOR number increases when changes break backward compatibility, MINOR increases when new features are added without breaking existing behavior, and PATCH increases for backwards-compatible bug fixes.
Keeping up with versions can be tedious, so we will let GitHub Actions handle the versioning for us. Create a new file named release.yml in .github/workflows and copy the content below:
name: Update version and add release notes to README
on:
pull_request:
types: [opened] # Only executed when a new PR is created
permissions: # Required for the Action to change our version and README files
contents: write
pull-requests: write
jobs:
update_readme:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
# Calls a Python script to increase the version number in `__version__.py`
- name: Increase version number
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
python deployment_scripts/update_version_number.py
# Calls a Python script to update the release notes in `README.md`
- name: Update release notes
env:
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
run: |
python deployment_scripts/update_release_notes.py
# Commits the changes the script made to `__version__.py` and `README.md`
- name: Commit changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add __version__.py
git add README.md
git commit -m "Add release notes for PR #${{ github.event.pull_request.number }}" || echo "No changes to commit"
git pushThe Action itself doesn't do a lot. It just calls Python scripts that reside in our repo (deployment_scripts):
update_version_number.py: reads the version number from__version__.pyand increments it.update_release_notes.py: reads the PR title and body and adds them to theREADME.mdfile.
These Python scripts use the information you enter in your PR title and description. To increase the version number, the script checks if the PR title contains major or minor. If so, it will increase the respective part of the semantic version number. The description of the PR will be used as the release notes that are inserted into the README file.
Once you have created the release.yml Action file, push it to the repository as described above. Now, go to the Pull request tab of your GitHub repository. Create a new PR with the following info:
-
Title:
Emergency fix for cheese crust pizza -
Body:
- Patching a bug found when calculating cheese crust pizza delivery cost. - Slight UI improvements.
Create the PR and go to the Actions tab. You should see the release Action running or already be completed. After the Action is completed, check the Files changed section in the PR for the changes made to __version__.py and README.md.
❓ Can you see what part of the version number was updated (e.g., major, minor, patch)? Do you know why?
We don't really want to merge this PR. Go ahead and click on Close pull request at the very bottom of the Conversation tab of the PR.
Now, let's see if we can make our Action update the Minor part of the semantic version number. Create a new PR with the following details:
-
Title:
Minor update for delivery to Mars -
Body:
- Can now calculate delivery to Mars! - Minor bug fixes
Create the PR and check again on the Action execution and then the changes it made. Did it work to increase the minor part of the version number?
Let's keep this PR open since we'll use it in the next exercise.
This exercise illustrated how we can use GitHub Actions to automate the release process, letting developers focus on being productive and leaving tedious tasks up to the CI/CD pipeline.
To make our application available to users, we need to deploy it to some server. For this exercise, we will use GitHub Pages to host our application. GitHub Pages is a free hosting service provided by GitHub that allows you to publish static websites directly from a GitHub repository.
Create a file in .github/workflows and name it deploy.yml. Then copy the contents below:
name: Deploy static content to Pages
on:
push:
branches: [ main ] # Runs when PR is merged to main branch or code is pushed directly to main
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: "./spacedelivery/web" # directory containing the web app files
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4Now, push the changes to the development branch. Go to the PR that should still be open. If not, you can just create a new PR merging the development branch into the main branch. Next, click on Merge pull request. That event will trigger the deployment script above and deploy the app to GitHub Pages. Go to the page:
https://enpm611.github.io/github-actions
but replace the user name to match your forked repository. You should see the app running.
This exercise illustrated the last step of the CI/CD pipeline, which is delivering the application to its final destination from where users will be able to access and interact with it. You have now implemented your own CI/CD pipeline. You can find many more actions in the GitHub Marketplace.
You finished all the exercises above already? Great job! The next few exercises build on the previous ones but introduce some additional complexities. They are ordered by increasing complexity and give you fewer step-by-step instructions as you progress. Consult the GitHub Actions documentation and the GitHub Marketplace whenever you get stuck. Do all work on the development branch unless told otherwise.
So far, our tests only run on push to development, even though the pipeline diagram promises they also run on pull requests. Real projects also check code style and make sure the code works on more than one runtime version.
- Run on pull requests. Update
test.ymlso that it is also triggered bypull_requestevents targetingmain. Open a PR fromdevelopmenttomainand verify that the checks appear at the bottom of the PR. - Add a lint step. Add
flake8torequirements.txtand add a step totest.ymlthat runsflake8 spacedelivery deployment_scripts tests --max-line-length=100. Push it and watch it fail. Fix the reported problems in the code (do not simply disable the linter). Hint: some warnings intests/test_delivery.pyare intentional (E402: import aftersys.pathchange). Decide whether to fix the cause or silence only that specific rule for that specific file using a# noqa: E402comment. - Add a build matrix. Use a
strategy.matrixso the tests run on Python3.10,3.11, and3.12in parallel. Replace the hard-coded Python version with${{ matrix.python-version }}. - Speed it up. Enable dependency caching by adding
cache: pipto theactions/setup-pythonstep. Run the workflow twice and compare the duration of theInstall Dependenciesstep.
❓ In the Actions tab, how many jobs are shown for a single run now? What happens to the other matrix jobs if one of them fails? Look up the fail-fast option and explain when you would want to change it.
Passing tests say little if they only exercise a fraction of your code. In this exercise, you will measure test coverage, enforce a minimum, and publish the results so that they can be downloaded from a run.
-
Add
pytest-covtorequirements.txt. Change the test step to:pytest -q --cov=spacedelivery --cov-report=term-missing --cov-report=xml --cov-report=html --cov-fail-under=90
-
Push and look at the log output. Which lines (if any) are reported as missing? Add test cases to
tests/test_delivery.pyuntil the build passes. Then raise the threshold to100and make sure the pipeline still passes. -
Use
actions/upload-artifactto upload thehtmlcov/directory as an artifact namedcoverage-report. The upload must happen even when the tests fail (hint: look at theif: always()condition). -
Write the coverage summary to the run page. GitHub provides a special file,
$GITHUB_STEP_SUMMARY, that renders Markdown on the workflow summary page. Add a step that appends a short table to this file, for example the percentage reported incoverage.xml. -
Download the artifact from the run summary page, open
index.htmllocally, and find the branch/line that is covered bytest_fatigue_penalty_kicks_in.
❓ Why would you want the coverage upload to run even if tests fail? What is a downside of enforcing 100% coverage as a hard gate?
Our release Action decides between major, minor, and patch by searching the PR title for the words "major" or "minor". That is fragile. A PR titled "Fix minority-language support" would be treated as a minor release. In this exercise, you will make the release process more robust and discover why that is harder than it looks.
-
Write failing tests first. Create
tests/test_deployment_scripts.pywith tests forbump_version()indeployment_scripts/update_version_number.py. Use pytest'stmp_pathandmonkeypatchfixtures to run the function against a temporary__version__.pyand a fakePR_TITLE(hint: the script reads__version__.pyfrom the current working directory, so usemonkeypatch.chdir(tmp_path); and sincedeployment_scriptsis not a package, you may need to add it tosys.pathor load the module withimportlib). Cover at least: patch, minor (resets patch to0), major (resets minor and patch to0), and the "minority" false positive described above. The false-positive test should fail. Make sure the new tests are executed by yourtest.ymlAction. -
Switch to labels. Create the labels
majorandminorin your repository (Issues → Labels). Changerelease.ymlso that it passes the PR labels to the script, for example:env: PR_LABELS: ${{ toJSON(github.event.pull_request.labels.*.name) }}
Update
get_release_type()to readPR_LABELS(parse it withjson.loads) instead of looking at the title. No label meanspatch. If both labels are present,majorwins. -
Discover the timing problem. Create a PR and add the
minorlabel after creating it. Was the version bumped correctly? Explain why not, given thetypes: [opened]trigger. -
Fix it without bumping twice. Extend the trigger to include
labeledandunlabeled(and optionallysynchronize). Now every label change causes a second bump on top of the first one. Design and implement a solution such that the version in the PR is always exactly one bump ahead of the version onmain, no matter how many times the workflow runs. Hint: compare against the version on the base branch, e.g. by checking out${{ github.base_ref }}or runninggit show origin/${{ github.base_ref }}:__version__.py. -
Do not duplicate release notes. The script
update_release_notes.pyappends to the end of the README every time it runs. Make it replace the entry for the current PR instead of adding another one (hint: add a hidden marker such as<!-- release-notes:PR-123 -->to each entry). -
Avoid infinite loops. Your Action pushes a commit to the PR branch. Explain why this does not trigger the test workflow when you use the default
GITHUB_TOKEN, and what would change if you used a personal access token instead.
❓ Why is passing github.event.pull_request.title through env: safer than writing ${{ github.event.pull_request.title }} directly inside a run: script? Look up script injection in the GitHub security hardening guide.
Right now test.yml and deploy.yml are completely independent. Pushing a broken change to main still deploys it to production. In this exercise, you will connect the pieces so that deployment can only happen after the tests pass, without copy-pasting steps.
-
Make
test.ymlreusable. Add aworkflow_calltrigger totest.ymlin addition to the existing triggers:on: push: branches: ["development"] pull_request: branches: ["main"] workflow_call:
-
Gate the deployment. In
deploy.yml, add a first job namedtestthat calls the reusable workflow withuses: ./.github/workflows/test.yml. Then make the existingdeployjob depend on it usingneeds: test. -
Prove the gate works. On
development, deliberately break a test (e.g., change an expected value), open a PR tomain, and merge it. Verify that thedeployjob is skipped and the live site is unchanged. Then fix the test, push again, and watch the deployment succeed. (Remember to repairmainbefore continuing!) -
Add a smoke test after deployment. Add a third job,
verify, that runs afterdeploy(needs: deploy) and usescurl --fail --retry 5 --retry-delay 5against the deployed URL. Pass the URL between jobs using theoutputsof thedeployjob (hint:${{ steps.deployment.outputs.page_url }}). Check that the HTML contains the title of the application usinggrep. -
Add a manual trigger with an input. Give
deploy.ymlaworkflow_dispatchinput namedskip_tests(typeboolean, defaultfalse). When it istrue, thetestjob should be skipped butdeploymust still run. This is trickier than it sounds because a job thatneedsa skipped job is also skipped by default. Look up theif:expressionsalways()andneeds.<job>.result.
❓ Draw the dependency graph (test → deploy → verify) of your workflow. Compare it to the visualization that GitHub shows on the run summary page.
Workflows run code with access to your repository and tokens, which makes them an attractive attack target. In this exercise, you will review your own pipeline as a security engineer would.
-
Least privilege. Set
permissions: {}(no permissions) at the top oftest.yml, then grant back only what each job needs. Does the test job still work? Why doesactions/checkoutneedcontents: readwhen the repository is private but not necessarily when it is public? -
Pin actions to commit SHAs. Tags like
@v4can be moved by the action's owner. Replace everyuses:reference in your workflows with the full commit SHA of the release, keeping the version as a comment:uses: actions/checkout@<full-40-character-sha> # v4.2.2
-
Keep them up to date automatically. Pinned SHAs are painful to update by hand. Create
.github/dependabot.ymlwith entries for thegithub-actionsandpipecosystems, checked weekly. Explain in your own words why Dependabot PRs are a good fit for the pipeline you built in Exercises 4-7. -
Restrict who can trigger a deployment. In your fork under Settings → Environments → github-pages, add a Required reviewer (yourself) and restrict deployments to the
mainbranch. Trigger a deployment and approve it from the run page. -
Find the vulnerability. The following step is added to a workflow in your repository by a (well-meaning) teammate:
- name: Greet contributor run: echo "Thanks for the PR '${{ github.event.pull_request.title }}'!"
Craft a PR title that makes this step execute an additional command (try printing the string
pwnedon a separate line - only in your own fork). Then fix the step using an environment variable, as inrelease.yml. -
Write a short threat model (5-10 sentences) in a new file
docs/pipeline_security.md: Which secrets and permissions does your pipeline have? What could an attacker do who can open a PR from a fork? What mitigations have you applied?
In this final exercise, you will bring everything together by building a release process that publishes a downloadable package whenever a version tag is pushed, and by extracting duplicated steps into a custom action.
- Create a composite action. Several of your workflows now repeat the same steps: checkout, set up Python, install dependencies. Create
.github/actions/setup-python-env/action.ymlas a composite action with an inputpython-version(default3.11). Replace the duplicated steps intest.ymlandrelease.ymlwithuses: ./.github/actions/setup-python-env. - Build a distributable. Add a small
pyproject.toml(orsetup.cfg) so thatspacedeliverycan be built as a package, with the version read dynamically from__version__.py. Verify locally withpip install build && python -m build. - Create
publish.yml. The workflow must be triggered when a tag matchingv*.*.*is pushed. It should have three jobs:test: reusestest.yml(Exercise 7).build: builds the package withpython -m buildand uploadsdist/as an artifact.release: downloads the artifact and creates a GitHub Release usinggh release create "$GITHUB_REF_NAME" dist/* --generate-notes. This job needspermissions: contents: write.
- Validate the tag. Add a step to the
buildjob that fails if the pushed tag (v1.2.3) does not match the version in__version__.py(1.2.3). Test both the success and failure cases by pushing a matching and a non-matching tag. - Schedule a nightly check. Add a
scheduletrigger (cron) totest.ymlthat runs the tests every night at 03:00 UTC, so that you notice if a dependency update breaks the build even when nobody pushes code. Useworkflow_dispatchto trigger it manually while testing. What are the limitations of scheduled workflows on forks and inactive repositories? - Stretch goals (pick one):
- Publish the package to TestPyPI using trusted publishing (OIDC) instead of a stored API token.
- Build and push a Docker image of the application to the GitHub Container Registry (
ghcr.io) withdocker/build-push-action. - Post a comment on each PR with the coverage summary from Exercise 5 using
actions/github-script.
❓ Look at your finished pipeline. Which events trigger which workflows? Create a table or diagram that documents it, and update docs/github_actions_exercises.svg or add a Mermaid diagram to this README.
This section should be populated by the release Action.
