hashicorp / setup-terraform

Sets up Terraform CLI in your GitHub Actions workflow.
https://developer.hashicorp.com/terraform/tutorials/automation/github-actions
Mozilla Public License 2.0
1.38k stars 243 forks source link

GitHub Actions - Init fails on self-hosted runner service - Error: Unexpected token '.' #445

Closed vetlekise closed 3 weeks ago

vetlekise commented 3 weeks ago

Info

Problem

Terraform init fails on GitHub self-hosted runner ONLY when its running as a service, if I run the script ./actions-runner/run.sh it works without any issues.

Error

Run terraform init -backend-config="storage_account_name=$STORAGE_ACCOUNT" -backend-config="container_name=$CONTAINER_NAME" -backend-config="resource_group_name=$RESOURCE_GROUP_NAME" -backend-config="key=$BLOB_KEY_NAME"
/home/admin/actions-runner/_work/_temp/e5807e43-00b0-46c6-ab86-1f2aaab75835/terraform:4342
      this._parser?.end()
                   ^
SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
Error: Process completed with exit code 1.

Workflow

NB! I stripped most of the workflow to only show what's necessary.

name: "Terraform"

on:
  pull_request:
    types: ["opened", "synchronize"]
    branches: ["main"]
    paths: ["terraform/**"]

jobs: 
  plan:
    runs-on: self-hosted

    defaults:
      run:
        shell: bash

    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Setup Terraform
      uses: hashicorp/setup-terraform@v3
      with:
        terraform_wrapper: true

    - name: Terraform Init
      id: init
      working-directory: ${{ vars.TF_WORKING_DIR }}
      env:
        BLOB_KEY_NAME: ${{ vars.BLOB_KEY_NAME }}
        STORAGE_ACCOUNT: ${{ vars.STORAGE_ACCOUNT }}
        CONTAINER_NAME: ${{ vars.CONTAINER_NAME }}
        RESOURCE_GROUP_NAME: ${{ vars.RESOURCE_GROUP_NAME }}
        ARM_CLIENT_ID: ${{ vars.ARM_CLIENT_ID }}
        ARM_SUBSCRIPTION_ID: ${{ vars.ARM_SUBSCRIPTION_ID }}
        ARM_TENANT_ID: ${{ vars.ARM_TENANT_ID }}
      run: terraform init -backend-config="storage_account_name=$STORAGE_ACCOUNT" -backend-config="container_name=$CONTAINER_NAME" -backend-config="resource_group_name=$RESOURCE_GROUP_NAME" -backend-config="key=$BLOB_KEY_NAME"

What's tested?

All the v3.x.x versions.

What works?

v2 works no problem.

vetlekise commented 3 weeks ago

Just found this earlier issue having the same error message and it got solved by setting the terraform_wrapper: to false.

Just tested this myself and it worked, but I unfortunately need the wrapper for the PR comment to work using the action actions/github-script@v7

vetlekise commented 3 weeks ago

Found this other issue telling them to use node 20 for terraform-setup v3. But the runner already had this installed as node -v showed me v20.18.0... I then ran sudo apt upgrade and I saw some error about a node 18 package.

I fixed it by running these commands:

sudo apt remove --purge nodejs BAD_PACKAGE
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs