如何让 gitlab 管道停止并要求我输入变量?

j4n*_*53n 11 gitlab gitlab-ci

我在 gitlab 中创建了一个管道,其中

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

variables:
  PLAN: dbrest.tfplan
  STATE: dbrest.tfstate

cache:
  paths:
    - .terraform

before_script:
  - terraform --version
  - terraform init

stages:
  - validate
  - build
  - deploy
  - destroy

validate:
  stage: validate
  script:
    - terraform validate

plan:
  stage: build
  script:
    - terraform plan -state=$STATE -out=$PLAN
  artifacts:
    name: plan
    paths:
      - $PLAN
      - $STATE

apply:
  stage: deploy
  environment:
    name: production
  script:
    - terraform apply -state=$STATE -input=false $PLAN
    - terraform state show aws_instance.bastion
  dependencies:
    - plan
  when: manual
  only:
    - master

destroy:
    stage: destroy
    environment:
      name: production
    script:
      - terraform destroy -state=$STATE -auto-approve
    dependencies:
      - apply
    when: manual
    only:
      - master
Run Code Online (Sandbox Code Playgroud)

我还在“设置”下创建了一个变量。-> 'CI/CD' -> '变量' - 我的印象是,当我进入手动阶段时deploy,gitlab 应该暂停并要求我输入该变量的值,但这并没有发生 - 缺少什么?

MrB*_*rta 11

when: manual当您手动触发管道时,您已将作业与 to 混合在一起。这就是你想要的:

https://docs.gitlab.com/ee/ci/pipelines/#run-a-pipeline-manually

您可以将其与only某些变量一起使用。就像是:

...
apply:
  stage: deploy
  environment:
    name: production
  script:
    - terraform apply -state=$STATE -input=false $PLAN
    - terraform state show aws_instance.bastion
  dependencies:
    - plan
  only:
    refs:
      - master
    variables:
      - $RELEASE == "yes" 

destroy:
    stage: destroy
    environment:
      name: production
    script:
      - terraform destroy -state=$STATE -auto-approve
    dependencies:
      - apply
    only:
      refs:
        - master
      variables:
        - $RELEASE == "yes" 
Run Code Online (Sandbox Code Playgroud)

有了这样的东西,您可以拥有永远不会正常运行的作业,但前提是您在 master 分支上手动启动一个新管道并将变量设置$RELEASEyes. 我还没有测试过这个,所以如果它不起作用,我很抱歉!