简体   繁体   中英

Terratest in GitHub Action fails

when running terratest locally with an assertion it works as expected. But when trying to run it via a github action i get the error below (removing the assertion has terratest running as expected):

TestTerraform 2022-07-27T08:34:49Z logger.go:66: ::set-output name=exitcode::0
    output.go:19: 
            Error Trace:    output.go:19
                                        terraform_test.go:24
            Error:          Received unexpected error:
                            invalid character 'c' looking for beginning of value
            Test:           TestTerraform

The terratest file looks like:

package test

import (
    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
    "testing"
)

func TestTerraform(t *testing.T) {

    iam_arn_expected := "arn:aws:iam::xxx:role/terratest_iam"
    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples/simple",
        Vars: map[string]interface{}{
            "iam_name": "terratest_iam",
            "region":   "eu-west-2",
        },
    })

    defer terraform.Destroy(t, terraformOptions)

    terraform.InitAndApply(t, terraformOptions)

    iam_arn := terraform.Output(t, terraformOptions, "iam_arn")

    assert.Equal(t, iam_arn_expected, iam_arn)
}

the github action looks like:

jobs:
  terratest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-go@v3.2.1
      - name: Install tfenv
        run: |
          git clone https://github.com/tfutils/tfenv.git ~/.tfenv
          echo "$HOME/.tfenv/bin" >> $GITHUB_PATH
      - name: Install Terraform
        working-directory: .
        run: |
          pwd
          tfenv install
          terraform --version
      - name: Download Go Modules
        working-directory: test
        run: go mod download
      - name: Run Terratest
        working-directory: test
        run: go test -v
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_PLAYGROUND_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_PLAYGROUND_KEY }}

The solution is to use the HashiCorp setup-terraform action with the Terraform wrapper turned off in place of your "Install Terraform" step.

  - name: Setup Terraform
    uses: hashicorp/setup-terraform@v2
    with:
      terraform_version: 1.3.6
      terraform_wrapper: false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM