【问题标题】:How to integrate terraform state into github action workflow?如何将 terraform 状态集成到 github 操作工作流程中?
【发布时间】:2022-04-21 19:23:13
【问题描述】:

我有一个 github 操作工作流,它概述了启动 terraform 以在 Azure 中创建资源的简单过程。我缺少的是如何集成 terraform 状态文件,以便在此工作流的顺序运行时,它应该将当前状态与 main.tf 文件进行比较,并且只允许净更改。目前如果我按顺序运行,第二次总是会失败,因为资源已经在 Azure 中创建了。

如何配置下面的 github 工作流以允许 terraform 状态文件比较?,我还没有找到这样做的单一来源

github repo 布局:

github 操作流程:

name: Terraform deploy to Azur

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - name: "Checkout"
      uses: actions/checkout@master
      
    - name: "Terraform Init"
      uses: hashicorp/terraform-github-actions@master
      with:
       tf_actions_version: 0.12.13
       tf_actions_subcommand: "init"

    - name: "Terraform Plan"
      uses: hashicorp/terraform-github-actions@master
      with:
       tf_actions_version: 0.12.13
       tf_actions_subcommand: "plan"
       args: -var="client_secret=${{ secrets.clientSecret }}"
             -var="client_id=${{ secrets.clientId }}"
             -var="tenant_id=${{ secrets.tenantId }}"
             -var="sub=${{ secrets.sub }}"
                  
    - name: "Terraform Apply"
      uses: hashicorp/terraform-github-actions@master
      with:
       tf_actions_version: 0.12.13
       tf_actions_subcommand: "apply"
       args: -var="client_secret=${{ secrets.clientSecret }}"
             -var="client_id=${{ secrets.clientId }}"
             -var="tenant_id=${{ secrets.tenantId }}"
             -var="sub=${{ secrets.sub }}"    

【问题讨论】:

    标签: terraform github-actions terraform-provider-azure


    【解决方案1】:

    您需要将add a backend configuration 发送到您的 Terraform,以便它将状态文件存储在外部某处,以便在每次运行时引用和更新。

    【讨论】:

    • 就是这样,之所以错过它,是因为我同时也在寻求保护后端的安全,即。没有像“access_key = hardcoded guid”这样的调用,所以我还需要阅读环境变量和存储秘密
    【解决方案2】:

    当在管道中运行时,比将后端配置存储在其他地方更好的解决方案是在 terraform init 之前即时生成后端配置:

        - name: Setup Terraform Backend
          id: backend
          run: |
            cat > backend.tf << EOF
            terraform {
              backend "remote" {
                organization = "${secrets.TF_CLOUD_ORGANIZATION}"
    
                workspaces {
                  name = "${secrets.TF_CLOUD_WORKSPACE}"
                }
              }
            }
            EOF
    
        - name: Terraform Init
          id: init
          run: terraform init
    

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 2021-04-14
      • 2021-11-09
      • 2023-04-10
      • 2017-04-16
      • 2021-04-17
      • 2022-12-03
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多