Act - Local Workflow Testing
Act - 本地工作流测试
Use this skill when testing GitHub Actions workflows locally with act. This covers act CLI commands, Docker setup, debugging, and best practices for fast local iteration on CI/CD workflows.
当需要使用 act 在本地测试 GitHub Actions 工作流时,请使用此 skill。本 skill 涵盖 act CLI 命令、Docker 设置、调试,以及在本地快速迭代 CI/CD 工作流的最佳实践。
bash
curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
bash
curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash
bash
choco install act-cli
bash
choco install act-cli
bash
go install github.com/nektos/act@latest
bash
go install github.com/nektos/act@latest
Run workflows triggered by push event
Run workflows triggered by push event
Equivalent to
Equivalent to
Run Specific Events
运行特定事件
Pull request event
Pull request event
Workflow dispatch
Workflow dispatch
act repository_dispatch -e event.json
act repository_dispatch -e event.json
Run Specific Workflows
运行特定工作流
Run specific workflow file
Run specific workflow file
act -W .github/workflows/ci.yml
act -W .github/workflows/ci.yml
Run specific job
Run specific job
Run specific workflow and job
Run specific workflow and job
act -W .github/workflows/deploy.yml -j production
act -W .github/workflows/deploy.yml -j production
List Available Workflows
列出可用工作流
List all workflows and jobs
List all workflows and jobs
List for specific event
List for specific event
Validation and Dry Runs
验证与试运行
Validate without executing
Validate without executing
Show what would run
Show what would run
Validate specific workflow
Validate specific workflow
act --dryrun -W .github/workflows/ci.yml
act --dryrun -W .github/workflows/ci.yml
Show workflow graph
Show workflow graph
Show graph for specific event
Show graph for specific event
Docker Configuration
Docker 配置
Act uses Docker images to simulate GitHub's runners:
Act 使用 Docker 镜像来模拟 GitHub 的 Runner:
Use default images (micro - minimal)
Use default images (micro - minimal)
Use medium images (more tools)
Use medium images (more tools)
act -P ubuntu-latest=catthehacker/ubuntu:act-latest
act -P ubuntu-latest=catthehacker/ubuntu:act-latest
Use large images (most compatible)
Use large images (most compatible)
act -P ubuntu-latest=catthehacker/ubuntu:full-latest
act -P ubuntu-latest=catthehacker/ubuntu:full-latest
Custom Platform Images
自定义平台镜像
Create
file in project root:
-P ubuntu-latest=catthehacker/ubuntu:act-latest
-P ubuntu-22.04=catthehacker/ubuntu:act-22.04
-P ubuntu-20.04=catthehacker/ubuntu:act-20.04
Or use command line:
bash
act -P ubuntu-latest=node:18 \
-P ubuntu-22.04=catthehacker/ubuntu:act-22.04
-P ubuntu-latest=catthehacker/ubuntu:act-latest
-P ubuntu-22.04=catthehacker/ubuntu:act-22.04
-P ubuntu-20.04=catthehacker/ubuntu:act-20.04
或者使用命令行:
bash
act -P ubuntu-latest=node:18 \
-P ubuntu-22.04=catthehacker/ubuntu:act-22.04
Reusing Docker Containers
复用 Docker 容器
Reuse containers between runs (faster)
Reuse containers between runs (faster)
Clean up after run
Clean up after run
Using .secrets File
使用 .secrets 文件
GITHUB_TOKEN=ghp_your_token_here
NPM_TOKEN=npm_your_token_here
AWS_ACCESS_KEY_ID=your_key
AWS_SECRET_ACCESS_KEY=your_secret
Run with secrets:
bash
act --secret-file .secrets
GITHUB_TOKEN=ghp_your_token_here
NPM_TOKEN=npm_your_token_here
AWS_ACCESS_KEY_ID=your_key
AWS_SECRET_ACCESS_KEY=your_secret
使用密钥运行:
bash
act --secret-file .secrets
Single secret
Single secret
act -s GITHUB_TOKEN=ghp_token
act -s GITHUB_TOKEN=ghp_token
Multiple secrets
Multiple secrets
act -s GITHUB_TOKEN=ghp_token
-s NPM_TOKEN=npm_token
act -s GITHUB_TOKEN=ghp_token
-s NPM_TOKEN=npm_token
Environment-Specific Secrets
环境特定的密钥
Development secrets
Development secrets
act --secret-file .secrets.dev
act --secret-file .secrets.dev
Production secrets
Production secrets
act --secret-file .secrets.prod
act --secret-file .secrets.prod
Environment Variables
环境变量
Single variable
Single variable
act --env NODE_ENV=development
act --env NODE_ENV=development
Multiple variables
Multiple variables
act --env NODE_ENV=development
--env DEBUG=true
act --env NODE_ENV=development
--env DEBUG=true
Using .env File
使用 .env 文件
NODE_ENV=development
DEBUG=true
LOG_LEVEL=debug
Run with env file:
NODE_ENV=development
DEBUG=true
LOG_LEVEL=debug
使用 env 文件运行:
GitHub Context Variables
GitHub 上下文变量
Act automatically sets these:
GITHUB_ACTOR=nektos/act
GITHUB_REPOSITORY=owner/repo
GITHUB_EVENT_NAME=push
GITHUB_SHA=abc123...
GITHUB_REF=refs/heads/main
ACT=true
Act 自动设置以下变量:
GITHUB_ACTOR=nektos/act
GITHUB_REPOSITORY=owner/repo
GITHUB_EVENT_NAME=push
GITHUB_SHA=abc123...
GITHUB_REF=refs/heads/main
ACT=true
Very verbose (debug)
Very verbose (debug)
Step-by-Step Execution
分步执行
Interactive mode - pause before each step
Interactive mode - pause before each step
Keep container running after workflow
Keep container running after workflow
Then in another terminal
Then in another terminal
docker ps
docker exec -it <container-id> /bin/bash
docker ps
docker exec -it <container-id> /bin/bash
Bind Mount Local Files
绑定挂载本地文件
Mount current directory
Mount current directory
Mount specific directory
Mount specific directory
act -b /host/path:/container/path
act -b /host/path:/container/path
Validate workflow syntax
Validate workflow syntax
Iterative Development
迭代开发
Edit workflow
Edit workflow
vim .github/workflows/ci.yml
vim .github/workflows/ci.yml
Test immediately
Test immediately
Iterate quickly
Iterate quickly
Run specific matrix combination
Run specific matrix combination
act --matrix os:ubuntu-latest --matrix node:20
act --matrix os:ubuntu-latest --matrix node:20
Run all combinations
Run all combinations
Check Docker is running
Check Docker is running
Pull required images manually
Pull required images manually
docker pull catthehacker/ubuntu:act-latest
docker pull catthehacker/ubuntu:act-latest
Clean up Docker resources
Clean up Docker resources
Run with sudo (Linux)
Run with sudo (Linux)
Fix Docker permissions (Linux)
Fix Docker permissions (Linux)
sudo usermod -aG docker $USER
newgrp docker
sudo usermod -aG docker $USER
newgrp docker
Use fuller image
Use fuller image
act -P ubuntu-latest=catthehacker/ubuntu:full-latest
act -P ubuntu-latest=catthehacker/ubuntu:full-latest
Or install in workflow
Or install in workflow
- run: |
apt-get update
apt-get install -y some-tool
- run: |
apt-get update
apt-get install -y some-tool
Check workflow files exist
Check workflow files exist
ls -la .github/workflows/
ls -la .github/workflows/
Validate YAML syntax
Validate YAML syntax
yamllint .github/workflows/*.yml
yamllint .github/workflows/*.yml
List detected workflows
List detected workflows
Action Compatibility
Action 兼容性
Some actions don't work with act:
Skip action in act
Skip action in act
- name: GitHub-only action
if: ${{ !env.ACT }}
uses: github/some-action@v1
- name: GitHub-only action
if: ${{ !env.ACT }}
uses: github/some-action@v1
Use alternative in act
Use alternative in act
- name: Local alternative
if: env.ACT == 'true'
run: echo "Running local version"
- name: Local alternative
if: env.ACT == 'true'
run: echo "Running local version"
✅ Use
before running full workflows
✅ Create
for consistent configuration
✅ Use
file and add it to
✅ Use
for faster iteration
✅ Test workflows locally before pushing
✅ Use appropriate image sizes for your needs
✅ Document act usage in README
✅ 运行完整工作流前使用
✅ 创建
以保持配置一致
✅ 使用
文件并将其添加到
✅ 使用
加快迭代速度
✅ 推送前在本地测试工作流
✅ 根据需求选择合适的镜像大小
✅ 在 README 中记录 act 的用法
❌ Commit
or
files
❌ Use
Docker tags in production
❌ Skip validation with
❌ Run act without understanding what it will do
❌ Ignore Docker disk space usage
❌ Assume all actions work perfectly with act
❌ 提交
或
文件
❌ 在生产环境中使用
Docker 标签
❌ 跳过使用
进行验证
❌ 在不了解 act 行为的情况下运行它
❌ 忽视 Docker 磁盘空间占用
❌ 假定所有 action 都能完美配合 act 运行
Platform mappings
Platform mappings
-P ubuntu-latest=catthehacker/ubuntu:act-latest
-P ubuntu-latest=catthehacker/ubuntu:act-latest
Default options
Default options
--reuse
--secret-file .secrets
--env-file .env
--reuse
--secret-file .secrets
--env-file .env
Container options
Container options
--container-architecture linux/amd64
--container-architecture linux/amd64
.github/workflows/.actrc
.github/workflows/.actrc
Project-specific overrides in workflows directory.
CI/CD Integration
CI/CD 集成
Pre-Push Hook
Pre-Push Hook
bash
#!/bin/bash
echo "Validating workflows..."
act --dryrun
if [ $? -ne 0 ]; then
echo "Workflow validation failed"
exit 1
fi
bash
#!/bin/bash
echo "Validating workflows..."
act --dryrun
if [ $? -ne 0 ]; then
echo "Workflow validation failed"
exit 1
fi
makefile
.PHONY: test-workflows
test-workflows:
act --dryrun
act -j test
.PHONY: ci-local
ci-local:
act --reuse
makefile
.PHONY: test-workflows
test-workflows:
act --dryrun
act -j test
.PHONY: ci-local
ci-local:
act --reuse
Use reuse flag
Use reuse flag
Skip checkout if not needed
Skip checkout if not needed
act --reuse -j test --no-recurse
act --reuse -j test --no-recurse
Use smaller images for simple tests
Use smaller images for simple tests
act -P ubuntu-latest=node:20-alpine
act -P ubuntu-latest=node:20-alpine
Act respects GitHub Actions caching:
yaml
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Act 支持 GitHub Actions 缓存:
yaml
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
- act-workflow-syntax: Creating and structuring workflow files
- act-docker-setup: Configuring Docker for act
- act-advanced-features: Advanced act usage patterns
- act-workflow-syntax:创建和构建工作流文件
- act-docker-setup:为 act 配置 Docker
- act-advanced-features:高级 act 使用模式