Gitlab-Ci管道未在同一工作中识别Azure CLI和Terraform

发布于 2025-01-21 17:41:59 字数 1901 浏览 4 评论 0原文

我是一个为期2/3周的新手吉特拉布(Gitlab),遇到了一个gitlab管道问题,这使我脱下头发,因为尽管在网上进行了数小时的研究,包括官方的gitlab网站,我似乎找不到一个解决。

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

第一个作业,gitlab_job1进行简单的Terraform版本检查,并确认了Terraform命令在该作业中识别的事实,并且根据我的期望,整个管道。

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

现在,我不介意在同一“准备”阶段创建第三个作业,然后我可以在其中引用我的Terraform映像(就像我在gitlab_job1中一样)并运行所需的命令,但是这里的关键问题是,此第三作业也将被要求识别或在其范围内,已安装的Azure CLI来自gitlab_job2

因此,Azure CLI和Terraform必须在同一工作中共存,或者如果保留离散的工作,则必须从其他工作中看到每个工作。我在做什么错?甚至还可以在GitLab管道作业中引用多个容器图像吗?我什至尝试过需要依赖项关键字,但无济于事。

以下是我的.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.

I am a 2/3 week old newbie to GitLab and have run into a GitLab pipeline issue which has left me tearing my hair out, as despite hours of research online, including the official GitLab website, I simply can't seem to find a resolution.

Basically, my pipeline is made up of a number of stages, with each stage requiring one or more jobs to be executed. I've called the first pipeline stage "prep" and it features two jobs - gitlab_job1 and gitlab_job2.

The first job, gitlab_job1 does a simple terraform version check and confirms the fact that the terraform command is recognised within that job and as per my expectation, the entire pipeline.

The second job, gitlab_job2 pulls down a container image of Azure CLI and installs it successfully. However, within this same job, I'm required to run a number of terraform commands (and possibly other scripts as well) and this is where my issue lies, because suddenly the pipeline job no longer appears to recognise any Terraform command. I'm assuming that referencing the Azure CLI image in that job has wiped out Terraform from the gitlab_job2's scope entirely, but I need it to recognise it here.

Now, I don't mind having to create a third job in this same "prep" stage within which I can then reference my Terraform image (like I did in gitlab_job1) and run the required commands, but the key issue here is that this 3rd job will also be required to recognise or have within its scope, the installed Azure CLI from gitlab_job2.

Azure CLI and Terraform have therefore got to co-exist within the same job or if kept in discrete jobs, each must be visible from the other job. What am I doing wrong? Is it even also possible to reference more than one container image in a GitLab pipeline job? I've even tried the Needs and Dependencies keywords, but all to no avail.

Below is a snippet of my .gitlab-ci.yml file, including some descriptive annotation to provide better clarity. Could greatly do with some help on this.

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.

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

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

发布评论

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

评论(1

后知后觉 2025-01-28 17:41:59

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

gitlab_job1工作是因为它使用hashicorp/terraform:1.1.7图像,其中包含terraform

考虑使用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.

In the job gitlab_job2 you have specified image: mcr.microsoft.com/dotnet/core/sdk:3.1 as the image for the job environment. This image does not contain terraform therefore, the command will not be found unless you first install terraform in the job.

gitlab_job1 works because it is using the hashicorp/terraform:1.1.7 image which does include terraform.

Consider using the terraform image and installing azure cli in it instead for gitlab_job2: -- or use an image with azure CLI and install terraform.

Working example:

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