dflook / terraform-github-actions

GitHub actions for terraform
757 stars 152 forks source link

backend_config read as file in apply #288

Open RossComputerGuy opened 1 year ago

RossComputerGuy commented 1 year ago

Problem description

When specifying the backend_config, I get errors about expecting a file. It should work but does not seem to align with according to how the documentation is. https://github.com/dflook/terraform-github-actions/tree/main/terraform-apply#inputs

Terraform version

1.5.5

Backend

s3

Workflow YAML

name: CI

on:
  push:
    branches:
      - master

jobs:
  plan:
    name: Apply to GKE
    if: ${{ github.repository_owner == 'ExpidusOS' }}
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
      pull-requests: write
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    strategy:
      matrix:
        region: [us-west1]
    steps:
      - uses: actions/checkout@v3
      - id: 'auth'
        name: 'Authenticate to Google Cloud'
        uses: 'google-github-actions/auth@v1.1.1'
        with:
          token_format: 'access_token'
          workload_identity_provider: 'projects/769178210270/locations/global/workloadIdentityPools/ci-pool/providers/github'
          service_account: 'github-ci@expidusos-infra.iam.gserviceaccount.com'
      - id: get-gke-credentials
        uses: google-github-actions/get-gke-credentials@v1.0.2
        with:
          cluster_name: infra-${{ matrix.region }}
          location: ${{ matrix.region }}
      - name: Write variables
        run: |
          cat << EOF > terraform.tfvars
          region = "${{ matrix.region }}"
          aws_access_key_id = "${{ secrets.WASABI_ACCESS_KEY }}"
          aws_secret_access_key = "${{ secrets.WASABI_SECRET_KEY }}"
          EOF
      - name: Apply
        uses: dflook/terraform-apply@v1.36.1
        with:
          auto_approve: true
          backend_config: |
            bucket = "expidusos-ci"
            key = "infra/${{ matrix.region }}/terraform.tfstate"
            endpoint = "s3.us-west-1.wasabisys.com"
            region = "us-west-1"
            access_key = ${{ secrets.WASABI_ACCESS_KEY }}
            secret_key = ${{ secrets.WASABI_SECRET_KEY }}

Workflow log

Installing Terraform
  gpg: Signature made Tue Apr 19 07:39:49 2022 UTC
  gpg:                using RSA key 374EC75B485913604A831CC7C820C6D5CD27AB87
  gpg: Good signature from "HashiCorp Security (hashicorp.com/security) <security@hashicorp.com>" [ultimate]
  terraform_0.9.1_linux_amd64.zip: OK
  gpg: Signature made Wed Aug  9 13:10:01 2023 UTC
  gpg:                using RSA key 374EC75B485913604A831CC7C820C6D5CD27AB87
  gpg: Good signature from "HashiCorp Security (hashicorp.com/security) <security@hashicorp.com>" [ultimate]
  terraform_1.5.5_linux_amd64.zip: OK
  Terraform version not specified, using the latest version
  Switched to Terraform v1.5.5
  Detected s3 backend
Initializing Terraform

  Initializing the backend...
  ╷
  │ Error: Failed to read file
  │ 
  │ The file "bucket" could not be read.
  ╵

  ╷
  │ Error: Invalid backend configuration argument
  │ 
  │ The backend configuration argument "" given on the command line is not
  │ expected for the selected backend type.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "\"expidusos-ci\"" could not be read.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "key" could not be read.
  ╵

  ╷
  │ Error: Invalid backend configuration argument
  │ 
  │ The backend configuration argument "" given on the command line is not
  │ expected for the selected backend type.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "\"infra/us-west1/terraform.tfstate\"" could not be read.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "endpoint" could not be read.
  ╵

  ╷
  │ Error: Invalid backend configuration argument
  │ 
  │ The backend configuration argument "" given on the command line is not
  │ expected for the selected backend type.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "\"s3.us-west-1.wasabisys.com\"" could not be read.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "region" could not be read.
  ╵

  ╷
  │ Error: Invalid backend configuration argument
  │ 
  │ The backend configuration argument "" given on the command line is not
  │ expected for the selected backend type.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "\"us-west-1\"" could not be read.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "access_key" could not be read.
  ╵

  ╷
  │ Error: Invalid backend configuration argument
  │ 
  │ The backend configuration argument "" given on the command line is not
  │ expected for the selected backend type.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "***" could not be read.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "secret_key" could not be read.
  ╵

  ╷
  │ Error: Invalid backend configuration argument
  │ 
  │ The backend configuration argument "" given on the command line is not
  │ expected for the selected backend type.
  ╵

  ╷
  │ Error: Failed to read file
  │ 
  │ The file "***" could not be read.
  ╵
flipcc commented 1 week ago

Encountering the same issue. @RossComputerGuy did you find a workaround?

dflook commented 1 week ago

Just a hunch, but are there spaces in the backend config? e.g.

backend_config: |
  bucket = "my-config"

And does it work if the spaces are removed, like this:

backend_config: |
  bucket="my-config"
flipcc commented 1 week ago

I have tried that, it works. Thank you very much for helping out @dflook!