Skip to content

GitHub Actions CI 设置

创建 Workflow 文件

在仓库根目录创建 .github/workflows/release.yml 文件。

必需的 Permissions 配置

yaml
permissions:
  id-token: write      # 用于签名证书(OIDC token)
  contents: write      # 用于创建 Release
  attestations: write  # 用于生成 Attestation

重要

这三个权限缺一不可,否则 Attestation 功能无法正常工作。

权限说明

权限用途必需
id-token: write获取 OIDC token 用于签名
contents: write创建 Release 和上传文件
attestations: write生成并存储 Attestation

触发条件设置

推荐使用 tag push 触发发布流程:

yaml
on:
  push:
    tags:
      - 'v*'  # 匹配 v1.0.0, v2.1.3 等格式

其他触发方式

yaml
# 手动触发(用于测试)
on:
  workflow_dispatch:
    inputs:
      version:
        description: '版本号'
        required: true

# 仅在特定分支的 tag
on:
  push:
    tags:
      - 'v*'
    branches:
      - main

基础 Workflow 结构

yaml
name: Build and Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
      attestations: write

    steps:
      - uses: actions/checkout@v4

      # 你的构建步骤
      - name: Build
        run: |
          # 构建命令

      # Attestation 和 Release 步骤(见后续章节)

Runner 选择

Runner适用场景
ubuntu-latest通用构建、Node.js、Python 等
windows-latestWindows 应用、.NET 等
macos-latestmacOS 应用、iOS 开发等

对于跨平台构建,可以使用 matrix 策略:

yaml
jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    runs-on: ${{ matrix.os }}

软件安全标准文档