act

repository·master·Indexed 13 days ago

https://github.com/nektos/act

A tool for running GitHub Actions locally using Docker to emulate the GitHub-hosted runner environment. It allows developers to test .github/workflows/ files without committing to GitHub, providing fast feedback loops and the ability to serve as a local task runner. Features include support for custom Docker images, secret and environment variable management via CLI or files, a built-in artifact server, and a file watcher for automatic workflow execution.

Tokens
4.4K
Snippets
14
Records
21
Agent score
98%

What's inside act

  1. What is act and how does it work?

    master

    act is a tool that allows you to run GitHub Actions locally. It provides fast feedback by letting you test .github/workflows/ files without committing and pushing to GitHub, and it can serve as a local task runner to replace tools like make using your existing GitHub Actions definitions.

    How it works:

    1. Parsing: act reads your GitHub Actions from the .github/workflows/ directory.
    2. Dependency Resolution: It determines the set of actions required and the execution path based on defined dependencies.
    3. Image Preparation: It uses the Docker API to pull or build the necessary images defined in your workflow files.
    4. Execution: It uses the Docker API to run containers for each action. The environment variables and filesystem are configured to match the GitHub-hosted runner environment.
  2. Use configuration files (.actrc)

    master

    Instead of passing long lists of flags every time, you can define your configuration in .actrc files. act searches for these files in the following order:

    1. The XDG configuration directory (e.g., ~/.config/act/actrc).
    2. The user's HOME directory (~/.actrc).
    3. The current working directory (./.actrc).
  3. Watch for file changes and auto-run

    master
    The --watch (or -w) flag enables a file watcher. act will monitor your local repository for changes and automatically trigger a run when files are modified. It respects your .gitignore file to avoid unnecessary runs.
    act -w
  4. Configure secrets, variables, and environment variables

    master

    You can pass sensitive information or configuration to your workflows using several flags. You can provide them directly via CLI or by pointing to files.

    Direct CLI flags

    • -s, --secret <name>[=<value>]: Pass a secret (e.g., -s MY_SECRET=foo or -s MY_SECRET).
    • --var <name>[=<value>]: Pass a variable (e.g., --var MY_VAR=foo).
    • --env <name>[=<value>]: Pass an environment variable (e.g., --env MY_ENV=foo).
    • --input <name>[=<value>]: Pass an action input.

    File-based configuration

    • --secret-file <path>: Read secrets from a file (defaults to .secrets).
    • --var-file <path>: Read variables from a file (defaults to .vars).
    • --env-file <path>: Read environment variables from a file (defaults to .env).
    • --input-file <path>: Read action inputs from a file (defaults to .input).
    act -s MY_SECRET=password --var MY_VAR=value --env MY_ENV=foo
    act --secret-file .secrets --var-file .vars
  5. Run GitHub Actions locally with act

    master

    The act CLI allows you to run GitHub Actions workflows on your local machine using Docker. By default, if no event name is provided, act will attempt to run workflows triggered by the push event. If your workflows only handle a single specific event, act will use that as the default instead.

    # Run the default event (usually push)
    act
    
    # Run a specific event
    act pull_request
    
    # Run a specific job
    act -j build
  6. Build act from source

    master

    If you need to build act manually, ensure you have Go tools 1.20 or higher installed. Follow these steps:

    1. Clone the repository.
    2. Run unit tests to verify the build.
    3. Build and install the binary.
    # Install Go tools 1.20+
    # Clone this repo
    git clone git@github.com:nektos/act.git
    
    # Run unit tests
    make test
    
    # Build and install
    make install
  7. Disable version check notices

    master

    By default, act attempts to fetch version-specific notices from the act API. You can disable this behavior by setting the ACT_DISABLE_VERSION_CHECK environment variable to 1. This prevents the tool from making network requests to https://api.nektosact.com/notices to check for updates or announcements.

    export ACT_DISABLE_VERSION_CHECK=1
  8. How secrets are handled and mapped in act

    master

    When providing secrets to act, they are processed as a case-insensitive map of key-value pairs. The tool resolves secret values using the following precedence:

    1. Explicit Key-Value Pairs: If a secret is provided in the format KEY=VALUE, that value is used.
    2. Environment Variables: If a secret is provided as just a KEY (without an =), act looks for an existing environment variable with that name. If found and non-empty, it uses that value.
    3. Interactive Input: If the key is not provided with a value and no matching environment variable exists, act will prompt the user to enter the password/value manually via the terminal (using term.ReadPassword for secure input).

    Note: All secret keys are converted to uppercase internally, making them case-insensitive.

  9. Choose Docker images based on virtual environments

    master

    For environments that closely mimic GitHub Actions runners, you can use images based on actions/virtual-environments.

    Nektos Environments: Note: nektos/act-environments-ubuntu images were last updated in February 2020. It is recommended to manually update the image after docker pull if you choose to use them.

    • nektos/act-environments-ubuntu:18.04
    • nektos/act-environments-ubuntu:18.04-lite
    • nektos/act-environments-ubuntu:18.04-full

    Catthehacker Virtual Environment Forks:

    • catthehacker/ubuntu:full-latest
    • catthehacker/ubuntu:full-20.04
    • catthehacker/ubuntu:full-18.04
  10. List and visualize workflows

    master

    Use act to inspect your workflow configurations without executing them.

    • -l, --list: List all available workflows/jobs.
    • -g, --graph: Draw a graph of the workflows.
    • --list-options: Print a JSON structure of compatible CLI options.
    act --list
    act --graph
  11. Choose Docker images based on buildpack-deps

    master

    You can use images based on buildpack-deps for your act runs.

    Important Considerations:

    • Node images: These are based on the Debian root filesystem. While similar to Ubuntu, there may be differences.
    • Slim images: node -slim images do not have python installed. If your actions or software depend on Python, you must specify a non-slim image manually.

    Available Images:

    • node:16-bullseye
    • node:16-bullseye-slim (No Python)
    • node:16-buster
    • node:16-buster-slim (No Python)

    Ubuntu-based images (catthehacker): These use an Ubuntu root filesystem:

    • catthehacker/ubuntu:act-latest
    • catthehacker/ubuntu:act-22.04
    • catthehacker/ubuntu:act-20.04
    • catthehacker/ubuntu:act-18.04
  12. Manage Docker container settings and platforms

    master

    Act uses Docker containers to execute jobs. You can customize how these containers behave and which images they use.

    Platform and Image mapping

    • -P, --platform <platform>=<image>: Map a specific platform to a custom Docker image (e.g., -P ubuntu-latest=catthehacker/ubuntu:full-latest).
    • --container-architecture <arch>: Specify the architecture for containers (e.g., linux/amd64).

    Container behavior

    • -r, --reuse: Do not remove containers on successful completion to maintain state between runs.
    • -b, --bind: Bind the working directory to the container instead of copying it.
    • -p, --pull: Pull Docker images even if they are already present (default is true).
    • --rebuild: Rebuild local action Docker images even if they are already present.
    • --rm: Automatically remove containers/volumes after a workflow failure.
    • --privileged: Run containers in privileged mode (deprecated; use --container-options).
    • --container-options <options>: Custom Docker container options.
    • --container-cap-add <capability>: Add kernel capabilities (e.g., --container-cap-add SYS_PTRACE).
    • --container-cap-drop <capability>: Remove kernel capabilities.
    # Use a specific image for ubuntu-latest and bind the directory
    act -P ubuntu-latest=my-custom-image:latest -b
    
    # Run with specific architecture
    act --container-architecture linux/amd64