Skip to content

Backend: Add CI_ENVIRONMENT_NAME variable to rules

Problem to solve

Run a subset of jobs in an pipeline based on the environment you select. e.g Run jobs just for dev. This may be thought of as a counterpart to the "redeploy or rollback" button in the environments view - but being able to choose to do this for the initial deployment. Do this while keeping pipeline configuration DRY (Don't repeat yourself)

Intended users

Personas are described at https://about.gitlab.com/handbook/marketing/product-marketing/roles-personas/ -->

Further details

See https://support.gitlab.com/hc/en-us/requests/126158

I'd like to be able to supply a TARGETENV variable in the run pipeline view, with a value matching an environment name and have it run appropriate deploy jobs. e.g Supply TARGETENV == dev, and only job1 is run given the following .gitlab-ci.yml:

.base_job: 
  script: echo $CI_ENVIRONMENT_NAME, $CI_JOB_NAME 
  rules: 
    - if: $CI_PIPELINE_SOURCE == "web" && $TARGETENV == $CI_ENVIRONMENT_NAME

job1: 
  extends: .base_job 
  environment: 
    name: dev

job3: 
  extends: .base_job 
  environment: 
    name: test

Proposal

Hoist evaluation of CI_ENVIRONMENT_NAME variable into the parsing stage of pipeline evaluation. Dynamic namespaces, where resolvable because of their constituent environment variables being resolvable could also be evaluated at this stage. Rules that are unresolvable should throw an error (separate issue)

More evaluation of environment variables during parsing and better errors on resolvable variables should lead to more pipelines either "just working" or being clearer as to why they failed (separate issue).

Permissions and Security

The ability to run these jobs in these environments would still be governed by protected environments etc

Documentation

Should be updated here.

What is the type of buyer?

A customer like ourselves, coming from a tool like Octopus, that wants strong controls over the deployment steps in their pipelines and/or uses infrastructure as code but would love that to be available in one tool (Gitlab) with the appropriate visibility benefits

Links / references

https://support.gitlab.com/hc/en-us/requests/126158 &2072 (comment 234204758)

Implementation

backend MR
Add support for CI_ENVIRONMENT_NAME to be used in job:rules !106093 (merged)
Edited by Leaminn Ma