【问题标题】:GitLab CI/CD - Change Working DirectoryGitLab CI/CD - 更改工作目录
【发布时间】:2018-10-03 21:34:33
【问题描述】:

我正在尝试为 Terraform 实现 CI/CD 管道,但是我需要在运行任何其他命令之前更改 pwd。

Terraform 容器似乎只接受 Terraform 命令,因此无法识别 cd/ls/echo $PATH。我是否需要构建一个支持这个并使用那个的图像,或者我可以使用维护的 Terraform 图像?

我尝试更改 ENTRYPOINT,但由于 Alpine 映像,似乎只有最小的安装可用。

【问题讨论】:

标签: continuous-integration gitlab terraform


【解决方案1】:

您可以将入口点更改为 /usr/bin/env 以运行 cd 命令。 gitlab-ci 示例:

validate_terraform:
  image:
    name: hashicorp/terraform:light
    entrypoint: ["/usr/bin/env"]
  stage: test

  script:
    - cd infrastructure/
    - terraform init
    - terraform validate

【讨论】:

  • 入口点的正确语法是:entrypoint: - "/usr/bin/env",因为它需要一个字符串数组
  • entrypoint 很好。括号在 yaml 中用于指定数组
【解决方案2】:

您可以在 .gitlab-ci.yml 中使用像 alpine 这样的通用图像,然后在脚本中获取 Terraform,例如:

wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_386.zip
unzip terraform*

并像这样使用它:

./terraform [...]

【讨论】:

    【解决方案3】:

    有点晚了,但我今天遇到了同样的问题。我发现以下帖子帮助我解决了问题:
    Is there any way to have Terraform CLI runs scripts in a directory other than PWD?

    所以我在我的 gitlab-ci 中使用了 chdir 标志:

    - terraform -chdir=infrastructure init
    - terraform -chdir=infrastructure validate
      ...
    

    文档:https://www.terraform.io/cli/commands#switching-working-directory-with-chdir

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2022-08-10
      • 2022-07-27
      • 1970-01-01
      • 2021-12-28
      • 2021-04-15
      • 2021-10-14
      • 2021-10-14
      • 1970-01-01
      相关资源
      最近更新 更多