GitLab管道正在抛出Azure CLI错误

发布于 2025-01-19 08:12:55 字数 1369 浏览 3 评论 0 原文

我正在运行一个非常轻巧的GitLab管道,该管道执行许多Terraform配置。但是,当管道试图运行Terraform Init时,我遇到了绝对的障碍(下面的屏幕截图),而我似乎根本无法解决此问题。有什么想法吗?

此错误发生在管道阶段: validate

但是,在此之前,我还有另一个管道阶段: deploy 我能够成功地安装Azure CLI,使用以下命令:

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

因此,经过进一步的调查,事实证明,只有在i时才会发生此错误包括我的Terraform Backend.tf 文件,该文件配置了用于存储我的Terraform状态文件的Azure后端。排除此文件,一切顺利运行。我完全亏损了,因为我绝对需要Azure的状态文件。

在我看来,Azure CLI在管道上成功安装 exploy stage(上),后端配置。

以下是以下我的Backend.tf文件

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

和以下是管道中的YAML段 Deploy 我的 .gitlab-ci.yml 文件,我调用 Terraform Init >和<代码>应用。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2025-01-26 08:12:55

谢谢 cdub 将OP指向正确的方向。发布了您宝贵的讨论,以帮助其他社区成员。

如果 backend.tf 文件参考Terraform配置中的Azure资源管理器提供商,则可以移动 azure cli /em>安装步骤进入部署阶段,在该阶段,在 Terraform Init 之前,在Terraform命令中执行。

请参阅显示了在GitHub管道中安装Azure CLI的另一种方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文