user971741
user971741

Reputation:

terraform: command not found in gitlab CI environment

I have following CI configurations:

variables:
  TF_ROOT: ${CI_PROJECT_DIR}
  TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${CI_PROJECT_NAME}
  TF_CLI_CONFIG_FILE: $CI_PROJECT_DIR/.terraformrc

default:
  image: hashicorp/terraform:light

cache:
  key: ${CI_PROJECT_NAME}
  paths:
    - ${TF_ROOT}/.terraform


before_script:
  - echo -e "credentials \"$CI_SERVER_HOST\" {\n  token = \"$CI_JOB_TOKEN\"\n}" > $TF_CLI_CONFIG_FILE
  - cd ${TF_ROOT}
  - export TF_LOG_CORE=TRACE
  - export TF_LOG_PATH=terraform_logs.txt

stages:
  - prepare

init:
  stage: prepare
  script:
    - terraform -v
    - terraform init

But at prepare stage i get:

$ terraform -v
bash: line 135: terraform: command not found

My understanding is by using hashicorp/terraform:light as base image, i get terraform available throughout the runner environment but that doesn't seems to be the case.

Can anyone correct me what am i doing wrong?

Upvotes: 0

Views: 5081

Answers (3)

user971741
user971741

Reputation:

the issue was the gitlab-runner was configured to be a shell executor. Which makes the whole image: hashicorp/terraform:light part redundant and terraform should be installed on the host machine itself where runner is being executed.

Upvotes: 0

Tolis Gerodimos
Tolis Gerodimos

Reputation: 4400

My understanding is by using hashicorp/terraform:light as base image, i get terraform available throughout the runner environment but that doesn't seems to be the case

But in the code you are using gitlab-terraform and it seems that is not available in hashicorp/terraform:light image.

Try the following image, from the gitlab registry

default:
  image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest

If you want to use terraform command instead add the following

default:
  image:
    name: hashicorp/terraform:light
    entrypoint:
      - /usr/bin/env
      - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Upvotes: 0

Tapan Hegde
Tapan Hegde

Reputation: 1328

I guess you have to use 'latest' image instead of 'light'. Update 'default' section with below Content. It is having entrypoint details with PATH initialized.

default:
  image:
    name: hashicorp/terraform:latest
    entrypoint:
      - /usr/bin/env
      - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Reference : https://matavelli.io/posts/2020/01/setup-gitlab-ci-with-terraform/

Upvotes: 0

Related Questions