GitLab Pipeline is throwing an Azure CLI error

487 views Asked by At

I'm running a very lightweight GitLab Pipeline which executes a number of Terraform configuration. However, I have hit an absolute roadblock as the pipeline throws an Azure CLI error (screenshot below) when it attempts to run a Terraform init and I simply can't seem to resolve this. Any ideas?

enter image description here

This error happens at the pipeline stage: validate.

Prior to that however, I have another pipeline stage: deploy where I am able to install Azure CLI successfully, using the below commands:

deploy:
  stage: deploy
  image: mcr.microsoft.com/dotnet/core/sdk:3.1
  script:
    - curl -sL https://aka.ms/InstallAzureCLIDeb | bash

So after some further investigation, it turns out that this error only occurs when I include my terraform backend.tf file which configures an Azure backend for storing my terraform state file. Exclude this file and everything runs smoothly. I'm at a complete loss, as I definitely require that state file in Azure.

It does appear to me that the Azure CLI successful install at the pipeline deploy stage (above) isn't picked up by the Backend.tf configuration.

Below is the content of my Backend.tf file

terraform {
  backend "azurerm" {
    resource_group_name   = "rg_xxx"
    storage_account_name  = "stxxxxtfstate"
    container_name        = "terraform"
    key                   = "terraform.tfstate"
  }
}

And below is the YAML snippet from the pipeline deploy stage of my .gitlab-ci.yml file where I call terraform init and apply.

deploy:
  stage: deploy
  script:  
    - terraform init
    - terraform plan -var-file=dev-settings.tfvars -out=plan.out
    - terraform apply -auto-approve plan.out
1

There are 1 answers

0
Delliganesh Sevanesan On

Thank You Cdub for pointing the OP in the right direction. Posted your valuable discussion as an Answer to help other community members.

If the backend.tf file references the Azure resource manager provider from the Terraform Configuration, then it's possible to move the Azure CLI installation step into the deploy stage where the terraform commands are being executed right before the terraform init.

Please refer to terraform-gitlab-image-no-azure-cli which shows another way to install Azure CLI in a GitHub Pipeline.