Thank you for your work. I am Working on Terraform using your template, and need to set my CUSTOM_ENVIRONMENT_NAME variable differently depending on the environment, in your included jobs but also in others' and mine.
There are different logics to differentiate the environment on my pipeline:
- your logic that is based on the branch BUT both production and staging share the master branch
- an Orange job I include that is based on the gitlab project (one project = one environment)
- my own jobs for which I am yet to precise the logic
So far I found different possible mechanisms:
- using $CI_ENVIRONMENT_NAME -- but I need to set the environment: name: foo on each job and I am including many different ones that I don't really want to overwrite
- using $CI_COMMIT_REF_NAME -- but in your template the production and staging share the same branch
- exporting an artifact in a prerequired job, cf. https://docs.gitlab.com/ee/ci/variables/#pass-an-environment-variable-to-another-job
- duplicating jobs with the extends keyword and setting CI_ENVIRONMENT_NAME or CUSTOM_ENVIRONMENT_NAME manually
- using your scoped variables syntax -- but it only applies in your jobs
I would like to use a combination of two: using your scoped variables and exporting them to other jobs using e.g. an artifact
Is there a way for me to export the scoped variable (cf. https://docs.gitlab.com/ee/ci/variables/#pass-an-environment-variable-to-another-job) from the tf-tflint job (which is running at the very beginning of my pipeline)?
Here is a sample gitlab-ci file:
include:
# terraform
- project: 'to-be-continuous/terraform' # template principal de TBC
ref: '2.3.0'
file: '/templates/gitlab-ci-terraform.yml'
- project: 'to-be-continuous/custom/devops-store' # template additionnel pour utiliser les runners RSC plutôt que DIOD/GIN
ref: '1.1.2'
file: '/templates/gitlab-ci-terraform-dos.yml'
# PDN
- project: "dixsiptal/newdelivery/templates/common"
ref: "1.0.30"
file: "templates/gitlab-ci-ansible.yml"
- project: "dixsiptal/newdelivery/templates/common-token"
ref: "1.0.1"
file: "settoken.yml"
- project: "dixsiptal/newdelivery/templates/pli/pdn"
ref: "2.1.2"
file: "templates/gitlab-ci.yml"
# PLI docker
# NB : common et common-token sont également des pré-requis au PDN
- project: "dixsiptal/newdelivery/templates/pli/docker"
ref: "3.1.0"
file: "templates/gitlab-ci.yml"
variables:
# les lignes scoped suivantes permettent de définir ENV_NOMMAGE_IPANEMA différemment selon l'environnement, grâce au template TBC
# ENV_NOMMAGE_IPANEMA sera alors utilisable depuis tous les jobs
# cf. https://to-be-continuous.gitlab.io/doc/usage/#scoped-variables
scoped__ENV_NOMMAGE_IPANEMA__if__CI_ENVIRONMENT_NAME__equals__production: production
scoped__ENV_NOMMAGE_IPANEMA__if__CI_ENVIRONMENT_NAME__equals__staging: maintenance
scoped__ENV_NOMMAGE_IPANEMA__if__CI_ENVIRONMENT_NAME__equals__integration: e2e
scoped__ENV_NOMMAGE_IPANEMA__if__CI_ENVIRONMENT_NAME__startswith__review: review
# For the docker and the PDN template:
INVENTORY: "./inventories/${ENV_NOMMAGE_IPANEMA}/terraform_inventory.ini"
# or, if it is not possible to interpolate ENV_NOMMAGE_IPANEMA at this stage:
# scoped__INVENTORY__if__CI_ENVIRONMENT_NAME__equals__production: "inventories/production/terraform_inventory.ini"
# scoped__INVENTORY__if__CI_ENVIRONMENT_NAME__equals__staging: "inventories/maintenance/terraform_inventory.ini"
# scoped__INVENTORY__if__CI_ENVIRONMENT_NAME__equals__integration: "inventories/e2e/terraform_inventory.ini"
# scoped__INVENTORY__if__CI_ENVIRONMENT_NAME__startswith__review: "inventories/review/terraform_inventory.ini"
# for the PDN template:
PDN_PROJET: "${CI_SERVER_HOST}/wanctr/sandbox/iac/pdn_inventories/pdn_inventory_${ENV_NOMMAGE_IPANEMA}.git" # doit être différencié selon l'environnement. Imposé par le template PDN
# same thing would apply here
# scoped__PDN_PROJET__if__CI_ENVIRONMENT_NAME__equals__integration: "${CI_SERVER_HOST}/wanctr/sandbox/iac/pdn_inventories/pdn_inventory_e2e.git"
# scoped__PDN_PROJET__if__CI_ENVIRONMENT_NAME__startswith__review: "${CI_SERVER_HOST}/wanctr/sandbox/iac/pdn_inventories/pdn_inventory_review.git"
# etc.
# terraform
TF_PROJECT_DIR: "./terraform"
TF_OUTPUT_DIR: "../ansible/inventories" # relatif à TF_PROJECT_DIR
TF_BINARY_VERSION: "1.0.10"
TF_BRMC_PROVIDER_VERSION: "2.8.1_2.7.0" # peut avoir la forme v1_v2 par exemple 2.8.1_2.7.0 pour avoir 2 versions de providers simultanément utile pour upgrade) (cf. la liste des versions disponibles sur https://gitlab.tech.orange/wanctr/sandbox/iac/terrabrmc/container_registry/18687)
TF_IMAGE: "registry.${CI_SERVER_HOST}/wanctr/sandbox/iac/terrabrmc:${TF_BINARY_VERSION}-${TF_BRMC_PROVIDER_VERSION}"
#TF_EXTRA_OPTS: ""
TF_INIT_OPTS: "-upgrade" # pour upgrade la version du provider le cas échéant
TF_REVIEW_ENABLED: "true" # permet l'utilisation des envs de test, créées automatiquement aux push sur des branches autres que "master" et "develop"
TF_REVIEW_EXTRA_OPTS: "-var-file=values-review.tfvars"
TF_INTEG_ENABLED: "true" # e2e/qualif
TF_INTEG_EXTRA_OPTS: "-var-file=values-e2e.tfvars"
TF_STAGING_ENABLED: "true" # maintenance
TF_STAGING_EXTRA_OPTS: "-var-file=values-maintenance.tfvars"
TF_PROD_ENABLED: "true" # production
TF_PROD_EXTRA_OPTS: "-var-file=values-production.tfvars"
BRMC_HOST: "https://brmc.si.fr.intraorange"
BRMC_TENANT: "vsphere.local"
BRMC_USERNAME: "$BRMC_API_ACCOUNT_USERNAME"
BRMC_PASSWORD: "$BRMC_API_ACCOUNT_PASSWORD"
# ansible utilisé par templates communs DESI https://gitlab.tech.orange/dixsiptal/newdelivery/templates/common/-/blob/1.0.30/README.md
ANSIBLE_PATH: "./ansible" # chemin vers les ressources ansible
ANSIBLE_USER: "ansible"
ODE_ANSIBLE_VERSION: "2.9" # ou 2.10 ?
# docker https://gitlab.tech.orange/dixsiptal/newdelivery/templates/pli/docker/-/blob/3.1.0/README.md
# INVENTORY is used here
.ansible:
extends: .ansible-runner-ode # indique au template gitlab-ci du PLI docker d'utiliser ODE pour le déploiement
DOCKER_INVENTORY_SECTION: "cluster"
DOCKER_REDHAT_VG: "docker_vg"
# etc.
# PLI PostgreSQL
# ...
# ODE CLI
ODE_CLI_VERSION: "2.1.8" # ou "2.3.0" ?
# custom-traefik (one of my own)
ANSIBLE_REQUIREMENTS: "${ANSIBLE_PATH}/requirements.yml"
# ...
CONFIG_APPLICATIVE_MAIN_INVENTORY_DIR: "config_applicative/inventories/${ENV_NOMMAGE_IPANEMA}"
# ...
pre-test-scoped: # testing the dotenv artifact variable setting
stage: test
rules:
- when: always
script:
- echo "ENV_NOMMAGE_IPANEMA_envfile=value_from_other_job" >> justtesting.env
artifacts:
reports:
dotenv: justtesting.env
# echoing preset variables for testing
test-scoped:
stage: test
image: dockerfactory.tech.orange/ode:$ODE_CLI_VERSION
variables:
ENV_NOMMAGE_IPANEMA_rules_by_commitrefname: "review" # valeur par défaut
environment: # Ideally I would like not to indicate the env name each time for concision and simplicity when importing templates
name: testscoped
needs: ["pre-test-scoped"]
rules:
- when: on_success
- if: $CI_COMMIT_REF_NAME == "master"
variables:
ENV_NOMMAGE_IPANEMA_rules_by_commitrefname: "production" # et maintenance ?..
- if: $CI_COMMIT_REF_NAME == "develop"
variables:
ENV_NOMMAGE_IPANEMA_rules_by_commitrefname: "E2E"
- if: $CI_COMMIT_REF_NAME == "scoped-variables"
variables:
ENV_NOMMAGE_IPANEMA_rules_by_commitrefname: "scoped-variables-review-test"
- if: $CI_ENVIRONMENT_NAME == "production"
variables:
ENV_NOMMAGE_IPANEMA_rules_by_cienvname: "production" # et maintenance ?..
- if: $CI_ENVIRONMENT_NAME == "staging"
variables:
ENV_NOMMAGE_IPANEMA_rules_by_cienvname: "maintenance"
- if: $CI_ENVIRONMENT_NAME == "integration"
variables:
ENV_NOMMAGE_IPANEMA_rules_by_cienvname: "E2E" # et maintenance ?..
- if: $CI_ENVIRONMENT_NAME == "review"
variables:
ENV_NOMMAGE_IPANEMA_rules_by_cienvname: "review"
script:
- echo $CI_COMMIT_REF_NAME
- echo $CI_ENVIRONMENT_NAME
- echo $ENV_NOMMAGE_IPANEMA_tfscoped
- echo $ENV_NOMMAGE_IPANEMA_rules_by_commitrefname
- echo $ENV_NOMMAGE_IPANEMA_rules_by_cienvname
- echo $ENV_NOMMAGE_IPANEMA_projectenvset # set in the gitlab project with a scope
- echo $ENV_NOMMAGE_IPANEMA_envfile
custom-traefik: # job supplémentaire
image: dockerfactory.tech.orange/ode:$ODE_CLI_VERSION
when: manual
stage: "Gestion"
script:
# ...
# this job uses scoped varibles
export-inventory-file:
image: dockerfactory.tech.orange/ode:$ODE_CLI_VERSION
when: manual
stage: "Gestion"
script:
# ...
# this job uses scoped varibles