Pulumi 与 GitHub Actions 使并行工作流崩溃并出现错误:[409] 冲突:另一个更新正在进行中 例如与翻新

问题描述

将 GitHub Actions 与 Pulumi 结合使用是一次很棒的体验,因为 the good Actions provided。但我倾向于遇到问题,其中多个 GitHub Action 工作流并行运行(例如,当配置 renovate 并尝试更新存储库依赖项时)。因此,要么第一个工作流程获胜并完成它的工作 - 而其他工作流程则失败。或者每个工作流都失败(这也取决于 GitHub Action 工作流设计)。然后我得到这样的错误(见 the full log here):

#### :tropical_drink: `pulumi --non-interactive up`
Previewing update (dev)

View Live: https://app.pulumi.com/jonashackt/scmbreakoutpulumi/dev/previews/fbf45825-5d8f-45bc-ad3e-c55b7576313e


    pulumi:pulumi:Stack scmbreakoutpulumi-dev running 
    azure:core:ResourceGroup scm-breakout-rg-pulumi  
    azure:storage:Account scmbreakresources  
    azure:appservice:Plan asp-scmbreakoutrg  
    azure:storage:Container rawimages  
    azure:storage:Queue thumbnails  
    azure:storage:Container thumbnails  
 +  azure:appservice:AppService scmContactsApi create 
 +  azure:appservice:AppService scmResourceApi create 
 +  azure:appservice:FunctionApp scmFunctionApp create 
 +  azure:appservice:Slot scmResourceApiStg create 
    pulumi:pulumi:Stack scmbreakoutpulumi-dev  
 
Resources:
    + 4 to create
    7 unchanged

Updating (dev)

error: [409] Conflict: Another update is currently in progress.
To learn more about possible reasons and resolution,visit https://www.pulumi.com/docs/troubleshooting/#conflict

解决方法

日志已经引出了一个很好的资源:https://www.pulumi.com/docs/troubleshooting/#conflict其实是app.pulumi.com提供的Pulumi状态管理的一个功能:

pulumi.com 提供的服务之一是并发控制。 该服务将允许最多一个用户更新特定堆栈 一次。

所以只使用一个 Stack,就像 app.pulumi.com 上的默认 dev 看起来像这样:

enter image description here

使用 GitHub Actions 或其他 CI/CD 平台,这会成为一个障碍。我在这里看到 2 个选项:我们可以切换到另一个 Pulumi 状态管理后端(例如 the Local Filesystem Backend,它不会在 app.pulumi.com 上创建堆栈,而是在本地创建 CI)。或者,我们可以在 app.pulumi.com 上创建一个 GitHub Action 作业特定堆栈,其中堆栈以特定作业 ID 或其他名称命名。

因为我不介意在这里使用 app.pulumi.com - 并且还使用附加日志,如果出现问题 - 我想为第二个选项提供解决方案。 GitHub Action 工作流文件设计可以通过以下步骤进行描述:

  1. 标准 Pulumi GitHub Action 管道:定义所需变量、检查存储库、设置 nodejs 环境,包括。安装 npm 依赖项 - 最后使用 action-install-pulumi-cli 操作配置 Pulumi CLI。
  2. 使用类似于 the github.run_id GitHub Action default context variablepulumi stack init github-${{ github.run_id }} 在 app.pulumi.com 上创建 Pulumi 堆栈。此变量表示“存储库中每次运行的唯一编号。”
  3. 利用版本 v2 中的(或多个)pulumi/actions@v2 操作(因为只有 from v2 we have the stack-name configuration option)并使用 stack-name: github-${{ github.run_id }} 配置 Pulumi app.pulumi.com 堆栈名称立>
  4. 使用最终 pulumi stack rm github-${{ github.run_id }} -y
  5. 删除 Pulumi app.pulumi.com 堆栈

完整的 GitHub Action 工作流程如下所示:

name: pulumi-preview-up

on: [push]

env:
  ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
  ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
  ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
  ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
  PULUMI_ACCESS_TOKEN: ${{ secrets.PULUMI_ACCESS_TOKEN }}

jobs:
  preview-up-destroy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: In order to use the Pulumi v2 action,we need to setup the Pulumi project specific language environment
        uses: actions/setup-node@v2
        with:
          node-version: '14'

      - name: After setting up the Pulumi project specific language environment,we need to install the dependencies also (see https://github.com/pulumi/actions#example-workflows)
        run: npm install

      - name: Install Pulumi CLI so that we can create a GHA pipeline specific Pulumi Stack
        uses: pulumi/action-install-pulumi-cli@v1.0.1

      - name: Create GHA pipeline specific Pulumi Stack incl. Azure location
        run: |
          pulumi stack init github-${{ github.run_id }}
          pulumi config set azure:location WestEurope

      - uses: pulumi/actions@v2
        with:
          command: preview
          stack-name: github-${{ github.run_id }}

      - uses: pulumi/actions@v2
        with:
          command: up
          stack-name: github-${{ github.run_id }}

      - uses: pulumi/actions@v2
        with:
          command: destroy
          stack-name: github-${{ github.run_id }}

      - name: Remove the GHA pipeline specific Pulumi Stack
        run: |
          pulumi stack rm github-${{ github.run_id }} -y

现在,当并行运行多个 GitHub Actions 工作流时,app.pulumi.com 概述也如下所示:

enter image description here