Over 2,000 mentors available, including leaders at Amazon, Airbnb, Netflix, and more. Check it out
Published

Streamlining Your GitHub Actions Workflow with Act

Learn how to enhance your GitHub Actions workflow efficiency with 'act,' an open-source tool that allows you to locally test your CI/CD processes without the need to commit code to a branch or create pull requests.

Senior Software Engineer, Red Hat

Continuous Integration (CI) and Continuous Delivery/Deployment (CD) are essential components of modern software development workflows. They allow developers to build, test, and deploy their applications automatically and quickly, which results in faster feedback cycles, higher-quality code, and fewer bugs.

Comic by geek-and-poke

The Importance of CI/CD in Modern Software Development

To delve further into the significance of CI/CD in modern software development, it's crucial to appreciate the overarching objectives and principles that drive these practices. CI/CD has evolved as a response to the ever-increasing complexity and demands of software development. Here are some key aspects to consider:

  1. Efficiency and Speed: In today's fast-paced software landscape, delivering features and updates quickly is paramount. CI/CD automates the process of integrating code changes and delivering them to production, reducing manual efforts and minimizing delays.
  2. Quality Assurance: Frequent code integration and automated testing in CI/CD pipelines help identify and address issues early in the development process. This results in higher code quality and fewer bugs, as developers receive rapid feedback on their changes.
  3. Predictable Releases: With CI/CD, the release process becomes more predictable and less error-prone. It allows for staging changes in a controlled manner, reducing the risk of unexpected issues in production.
  4. Collaboration and Transparency: CI/CD encourages collaboration among development and operations teams. It provides transparency into the entire software delivery process, making it easier to coordinate efforts and troubleshoot problems.
  5. Scaling and Resilience: CI/CD pipelines can be configured to scale with the application's demands. This means that as the application grows, the deployment process can adapt to handle increased traffic and maintain resilience.

Exploring GitHub Actions

GitHub Actions, as mentioned earlier, is a powerful CI/CD platform that has gained prominence in the software development ecosystem. It offers a range of features and benefits, including:

  1. Workflow Automation: GitHub Actions allows you to define and automate workflows using YAML files. This enables you to specify the exact steps that should be executed when a certain event occurs, such as a code push or a pull request.
  2. Diverse Ecosystem: GitHub Actions has a vast marketplace of pre-built actions that cover a wide range of tasks. These actions can be incorporated into your workflows to simplify tasks like code testing, deployment, and notifications.
  3. Customizability: While GitHub Actions offers a rich set of predefined actions, you can also create custom actions to tailor the CI/CD process to your specific needs.
  4. Integration with GitHub: GitHub Actions is seamlessly integrated with GitHub repositories, making it a natural choice for developers working on the platform. This tight integration simplifies the setup and execution of CI/CD pipelines.
  5. Parallel Execution: GitHub Actions allows for parallel execution of jobs and steps, which can significantly reduce the time it takes to run complex workflows.

GitHub Actions is a powerful CI/CD platform that enables developers to automate their workflows, from building and testing their code to deploying it to production. One of the limitations of GitHub Actions is that developers can only test their workflows by pushing code to a branch or opening a pull request. However, this can be time-consuming and inefficient, especially when testing complex workflows that involve multiple steps.

That’s where act comes in. Act is an open-source tool that allows developers to run their GitHub Actions workflows locally.

By running workflows locally, developers can test their code and workflows quickly and efficiently, without having to push code to a branch or open a pull request.

In this article, I will demonstrate how to use act to run a simple workflow that builds a Maven project and caches its dependencies. We will also explain how to use the cache action, which requires the act cache server to be running locally.

Getting started with act

To get started with act, you first need to install it on your machine. The easiest way to install act is via Homebrew on macOS or Linux. On Windows, you can install act using Chocolatey. See full installation method on offical docs.

Once you have installed act, you can run it by navigating to your project directory and running the command:

act -l

This command will list all the available workflows in your project’s GitHub Actions configuration file (.github/workflows/*.yml). You can then run a specific workflow by specifying its name:

act -j <job_name></job_name>

Building a Maven project with act

Let’s assume that you have a Maven project workflow that you want to build and test locally using act. Here’s an example workflow that you can use:

name: Build and Test
on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Cache dependencies
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: $-maven-$
          restore-keys: |
            $-maven-
      - name: Build and test project
        run: mvn test

This workflow consists of a single job named “build” that runs on Ubuntu. The job has three steps:

The first step checks out the code from the repository. The second step caches the Maven dependencies in the local cache directory (~/.m2/repository). This step is important as it can significantly reduce the build time of the project. The third step builds and tests the project using the mvn test command.

Using the cache action with act

The actions/cache action is an official GitHub Action that allows you to cache files and directories between jobs and workflows. In the example workflow above, we use the cache action to cache the Maven dependencies in the local cache directory.

When using the actions/cache action with act, you need to have the act cache server running locally. The cache server is responsible for managing the cache directories and serving cached files when requested by the cache action.

To start the act cache server, you need to have Docker installed on your machine. Once you have Docker installed, you can run the following command in your terminal:

export ACT_CACHE_AUTH_KEY=foo
docker compose up --build

Ensure you add the following configuration to your ~/.actrc file:

--env ACTIONS_CACHE_URL=http://localhost:8080/
--env ACTIONS_RUNTIME_URL=http://localhost:8080/
--env ACTIONS_RUNTIME_TOKEN=foo
You can set ACT_CACHE_AUTH_KEY and ACTIONS_RUNTIME_TOKEN to the value you want, but they must be the same.
-

Once the cache server is running, you can run your workflows locally using act, and the cache action will automatically use the cache server to cache and retrieve files.

act -j build

In summary, act is a powerful tool that allows to run GitHub Actions workflows locally. By using act, you can test your workflows quickly and efficiently, without having to push code to a branch or open a pull request.

Give it a try and see how it can benefit your.

Find an expert mentor

Get the career advice you need to succeed. Find a mentor who can help you with your career goals, on the leading mentorship marketplace.