You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GitHub Actions多Job间共享Postgres服务的services YAML配置?

复用GitHub Actions中Postgres服务配置的两种方案

针对你需要在多个Job中共享Postgres服务配置的需求,以下是利用GitHub Actions两种共享机制的具体实现方式:

方案一:可复用工作流(Reusable Workflow)

适合跨不同工作流文件共享完整的Job配置(包含services),彻底避免复制粘贴services代码。

1. 创建可复用工作流文件

在仓库的.github/workflows目录下新建reusable-postgres-job.yml,内容如下:

name: 带Postgres服务的基础Job
on:
  workflow_call:
    # 可选:定义可自定义的参数,灵活调整Postgres配置
    inputs:
      postgres-image:
        type: string
        default: postgres
      postgres-password:
        type: string
        default: postgres
      runner-os:
        type: string
        default: ubuntu-latest

jobs:
  base-postgres-job:
    runs-on: ${{ inputs.runner-os }}
    services:
      postgres:
        image: ${{ inputs.postgres-image }}
        env:
          POSTGRES_PASSWORD: ${{ inputs.postgres-password }}
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    steps:
      # 这里添加所有Job通用的步骤,比如拉取代码
      - name: 拉取代码
        uses: actions/checkout@v4

2. 在其他工作流中调用

在需要使用Postgres的工作流文件中,直接引用这个可复用工作流,并添加当前Job特有的步骤:

name: 主工作流
on: [push, pull_request]

jobs:
  integration-tests:
    uses: ./.github/workflows/reusable-postgres-job.yml
    with:
      # 可选:覆盖默认参数,比如指定Postgres版本
      postgres-image: postgres:15
    steps:
      - name: 运行集成测试
        run: npm run test:integration

  api-tests:
    uses: ./.github/workflows/reusable-postgres-job.yml
    steps:
      - name: 运行API测试
        run: npm run test:api

方案二:复合动作(Composite Action)

复合动作无法直接共享services配置,但可以封装与Postgres交互的通用步骤(比如初始化数据库、等待服务就绪),减少步骤层面的重复代码。

1. 创建复合动作文件

在仓库的.github/actions目录下新建postgres-setup/action.yml,内容如下:

name: Postgres初始化工具
description: 完成Postgres服务的就绪检查、数据库创建等通用操作
inputs:
  db-name:
    type: string
    default: test_db
  db-user:
    type: string
    default: postgres
  db-password:
    type: string
    default: postgres
runs:
  using: "composite"
  steps:
    - name: 等待Postgres服务就绪
      run: |
        until pg_isready -h localhost -p 5432 -U ${{ inputs.db-user }}; do
          echo "等待Postgres启动..."
          sleep 2
        done
      shell: bash
    - name: 创建测试数据库
      run: createdb -h localhost -p 5432 -U ${{ inputs.db-user }} ${{ inputs.db-name }}
      shell: bash
      env:
        PGPASSWORD: ${{ inputs.db-password }}

2. 在Job中使用复合动作

每个需要Postgres的Job仍需配置services,但可以直接调用复合动作完成通用步骤:

jobs:
  integration-tests:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    steps:
      - name: 拉取代码
        uses: actions/checkout@v4
      - name: 初始化Postgres
        uses: ./.github/actions/postgres-setup/action.yml
        with:
          db-name: my_test_db
      - name: 运行集成测试
        run: npm run test:integration

额外技巧:同一工作流内复用services

如果多个Job在同一个工作流文件中,可以用YAML锚点快速复用services配置:

# 定义锚点
x-postgres-service: &postgres-service
  postgres:
    image: postgres
    env:
      POSTGRES_PASSWORD: postgres
    options: >-
      --health-cmd pg_isready
      --health-interval 10s
      --health-timeout 5s
      --health-retries 5
    ports:
      - 5432:5432

jobs:
  integration-tests:
    runs-on: ubuntu-latest
    services:
      <<: *postgres-service # 引用锚点复用配置
    steps:
      ...

  unit-tests:
    runs-on: ubuntu-latest
    services:
      <<: *postgres-service # 再次复用
    steps:
      ...

内容的提问来源于stack exchange,提问作者Kannan Goundan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 20:14:51