Skip to content

Add support for CI_ENVIRONMENT_NAME to be used in job:rules

Leaminn Ma requested to merge 35315-ci_environment_name-to-rules into master

What does this MR do and why?

We noted in #35315 (closed) that customers would like the ability to use the variable CI_ENVIRONMENT_NAME in job:rules. The purpose is to run a subset of jobs in a pipeline based on an environment name of their choosing.

Prior to this MR, we can only access most of the CI_ENVIRONMENT_* variables after the build and environment creation. But we can make an exception for CI_ENVIRONMENT_NAME as its value is easily accessible from the job's environment config. In this MR, we set its value earlier in the job cycle such that it is populated prior to the job's rules evaluation.

Documentation updated: Where variables can be used

Screenshots or screen recordings

Example config:

variables:
  TARGET_ENV: "dev"

.base_job: 
  script: echo "Only run dev jobs - $CI_ENVIRONMENT_NAME, $CI_JOB_NAME"
  rules: 
    - if: $CI_ENVIRONMENT_NAME == $TARGET_ENV

job1: 
  extends: .base_job
  environment: 
    name: dev
    url: "http://test.com"

job2: 
  extends: .base_job 
  environment: dev

job3: 
  extends: .base_job 
  environment: 
    name: test
  • We want job1 and job2 to run.

  • Prior behaviour: The CI_ENVIRONMENT_NAME value is not set so no jobs run.

  • New behaviour: The CI_ENVIRONMENT_NAME value is set so job1 and job2 run as expected.

Screenshot_2022-12-05_at_7.03.52_PM

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #35315 (closed)

Edited by Leaminn Ma

Merge request reports