A collection of small GitHub Actions workflows demonstrating various capabilities and features to get you started.
A basic workflow to do the essentials for a Python project.
If you're unfamiliar with GitHub Actions this will help you get started quickly.
- Runs when changes are pushed
- Runs on a schedule
- Can be run manually from the GitHub UI
- Uses
actions/checkout
- Uses
actions/setup-python
with pip cache - Installs
requirements.txt
and runs a simple test - Includes
dependabot.yml
to automatically check for package updates
See the workflow.
Getting the branch name.
This is a common CI operation. Surprisingly, there's no pre-defined way to get it in GitHub Actions.
This demo shows the simplest way without using 3rd party actions or other tools.
It works in most cases, but there are some quirks.
For example, if your commit is tagged this method will return the tag instead of the branch name. See SO link in the references for details.
You may also get an unexpected result depending on the event that triggered the workflow. This demo is set to trigger on pull_request
and on push
to illustrate this behavior.
- Shows various
github
context properties that may or may not contain the branch name - Sets branch name to the top level
env
so it can be accessed by the entire workflow
See the workflow.
Data persistence by caching.
You can cache files, directories, or a combination of them. If you want to test for a cache hit, keep in mind that it only occurs if it matches the primary cache key
. A partial match on restore-keys
is still considered a cache miss.
- Uses
actions/cache
See the workflow.
Accessing context information about workflow runs.
You can access various contexts about the workflow run, which can be helpful for debugging workflow errors or bugs. Be careful as it has the potential to output sensitive information.
See the workflow.
Working with environment variables.
Environment variables and their scopes work as you'd expect in GitHub Actions.
They're also fairly self-contained, so any changes you make are isolated to the job you're in.
One quirk that can cause confusion is the fact that environment variables defined within a step aren't accessible until the next step.
See the workflows:
Scripting in workflows.
Easily and quickly write JavaScript in your workflow that uses the GitHub API and the workflow run context. The action includes a pre-authenticated octokit/rest.js client and references to many other useful packages.
- Uses
actions/github-script
See the workflow.
Using homebrew in your workflow.
Leverage the convenience of homebrew to install applications on GitHub Actions runners.
- Uses
Homebrew/actions/setup-homebrew
See the workflow.
Custom output for the summary page of a workflow run.
You can set custom Markdown for each job so it will be displayed on the summary page of a workflow run. Job summaries support GitHub flavored Markdown, and you can add your Markdown content for a step to the GITHUB_STEP_SUMMARY
environment file.
When a job finishes, the summaries for all steps in a job are grouped together into a single job summary and are shown on the workflow run summary page. If multiple jobs generate summaries, the job summaries are ordered by job completion time.
Job summaries are isolated between steps and each step is restricted to a maximum size of 1MB. A maximum of 20 job summaries from steps are displayed per job.
See the workflow.
Create annotations in workflow logs.
You can create notice
, warning
, and error
annotations in your workflow logs. Optionally, they can be associated with a file and even a position within the file. Annotations also show up on the job summary page.
See the workflow.
Define a matrix of different job configurations.
The matrix strategy helps you easily target multiple operating systems and language versions.
- Uses
actions/setup-node
See the workflow.
Using mise in your workflow.
The polyglot tool version manager.
- Uses
jdx/mise-action
See the workflow.
Parallel testing without any code changes or extra dependencies.
The matrix strategy can be used in a particular way to enable parallel testing for free. This means no code changes and no extra dependencies. The idea is to identify where your tests are and distrubute them across multiple GitHub Actions runners. If your testing framework supports parallel testing, you can use it in combination with this strategy to really go fast!
See the workflows:
Adjust how a workflow will run with custom input.
When using the workflow_dispatch
event, you can optionally specify inputs that are passed to the workflow.
This trigger only receives events when the workflow file is on the default branch. This means you have to merge your changes to main
or master
before you can test your inputs. It would be wise to try input changes in a totally separate workflow before merging them into critical workflows.
Also, if the event that triggers the workflow isn't workflow_dispatch
the input values are empty/null. This is true even if you have default values defined.
See the workflow.
- GitHub Actions
- GitHub Actions: Workflow syntax for GitHub Actions
- GitHub Actions: Accessing contextual information about workflow runs
- GitHub Actions: Adding a job summary
- GitHub Actions: Adding a log annotation
- GitHub Actions: Adding a system path
- GitHub Actions: Caching dependencies to speed up workflows
- GitHub Actions: Evaluate expressions in workflows and actions
- GitHub Actions: Events that trigger workflows
- GitHub Actions: Running variations of jobs in a workflow
- GitHub Actions: Setting an environment variable
- actions/cache
- actions/checkout
- actions/github-script
- actions/setup-node
- actions/setup-python
- Homebrew/actions/setup-homebrew
- jdx/mise-action
- GitHub Docs: Dependabot options reference
- octokit/rest.js API documentation
- Stack Overflow: How to get a branch name on GitHub action when push on a tag?
- Stack Overflow: How to get the current branch within Github Actions?