GitLab:所选阶段不存在

dan*_*rts 6 continuous-integration gitlab gitlab-ci

我试图将一个相当复杂的管道与在不同环境中按顺序运行的多个作业组合在一起。这是为了在我们的基础设施上运行 Terraform 更改。作业序列应该在我们的infraci环境中自动运行,该环境仅通过 CI 推出,然后停止并需要单击按钮才能开始部署到具有实际(尽管是开发)用户的开发环境。当然,我不想一遍又一遍地编写相同的代码,因此我尝试尽可能保持干燥。这是我的gitlab-ci.yml

---
# "variables" & "default" are used by all jobs
variables:
  TF_ROOT: '${CI_PROJECT_DIR}/terraform'
  TF_CLI_CONFIG_FILE: .terraformrc
  AWS_STS_REGIONAL_ENDPOINTS: regional
  AWS_DEFAULT_REGION: eu-west-2
  ASG_MODULE_PATH: module.aws_asg.aws_autoscaling_group.main_asg

default:
  image:
    name: hashicorp/terraform:light
    entrypoint: ['']
  cache:
    paths:
      - ${TF_ROOT}/.terraform
  tags:
    - nonlive # This tag matches the group wide GitLab runner.
  before_script:
    - cd ${TF_ROOT}

# List of all stages (jobs within the same stage are executed concurrently)
stages:
  - init
  - infraci_plan
  - infraci_taint
  - infraci_apply
  - dev_plan
  - dev_taint
  - dev_apply

# "Hidden" jobs we use as templates to improve code reuse.
.default:
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

.plan:
  extends: .default
  stage: ${CI_ENVIRONMENT_NAME}_plan
  script:
    - terraform workspace select ${CI_ENVIRONMENT_NAME}
    - terraform plan
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_ENVIRONMENT_NAME != "infraci"'
      when: manual
      allow_failure: false

.taint:
  extends: .default
  stage: ${CI_ENVIRONMENT_NAME}_taint
  script: terrafrom taint ${ASG_MODULE_PATH}
  needs:
    - ${CI_ENVIRONMENT_NAME}_plan

.apply:
  extends: .default
  stage: ${CI_ENVIRONMENT_NAME}_apply
  script: terraform apply -auto-approve

# Create actual jobs
## init - runs once per pipeline
init:
  stage: init
  script: terraform init
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "web"'
      when: manual
  
## infraci - auto deploy
infraci_plan:
  extends: .plan
  environment:
    name: infraci

infraci_taint:
  extends: .taint
  environment:
    name: infraci

infraci_apply:
  extends: .apply
  environment:
    name: infraci

## dev - manual deployment
dev_plan:
  extends: .plan
  environment:
    name: dev

dev_taint:
  extends: .taint
  environment:
    name: dev

dev_apply:
  extends: .apply
  environment:
    name: dev
Run Code Online (Sandbox Code Playgroud)

不幸的是,验证失败并出现以下错误:

infraci_plan job: chosen stage does not exist; available stages are .pre, init, infraci_plan, infraci_taint, infraci_apply, dev_plan, dev_taint, dev_apply, .post
Run Code Online (Sandbox Code Playgroud)

我的假设是,这与在隐藏作业中进行插值有关CI_ENVIRONMENT_NAME,但在实际定义作业的作业之前并不实际设置该值。

如果是这种情况,那么有什么方法可以在不进行大量重复的情况下获得我需要的设置呢?

dan*_*elz 1

你是对的,不可能在 中使用变量stage。我认为您需要直接在工作中定义阶段并在.plan.

infraci_plan:
  extends: .plan
  stage: infraci_plan
  environment:
    name: infraci
Run Code Online (Sandbox Code Playgroud)