【问题标题】:How to make a GitHub Actions workflow dry如何让 GitHub Actions 工作流程干涸
【发布时间】:2022-01-10 05:13:30
【问题描述】:

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

我的测试分支目前有 2 个工作流。第一个是由 pull_request 触发的 github_action_pull_test.yml。当测试分支上有 pull_request 时,工作流运行到 terragrunt 计划。第二个是 github_action_push_test.yml当我的测试分支合并时触发,工作流运行 terragrunt apply。

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

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

【问题讨论】:

  • 您是否研究过可重用的工作流程? github.blog/changelog/…
  • 您还可以将两个工作流程合二为一,并在apply 步骤中添加if: github.ref == 'refs/head/test'。这个组合的工作流将在push 上触发到任何分支。当然,除非您使用 on: pull_request 有特定原因,但是您仍然可以同时使用这两个触发器并使用 if github.event... 区分两者
  • 非常感谢您的回答..它奏效了

标签: terraform github-actions


【解决方案1】:

你可以让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 表达式意味着build-second 将仅在first 工作流成功的情况下运行。

【讨论】:

    猜你喜欢
    • 2020-04-02
    • 2022-11-02
    • 1970-01-01
    • 2020-01-18
    • 2020-04-03
    • 2022-08-13
    • 2021-07-10
    • 2020-01-09
    • 2023-01-11
    相关资源
    最近更新 更多