繁体   English   中英

GitLab-CI 管道在同一作业中无法识别 Azure CLI 和 Terraform

[英]GitLab-CI Pipeline Not Recognizing Azure CLI and Terraform in Same Job

我是 GitLab 的 2/3 周大的新手,遇到了 GitLab 管道问题,这让我焦头烂额,尽管在网上进行了数小时的研究,包括官方 GitLab 网站,但我似乎根本找不到解析度。

基本上,我的管道由多个阶段组成,每个阶段都需要执行一个或多个作业。 我将第一个管道阶段称为“准备”,它有两个作业—— gitlab_job1gitlab_job2

第一项工作gitlab_job1执行简单的 terraform 版本检查并确认 terraform 命令在该工作中被识别,并且按照我的预期,整个管道。

第二个作业, gitlab_job2拉取一个Azure CLI的容器镜像,安装成功。 但是,在同一项工作中,我需要运行许多 terraform 命令(可能还有其他脚本),这就是我的问题所在,因为管道工作突然似乎不再识别任何 Terraform 命令。 我假设在该作业中引用 Azure CLI 图像已经从 gitlab_job2 的 scope 中完全消除了 Terraform,但我需要它在这里识别它。

现在,我不介意必须在同一个“准备”阶段创建第三个作业,然后我可以在其中引用我的 Terraform 图像(就像我在gitlab_job1中所做的那样)并运行所需的命令,但这里的关键问题是第三份工作也需要识别或在其 scope 中安装来自gitlab_job2的 Azure CLI。

因此,Azure CLI 和 Terraform 必须在同一个作业中共存,或者如果保留在不同的作业中,则每个作业都必须在另一个作业中可见。 我究竟做错了什么? 是否也可以在 GitLab 管道作业中引用多个容器镜像? 我什至尝试过NeedsDependencies关键字,但都无济于事。

下面是 my.gitlab-ci.yml 文件的片段,包括一些描述性注释以提供更好的清晰度。 在这方面可以提供一些帮助。

default:
  image: 
    name: hashicorp/terraform:1.1.7
    entrypoint: [""]

stages:
  - prep
  - build
  - deploy  

gitlab_job1:
  stage: prep
  inherit:
    default: true
  script:
    - echo "This job successfully confirms the inherited Terraform version as 1.1.7" 
    - terraform --version

gitlab_job2:
   stage: prep
   inherit:
    default: true
   image: mcr.microsoft.com/dotnet/core/sdk:3.1

   script:
     - echo "This job will install the Azure cli."
     - curl -sL https://aka.ms/InstallAzureCLIDeb | bash
     - az --version   # Verify cli version after the install

     # This next command below should then run a basic Terraform validation, but throws the error directly below it.

     - terraform --validate 
     # Error msg displayed: - '/bin/bash line 139':' terraform':' command not found.

     # Other terraform commands follow the above validation command, but also fail as a result of the same error as above.

在作业gitlab_job2 ,您已指定image: mcr.microsoft.com/do.net/core/sdk:3.1作为作业环境的图像。 此映像不包含terraform ,因此除非您首先在作业中安装 terraform,否则将找不到该命令。

gitlab_job1有效,是因为它使用的是包含terraformhashicorp/terraform:1.1.7图像。

考虑使用 terraform 图像并在其中安装 azure cli 而不是gitlab_job2: -- 或者使用带有 azure CLI 的图像并安装 terraform。

工作示例:

gitlab_job2:
   stage: prep
   # use Azure CLI image as a starting point
   image: mcr.microsoft.com/azure-cli

   script:
     - az --version   # CLI is included in image

     # install Terraform
     - curl -o ./terraform-bin.zip https://releases.hashicorp.com/terraform/1.1.8/terraform_1.1.8_linux_amd64.zip
     - unzip ./terraform-bin.zip -d /usr/local/bin
     - rm ./terraform-bin.zip
     
     - terraform version # terraform is now installed!
     #... now you can use azure cli and terraform together.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM