[ PROMPT_NODE_28058 ]
github-actions-templates
[ SKILL_DOCUMENTATION ]
# GitHub Actions 模板
用于测试、构建和部署应用程序的生产级 GitHub Actions 工作流模式。
## 不使用此技能的情况
- 任务与 GitHub Actions 模板无关
- 你需要此范围之外的其他领域或工具
## 指令
- 明确目标、约束和所需输入。
- 应用相关的最佳实践并验证结果。
- 提供可操作的步骤和验证方法。
- 如果需要详细示例,请打开 `resources/implementation-playbook.md`。
## 目的
为各种技术栈的持续集成和部署创建高效、安全的 GitHub Actions 工作流。
## 何时使用此技能
- 自动化测试和部署
- 构建 Docker 镜像并推送到注册表
- 部署到 Kubernetes 集群
- 运行安全扫描
- 为多个环境实现矩阵构建
## 常见工作流模式
### 模式 1:测试工作流
yaml
name: Test
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run tests
run: npm test
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage/lcov.info
**参考:** 参见 `assets/test-workflow.yml`
### 模式 2:构建并推送 Docker 镜像
yaml
name: Build and Push
on:
push:
branches: [ main ]
tags: [ 'v*' ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pa