GitHub Actions 工作流文件结构 #

工作流文件是GitHub Actions的核心配置,使用YAML格式定义。本节详细介绍工作流文件的结构和语法。

文件位置 #

工作流文件必须存放在仓库的以下目录:

text
.github/
└── workflows/
    ├── ci.yml
    ├── deploy.yml
    └── release.yml

文件命名规范 #

yaml
# 推荐的命名方式
ci.yml              # 持续集成
cd.yml              # 持续部署
test.yml            # 测试工作流
release.yml         # 发布工作流
security-scan.yml   # 安全扫描

YAML基础语法 #

缩进 #

YAML使用空格缩进,不能使用Tab:

yaml
# 正确
jobs:
  build:
    runs-on: ubuntu-latest

# 错误 - 使用了Tab
jobs:
	build:
		runs-on: ubuntu-latest

键值对 #

yaml
name: My Workflow
on: push
runs-on: ubuntu-latest

列表 #

yaml
on:
  - push
  - pull_request

# 或使用内联格式
on: [push, pull_request]

多行字符串 #

yaml
# 使用 | 保留换行
run: |
  echo "Line 1"
  echo "Line 2"
  echo "Line 3"

# 使用 > 折叠换行
description: 了解GitHub Actions工作流YAML文件的结构、语法和配置选项。
  This is a long
  description that
  will be folded.

注释 #

yaml
# 这是单行注释

name: My Workflow  # 行尾注释

jobs:
  # 这是作业注释
  build:
    runs-on: ubuntu-latest

工作流完整结构 #

yaml
# 工作流名称
name: Workflow Name

# 触发器配置
on:
  push:
    branches: [main]

# 环境变量(全局)
env:
  GLOBAL_VAR: value

# 默认配置
defaults:
  run:
    shell: bash

# 并发控制
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

# 作业定义
jobs:
  job1:
    # 作业配置
    runs-on: ubuntu-latest
    steps:
      - run: echo "Hello"

name - 工作流名称 #

yaml
# 简单名称
name: CI

# 带emoji的名称
name: 🚀 Deploy

# 带描述的名称
name: "Build, Test and Deploy"

工作流名称会显示在GitHub Actions页面。

on - 触发器配置 #

单一事件 #

yaml
on: push

多个事件 #

yaml
on: [push, pull_request]

# 或
on:
  - push
  - pull_request

带条件的事件 #

yaml
on:
  push:
    branches:
      - main
      - 'release/**'
    paths:
      - 'src/**'
      - 'tests/**'
    paths-ignore:
      - '**.md'
      
  pull_request:
    branches:
      - main
    types:
      - opened
      - synchronize

定时触发 #

yaml
on:
  schedule:
    - cron: '0 0 * * *'     # 每天UTC 0点
    - cron: '0 0 * * 1'     # 每周一UTC 0点

手动触发 #

yaml
on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Environment to deploy'
        required: true
        default: 'staging'
        type: choice
        options:
          - staging
          - production
      debug:
        description: 'Enable debug mode'
        required: false
        type: boolean
        default: false

env - 环境变量 #

全局环境变量 #

yaml
env:
  NODE_VERSION: '20'
  CI: true

作业级环境变量 #

yaml
jobs:
  build:
    env:
      JOB_VAR: value

步骤级环境变量 #

yaml
steps:
  - name: Step with env
    env:
      STEP_VAR: value
    run: echo $STEP_VAR

defaults - 默认配置 #

yaml
defaults:
  run:
    shell: bash
    working-directory: ./app

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: pwd  # 在 ./app 目录执行

concurrency - 并发控制 #

yaml
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

这确保同一分支上只有一个工作流在运行,新的运行会取消旧的运行。

jobs - 作业定义 #

作业基本结构 #

yaml
jobs:
  job-id:           # 作业标识符,必须以字母或_开头
    name: Job Name  # 显示名称(可选)
    runs-on: ubuntu-latest
    steps:
      - run: echo "Hello"

作业依赖 #

yaml
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building..."
  
  test:
    needs: build    # 依赖build作业
    runs-on: ubuntu-latest
    steps:
      - run: echo "Testing..."
  
  deploy:
    needs: [build, test]  # 依赖多个作业
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying..."

条件执行 #

yaml
jobs:
  deploy:
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying to production"

矩阵构建 #

yaml
jobs:
  test:
    strategy:
      matrix:
        node: [16, 18, 20]
        os: [ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}

steps - 步骤定义 #

运行命令 #

yaml
steps:
  - name: Run single command
    run: echo "Hello"
  
  - name: Run multiple commands
    run: |
      echo "Line 1"
      echo "Line 2"

使用Action #

yaml
steps:
  - name: Checkout code
    uses: actions/checkout@v4
  
  - name: Setup Node
    uses: actions/setup-node@v4
    with:
      node-version: '20'
      cache: 'npm'

条件步骤 #

yaml
steps:
  - name: Only on main branch
    if: github.ref == 'refs/heads/main'
    run: echo "This is main branch"
  
  - name: On success
    if: success()
    run: echo "Previous steps succeeded"
  
  - name: On failure
    if: failure()
    run: echo "Previous steps failed"
  
  - name: Always run
    if: always()
    run: echo "This always runs"

环境变量 #

yaml
steps:
  - name: With environment variables
    env:
      MY_VAR: value
      ANOTHER_VAR: another
    run: |
      echo $MY_VAR
      echo $ANOTHER_VAR

工作目录 #

yaml
steps:
  - name: Run in specific directory
    working-directory: ./subdir
    run: pwd

Shell指定 #

yaml
steps:
  - name: Using bash
    run: echo $PATH
    shell: bash
  
  - name: Using PowerShell
    run: Write-Host "Hello"
    shell: pwsh
  
  - name: Using Python
    run: print("Hello")
    shell: python

outputs - 输出变量 #

作业输出 #

yaml
jobs:
  build:
    outputs:
      version: ${{ steps.get_version.outputs.version }}
    steps:
      - id: get_version
        run: echo "version=1.0.0" >> $GITHUB_OUTPUT
  
  deploy:
    needs: build
    steps:
      - run: echo "Deploying version ${{ needs.build.outputs.version }}"

步骤输出 #

yaml
steps:
  - id: set_var
    run: echo "my_var=hello" >> $GITHUB_OUTPUT
  
  - name: Use output
    run: echo "The value is ${{ steps.set_var.outputs.my_var }}"

完整示例 #

yaml
name: Complete Workflow Example

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:
    inputs:
      environment:
        description: 'Deploy environment'
        required: true
        default: 'staging'
        type: choice
        options: [staging, production]

env:
  NODE_VERSION: '20'
  CI: true

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
      - run: npm ci
      - run: npm run lint

  test:
    needs: lint
    strategy:
      matrix:
        node: [16, 18, 20]
        os: [ubuntu-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci
      - run: npm test

  build:
    needs: test
    runs-on: ubuntu-latest
    outputs:
      artifact: ${{ steps.build.outputs.artifact }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}
      - run: npm ci
      - run: npm run build
      - id: build
        run: echo "artifact=dist" >> $GITHUB_OUTPUT
      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist

  deploy:
    needs: build
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: ${{ github.event.inputs.environment || 'staging' }}
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: dist
      - name: Deploy
        run: echo "Deploying to ${{ github.event.inputs.environment || 'staging' }}"

YAML验证工具 #

在线验证 #

本地验证 #

bash
# 安装actionlint
go install github.com/rhysd/actionlint/cmd/actionlint@latest

# 验证工作流文件
actionlint .github/workflows/ci.yml

常见错误 #

1. 缩进错误 #

yaml
# 错误
jobs:
build:
runs-on: ubuntu-latest

# 正确
jobs:
  build:
    runs-on: ubuntu-latest

2. 引号问题 #

yaml
# 包含特殊字符时需要引号
run: echo "Hello: World"

# 普通字符串可以不加引号
run: echo Hello World

3. 布尔值 #

yaml
# 正确的布尔值
if: true
if: false

# 错误 - 字符串不是布尔值
if: "true"  # 这是字符串

下一步学习 #

小结 #

  • 工作流文件使用YAML格式,存放在 .github/workflows/ 目录
  • 工作流由name、on、jobs等顶级字段组成
  • 作业(jobs)包含多个步骤(steps)
  • 步骤可以运行命令或使用Action
  • 注意YAML语法,特别是缩进和引号
最后更新:2026-03-28