如何使 GitHub Actions 工作流程变干

How to make a GitHub Actions workflow dry

一个工作流是否可以依赖于另一个工作流?

我目前有 2 个测试工作流程 branch.The 首先是 github_action_pull_test.yml 由 pull_request.When 触发,测试分支上有一个 pull_request 工作流程 运行 terrag运行t plan.The 第二个是 github_action_push_test.yml 当合并到我的测试分支时触发,工作流 运行 terrag运行t申请。

当前设置有一个负面影响: 我的 github_action_push_test.yml 包括 github_action_pull_test.yml 中的所有步骤。 有没有一种方法可以让我拥有一个包含所有步骤的工作流程,但只有 运行 terrag运行t 在 terrag运行t 计划被审查并合并到测试分支后才适用,这样我就可以避免重复

github_action_pull_test.yml

name: 'GitHub OIDC workflow'
on:
  pull_request:
       branches:
         - test
env:
  tf_version: 'latest'
  tg_version: 'latest'
  tf_working_dir: './testing'
permissions:
    id-token: write
    contents: read
jobs:
  deploy:
    name: 'Build and Deploy'
    runs-on: ubuntu-latest

    steps:
      - name: 'checkout'
        uses: actions/checkout@v2

      - name: configure AWS credentials
        uses: aws-actions/configure-aws-credentials@master
        with:
          aws-region: us-east-1
          role-to-assume: arn:aws:iam::123456789012:role/GitHubActions_Workflow_role
          role-duration-seconds: 3600

      - name: 'Terragrunt Init'
        uses: the-commons-project/terragrunt-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tg_actions_version: ${{ env.tg_version }}
          tf_actions_subcommand: 'init'
          tf_actions_working_dir: ${{ env.tf_working_dir }}
          tf_actions_comment: true
        env:
          TF_INPUT: false


      - name: 'Terragrunt Validate'
        uses: the-commons-project/terragrunt-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tg_actions_version: ${{ env.tg_version }}
          tf_actions_binary: 'terraform'
          tf_actions_subcommand: 'validate'
          tf_actions_working_dir: ${{ env.tf_working_dir }}
          tf_actions_comment: true

      - name: 'Terragrunt Plan'
        uses: the-commons-project/terragrunt-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tg_actions_version: ${{ env.tg_version }}
          tf_actions_subcommand: 'plan'
          tf_actions_working_dir: ${{ env.tf_working_dir }}
          tf_actions_comment: true

github_action_push_test.yml

name: 'GitHub OIDC workflow'
on:
  push:
       branches:
         - test
env:
  tf_version: 'latest'
  tg_version: 'latest'
  tf_working_dir: './testing'
permissions:
    id-token: write
    contents: read
jobs:
  deploy:
    name: 'Build and Deploy'
    runs-on: ubuntu-latest

    steps:
      - name: 'checkout'
        uses: actions/checkout@v2

      - name: configure AWS credentials
        uses: aws-actions/configure-aws-credentials@master
        with:
          aws-region: us-east-1
          role-to-assume: arn:aws:iam::123456789012:role/GitHubActions_Workflow_role
          role-duration-seconds: 3600

      - name: 'Terragrunt Init'
        uses: the-commons-project/terragrunt-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tg_actions_version: ${{ env.tg_version }}
          tf_actions_subcommand: 'init'
          tf_actions_working_dir: ${{ env.tf_working_dir }}
          tf_actions_comment: true
        env:
          TF_INPUT: false


      - name: 'Terragrunt Validate'
        uses: the-commons-project/terragrunt-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tg_actions_version: ${{ env.tg_version }}
          tf_actions_binary: 'terraform'
          tf_actions_subcommand: 'validate'
          tf_actions_working_dir: ${{ env.tf_working_dir }}
          tf_actions_comment: true

      - name: 'Terragrunt Plan'
        uses: the-commons-project/terragrunt-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tg_actions_version: ${{ env.tg_version }}
          tf_actions_subcommand: 'plan'
          tf_actions_working_dir: ${{ env.tf_working_dir }}
          tf_actions_comment: true

      - name: 'Terragrunt Apply'
        uses: the-commons-project/terragrunt-github-actions@master
        with:
          tf_actions_version: ${{ env.tf_version }}
          tg_actions_version: ${{ env.tg_version }}
          tf_actions_subcommand: 'apply'
          tf_actions_working_dir: ${{ env.tf_working_dir }}
          tf_actions_comment: true

你可以让first触发second:

name: second

on:
  workflow_run:
    workflows: [first]
    types: [completed]
    branches: [main]

jobs:
  build-second:
    name: Second
    if: github.event.workflow_run.conclusion == 'success'
    runs-on: ubuntu-latest

作业级别的 if 表达式意味着如果 first 工作流成功,build-second 将仅 运行。