🐙

GCPのリソース管理をTerraform(Cloud)とGithub Actionsで

2022/06/09に公開

https://learn.hashicorp.com/tutorials/terraform/github-actions
記事内ではRemote実行を設定して、Terraform Cloudでの実行となっているが、そうなるとGSA(GCP Service Account) Keyを作成する悩みが生まれる。

Github ActionsならOIDCでよしなにできるのに...
https://docs.github.com/ja/enterprise-cloud@latest/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-google-cloud-platform

ということで、Local実行に変更してActionsでやるのがいまいまはいいのではと落ち着きましたのでメモ

name: run terraform plan or apply
description: Describes a shared process for all projects
inputs:
  workload_identity_provider:
    description: workload_identity_provider
    required: true
  service_account:
    description: service_account for terraform
    required: true
  tf_api_token:
    description: terraform cloud api token
    required: true
  github_token:
    description: github token
    required: true    
  working_directory:
    description: working directory
    required: true    

runs:
  using: "composite"
  steps:
      - uses: google-github-actions/setup-gcloud@v0
      - uses: google-github-actions/auth@v0.4.0
        with:
          workload_identity_provider: ${{ inputs.workload_identity_provider }}
          service_account: ${{ inputs.service_account }}

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v1
        with:
          cli_config_credentials_token: ${{ inputs.tf_api_token }}

      - name: Terraform Format
        id: fmt
        shell: /usr/bin/bash -e {0}
        run: terraform fmt -check
        working-directory: ${{ inputs.working_directory }}

      - name: Terraform Init
        id: init
        shell: /usr/bin/bash -e {0}
        run: terraform init
        working-directory: ${{ inputs.working_directory }}

      - name: tflint
        uses: reviewdog/action-tflint@master
        with:
          github_token: ${{ inputs.github_token }}
          reporter: github-pr-review 
          fail_on_error: "false" 
          filter_mode: "nofilter" 
          working_directory: ${{ inputs.working_directory }}

      - name: Terraform Validate
        id: validate
        shell: /usr/bin/bash -e {0}
        run: terraform validate -no-color
        working-directory: ${{ inputs.working_directory }}

      - name: Terraform Plan
        id: plan
        shell: /usr/bin/bash -e {0}
        if: github.event_name == 'pull_request'
        run: terraform plan -no-color -input=false
        continue-on-error: true
        working-directory: ${{ inputs.working_directory }}

      - uses: actions/github-script@v6
        if: github.event_name == 'pull_request'
        env:
          PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
        with:
          github-token: ${{ inputs.github_token }}
          script: |
            const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
            #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
            #### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
            #### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
            <details><summary>Show Plan</summary>
            \`\`\`\n
            ${process.env.PLAN}
            \`\`\`
            </details>
            *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: output
            })
            
      - name: Terraform Plan Status
        if: steps.plan.outcome == 'failure'
        shell: /usr/bin/bash -e {0}
        run: exit 1

      - name: Terraform Apply
        if: github.ref == 'refs/heads/main' && github.event_name == 'push'
        shell: /usr/bin/bash -e {0}
        run: terraform apply -auto-approve -input=false
        working-directory: ${{ inputs.working_directory }}

composite化したのと、oidc周り追加したくらいで、それいがいは公式のactionsまるこぴです。

Discussion