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 242 forks source link

/usr/bin/env: 'node' no such file or directory when terraform_wrapper is set to true #84

Open dimisjim opened 3 years ago

dimisjim commented 3 years ago

Hey there,

I tried using setup-terraform with terraform_wrapper se to true, but getting

image

I am running this in a self-hosted, dockerized runner. I have tried installing node in the container before running the action but it still fails. I have also tried to install node from within the workflow yaml, but still getting the same.

I have resorted to disable the terraform_wrapper, which then makes the whole workflow run fine.

Could you look into it?

daroga0002 commented 3 years ago

I have exactly same issue

daroga0002 commented 3 years ago

I have found solution to this. Running on self runner you must check availability of node binary in: /usr/local/bin/node

in my Ubuntu case following helped:

apt update
apt install nodejs
ln -s /usr/bin/nodejs /usr/local/bin/node
buttsp commented 3 years ago

@daroga0002 i did the above you suggested, but then i get the error at Terraform init:

failed 4 minutes ago in 7s

Run hashicorp/setup-terraform@v1
  with:
    cli_config_credentials_hostname: app.terraform.io
    terraform_version: latest
    terraform_wrapper: true
  env:
    ARM_CLIENT_ID: ***
    ARM_CLIENT_SECRET: ***
    ARM_SUBSCRIPTION_ID: ***
    ARM_TENANT_ID: ***
    TF_VAR_location: westeurope
    TF_VAR_resource_name: ***-devops-dev
    TF_VAR_sql_user: ***
    TF_VAR_sql_password: ***
/usr/bin/unzip -q /home/github-runner-elsa/actions-runner/_work/_temp/fe1b5c37-f386-4451-a746-9fcd852091b0
0s
**Run terraform -chdir=./src/infra init**
  terraform -chdir=./src/infra init
  shell: /bin/bash -e {0}
  env:
    ARM_CLIENT_ID: ***
    ARM_CLIENT_SECRET: ***
    ARM_SUBSCRIPTION_ID: ***
    ARM_TENANT_ID: ***
    TF_VAR_location: westeurope
    TF_VAR_resource_name: ***-devops-dev
    TF_VAR_sql_user: ***
    TF_VAR_sql_password: ***
    TERRAFORM_CLI_PATH: /home/github-runner-elsa/actions-runner/_work/_temp/xxxxxx-d062dfaaf7b7
/home/github-runner-elsa/actions-runner/_work/_temp/xxxxx-062dfaaf7b7/terraform:75
**function cp(source, dest, options = {}) {**
                                  ^

**SyntaxError: Unexpected token =**
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3
Error: Process completed with exit code 1.
daroga0002 commented 3 years ago

as per me one of your variables seems having some special character what is causing that it escape from variable and breaking action nodejs code

buttsp commented 3 years ago

@daroga0002 that was indeed the problem in the end! The variable had a special character, which i have now removed and the pipeline works fine!

jwenz723 commented 3 years ago

I experienced this same issue and resolved the issue by adding the following into my workflow:

steps:
# https://github.com/actions/setup-node
- uses: actions/setup-node@v2
  with:
    node-version: '14'
jimrazmus commented 3 years ago

Possibly related issue: https://github.com/actions/setup-node/issues/224

sr-apotapov commented 2 years ago

We have exactly same issue

luka5 commented 11 months ago

I did run just run into the same issue. As mentioned in the README.md: NodeJS must be previously installed with the version specified in the [action.yml](https://github.com/hashicorp/setup-terraform/blob/main/action.yml).. This is caused by the fact, that terraform is not the binary but a NodeJS wrapper.

You can install NodeJS in your runner environment, or you can run the setup-node action. But actually, NodeJS is already installed. The setup-terraform action runs on node20 provided by the runner as an external.

To utilize this runner external, you need to determine its path. For the https://ghcr.io/actions/actions-runner image it is /home/runner/externals/node20/bin/. Then export and extend the PATH variable in the step where you run terraform:

export PATH="${PATH}:/home/runner/externals/node20/bin/"

This could actually by an idea for the terraform wrapper here. If you somehow manage to get the path of the node external, you could automatically utilize this NodeJS installation.

younsl commented 4 months ago

@dimisjim Hi, folks. I experienced this same issue and resolved the issue by pre-installing node.js in self-hosted actions-runner pod.


Issue

The setup-terraform step runs successfully, but the workflow fails during the terraform init process with the following error message:

/usr/bin/env: 'node': No such file or directory

Environment

Background

Prerequisite for setup-terraform

For running setup-terraform on self-hosted GitHub Actions runners, it is necessary to have NodeJS installed. This is mentioned in the official setup-terraform documentation:

When running on self-hosted GitHub Actions runners, NodeJS must be previously installed with the version specified in the action.yml.

Make sure to install NodeJS with the version specified in the action.yml to ensure proper execution.

Solution

I resolved the issue by referring to this comment.

The actions-runner executing the setup-terraform action needs to have the node command installed to use terraform commands correctly. For Terraform CLI v1.2.0, include a step to install Node.js version 20 using the setup-node action.

Here is my working setup-terraform code:

jobs:
  terraform:
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    # When running on self-hosted GitHub Actions runners,
    # NodeJS must be previously installed with the version specified in the action.yml.
    - uses: actions/setup-node@v2
      with:
        node-version: '20'

    - name: Setup terraform
      uses: actions/setup-terraform@v3
      with:
        terraform_version: '1.2.0'

    - name: Configure AWS Credentials
      uses: actions/configure-aws-credentials@v4
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: 'ap-northeast-2'

    - name: Terraform fmt
      id: fmt
      run: terraform fmt -check
      continue-on-error: true

    - name: Terraform init
      id: init
      run: terraform init