Running GitHub Actions Locally with act - A Comprehensive Guide
Mahesh Bhosle
GitHub Actions has been one of the most popular platforms among developers to automate software workflows, enabling them to streamline CI/CD pipelines and automate testing processes. However, testing and iterating on these workflows directly on GitHub can be time-consuming and expensive, especially when dealing with complex actions or debugging errors. That's where the act tool comes in. It offers a seamless solution for running GitHub actions locally on a local system.
What is act?
act is a command-line tool designed to run GitHub Actions workflows locally. By simulating the GitHub Actions environment on the local machine, act enables testing, debugging, and iterating on workflows without the need to push changes to GitHub and consume valuable Actions minutes. This significantly accelerates the development process and ensures that workflows are error-free before deployment.
How does it work?
When act is run in the GitHub repository, it reads the GitHub actions from .github/workflows/ folder and finds out the set of actions that need to be performed. First, it pulls the Docker images mentioned in the workflow files with the help of Docker APIs and then runs the workflows on the Docker container.
Key Features
Local Workflow Execution: With act, workflows can be run locally, saving time and GitHub Actions minutes.
Environment Simulation: act simulates the GitHub Actions runner environment, ensuring that workflows run as if they were executed on GitHub.
Customization: act allows for customization by supporting custom Docker images, specifying event types (e.g., push, pull request) for workflow runs.
Secrets and Environment Variables: Secrets and environment variables can be passed effortlessly to workflow with act, perfectly emulating the GitHub environment.
Prerequisite
As mentioned earlier, act uses Docker containers in the background to run Github workflows, so it is a must to have Docker installed on the local system. Here's the documentation to install Docker on different operating systems:
act is available for macOS, Linux, and Windows. It can be installed with package managers or download the binary directly from the GitHub releases page. Here's how to install act on different operating systems:
macOS: Use Homebrew:
Linux: Run the following command:
Windows: Download the binary from the releases page, or use Chocolatey:
Usage
Using act is simple. Just navigate to the root of the GitHub repository where .github/workflows directory resides, and execute the following command:
This command will scan workflows and present a list of workflows to run. Or specify a workflow by its event name using the -e flag:
To run a specific job from workflows, use the -j flag
To pass secrets or environment variables to workflow, utilize the -s or --secret option:
Advanced Usage
custom Docker Images
If workflows require a specific environment, custom docker images can be used with act. Simply create a .actrc file in the home directory and specify the Docker image. For example:
Specifying the GitHub Event Payload
If the workflow needs to be triggered with specific data, then the GitHub event payload can be specified with the -p option:
Action
Let us see the act in action. Here, I have a repository with a simple to-do list, a React app, and a GitHub workflow in the .github/workflows/node.js.yml file. Workflow runs simple echo commands for each of the jobs. This is for understanding purposes; you will have to update those jobs as per your requirements.
The workflow file consists of three jobs:
build: Build the code.
test: Run the test cases.
deploy: Deploy the code.
I will start by listing all the workflows present in the repository:
So as per the output, we have 5 jobs out of which 2 jobs belong to Code Check workflow and the rest to the Build and Test React App.
NOTE: If you too feel annoyed by the warning then use
--container-architecture linux/amd64 flag or just add it to the
~/.actrc file.
Lets check all jobs that are triggered for push event:
When act is used without any flags it will run all workflows that are present in the repository:
It is not always required to run all the workflows. If I just want to run the sanity-check job, I can use the -j flag to specify the job:
act only ran the sanity-check job which not only saves a lot of time but also helps in debugging issues in the workflow jobs because with act it is possible to run each job independently.
Tips for Getting the Most Out of act
Debugging: Enable verbose logging with the -v flag to assist with troubleshooting.
Workflow Selection: If there are too many workflows in the repository, use -W flag to specify the workflow to run.
Local Execution: In case, it is required to run the workflows directly on the host system then use -P flag with one of the following parameters:
Conclusion
The act tool is a powerful asset for developers seeking to streamline their GitHub Actions workflow development. By allowing local execution, act not only saves time but Integrating act into the development process can significantly enhance workflow efficiency and reliability. Happy automating!