Skip to content

Support variable expansion on environment:auto_stop_in

Release Notes

With this update, you can now use a variable when specifying the lifetime of an environment.

Problem

environment:auto_stop_in keyword doesn't support variable expansion. We should support it.

Use case

#264198 (comment 849058703)

.deploy_my_cluster: &deploy_cluster
  stage: deploy
  image:
    name: hashicorp/terraform:${TERRAFORM_VERSION}
    entrypoint: [""]
  before_script:
    - terraform init
  script:
    # plan generated in a previous job as an artifact
    - terraform apply -auto-approve plan.${ENVIRONMENT}.json
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline" && $CI_COMMIT_BRANCH == "master" && $RESET_TTL != "yes" && $ENVIRONMENT != "prod"'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master" && $RESET_TTL != "yes" && $ENVIRONMENT != "prod"'
    - if: '$CI_PIPELINE_SOURCE == "web" && $CI_COMMIT_BRANCH == "master" && $RESET_TTL != "yes" && $ENVIRONMENT == $JOB_ENVIRONMENT'
      when: manual
  tags:
    - docker
    - my_company
  environment:
    name: $ENVIRONMENT
    on_stop: destroy_swarm_cluster
    auto_stop_in: $TTL

deploy_swarm_prod:
  <<: *deploy_cluster
  variables:
    JOB_ENVIRONMENT: 'prod'
    TTL: 'never'

deploy_swarm_preprod:
  <<: *deploy_cluster
  variables:
    JOB_ENVIRONMENT: 'preprod'
    TTL: '3 days'

Proposal

  • We loose the auto_stop_in validation to String type from duration parser's validation.
  • Renew the environments.auto_stop_in with variable expansion when a deployment succeeded.
  • Set the environments.auto_stop_in with variable expansion when an environment is created. (This could be optional. We need to make sure the user expectations)
  • Track the parser error in Sentry/Log. This is helpful to debug user errors.

Here is a PoC: !93536 (closed)

Edited by Chris Balane