terraform 在空目录中初始化

terraform is initialized in an empty directory

我试图让 terraform 在特定的根目录中执行 terraform init,但管道无法识别它。我的 gitlab-ci.yml 文件的结构可能有问题吗?我已经尝试将所有内容移动到根目录,效果很好,但我希望在存储库中有一些文件夹结构,以使其对未来的开发人员更具可读性。

default:
  tags:
    - aws

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

variables:
  # If not using GitLab's HTTP backend, remove this line and specify TF_HTTP_* variables
  TF_STATE_NAME: default
  TF_CACHE_KEY: default
  # If your terraform files are in a subdirectory, set TF_ROOT accordingly
  TF_ROOT: ./src/envs/infrastruktur

before_script:
  - rm -rf .terraform
  - terraform --version
  - export AWS_ACCESS_KEY_ID
  - export AWS_ROLE_ARN
  - export AWS_DEFAULT_REGION
  - export AWS_ROLE_ARN

stages:
  - init
  - validate
  - plan
  - pre-apply

init:
  stage: init
  script:
      - terraform init

在验证阶段之前一切都很好,但是一旦管道进入计划阶段,它就说找不到任何配置文件。

validate:
  stage: validate
  script:
    - terraform validate

plan:
  stage: plan
  script:
    - terraform plan -out "planfile"
  dependencies:
    - validate
  artifacts:
    paths:
      - planfile

apply:
  stage: pre-apply
  script:
    - terraform pre-apply -input=false "planfile"
  dependencies:
    - plan
  when: manual

您需要在每个作业中 cd 到您的配置文件夹中,并且在每个作业之后您需要通过 artifacts 将 terraform 正在运行的 /src/envs/infrastruktur 的内容传递到下一个作业。为简洁起见,我省略了管道的其余部分。

before_script:
  - rm -rf .terraform
  - terraform --version
  - cd $TF_ROOT
  - export AWS_ACCESS_KEY_ID
  - export AWS_ROLE_ARN
  - export AWS_DEFAULT_REGION
  - export AWS_ROLE_ARN

stages:
  - init
  - validate
  - plan
  - pre-apply

init:
  stage: init
  script:
      - terraform init
  artifacts:
    paths:
      - $TF_ROOT

validate:
  stage: validate
  script:
    - terraform validate
  artifacts:
    paths:
      - $TF_ROOT

plan:
  stage: plan
  script:
    - terraform plan -out "planfile"
  dependencies:
    - validate
  artifacts:
    paths:
      - planfile
      - $TF_ROOT