无法使用gitlab CI/CD部署AK

发布于 2025-02-09 01:28:37 字数 1699 浏览 1 评论 0原文

我正在尝试使用以下gitlab管道在Azure上部署kubernetes群集,

image:
  name: hashicorp/terraform:1.2.3
  entrypoint:
    - '/usr/bin/env'
    - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'

variables:
  TF_ROOT: ${CI_PROJECT_DIR}/infrastructure
  TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${CI_PROJECT_NAME}

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

before_script:  
  - cd ${TF_ROOT}
  - rm -rf .terraform
  - terraform --version
  - terraform init

stages:
  - terraform_validate
  - terraform_plan
  - terraform_apply

terraform_validate_dev:
  stage: terraform_validate
  environment:
    name: development
  script:
    - terraform validate
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH

terraform_plan_dev:
  stage: terraform_plan
  environment:
    name: development
  script:
    - terraform plan
    - terraform plan-json
  dependencies:
    - terraform_validate_dev
  artifacts:
    name: plan deployment
    paths:
      - ${TF_ROOT}/plan.cache
    reports:
      terraform: ${TF_ROOT}/plan.json
  rules:
    - if: $CI_COMMIT_BRANCH == "development"

terraform_apply_dev:
  stage: terraform_apply
  environment:
    name: development
  script:
    - terraform apply
  dependencies:
    - terraform_plan_dev
  rules:
    - if: $CI_COMMIT_BRANCH == "development"
      when: manual

但是在Terraform_plan阶段期间,我会收到以下错误:

"Error: building AzureRM Client: please ensure you have installed Azure CLI version 2.0.79 or newer. Error parsing json result from the Azure CLI: launching Azure CLI: exec: "az": executable file not found in $PATH."

有什么想法吗?

I'm trying to deploy a Kubernetes cluster on Azure using the following GitLab pipeline

image:
  name: hashicorp/terraform:1.2.3
  entrypoint:
    - '/usr/bin/env'
    - 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'

variables:
  TF_ROOT: ${CI_PROJECT_DIR}/infrastructure
  TF_ADDRESS: ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/terraform/state/${CI_PROJECT_NAME}

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

before_script:  
  - cd ${TF_ROOT}
  - rm -rf .terraform
  - terraform --version
  - terraform init

stages:
  - terraform_validate
  - terraform_plan
  - terraform_apply

terraform_validate_dev:
  stage: terraform_validate
  environment:
    name: development
  script:
    - terraform validate
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH

terraform_plan_dev:
  stage: terraform_plan
  environment:
    name: development
  script:
    - terraform plan
    - terraform plan-json
  dependencies:
    - terraform_validate_dev
  artifacts:
    name: plan deployment
    paths:
      - ${TF_ROOT}/plan.cache
    reports:
      terraform: ${TF_ROOT}/plan.json
  rules:
    - if: $CI_COMMIT_BRANCH == "development"

terraform_apply_dev:
  stage: terraform_apply
  environment:
    name: development
  script:
    - terraform apply
  dependencies:
    - terraform_plan_dev
  rules:
    - if: $CI_COMMIT_BRANCH == "development"
      when: manual

but during the terraform_plan stage, I receive the following error:

"Error: building AzureRM Client: please ensure you have installed Azure CLI version 2.0.79 or newer. Error parsing json result from the Azure CLI: launching Azure CLI: exec: "az": executable file not found in $PATH."

Any idea?

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

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

发布评论

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

评论(2

秋凉 2025-02-16 01:28:37

最后,我能够找到问题。

不幸的是,@sytech提出的解决方案并没有解决问题,而是帮助我发现了真正的问题。

作为一个很好的做法,想要部署或使用Azure服务的自动化工具应始终使用服务主体。因此,我在Azure创建了一个服务主体,并试图将其与我的Terraform代码一起使用。
As the 文档说,要使用服务主体,我们需要创建以下环境变量:

ARM_CLIENT_ID
ARM_CLIENT_SECRET
ARM_SUBSCRIPTION_ID
ARM_TENANT_ID

一旦添加了这些环境变量,Terraform_plan阶段就可以完成其工作。

Finally, I was able to find the problem.

Unfortunately, the solution proposed by @sytech did not solve the problem but helped me discover the real problem.

As a good practice, automated tools that want to deploy or use Azure services should always use service principals. For that reason, I created a service principal in Azure and was trying to use it with my Terraform code.
As the documentation says, to use the service principal we need to create the following environment variables:

ARM_CLIENT_ID
ARM_CLIENT_SECRET
ARM_SUBSCRIPTION_ID
ARM_TENANT_ID

Once I added these environment variables the terraform_plan stage was able to complete its work.

过期以后 2025-02-16 01:28:37

正如错误消息所解释的那样,您必须安装azure cli

例如:

# ...
before_script:
  - curl -L https://aka.ms/InstallAzureCli | bash
  # ...

As the error message explains, you must install azure CLI.

For example:

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