Is there a way to select an environment for a child pipeline?

Is it possible set an environment for a triggered child pipeline?

Say I have a generic deployment pipeline in a file named deploy.yml that I’d like to reuse for deploying to multiple environments. A simplified version of the pipeline might look like this:

stages:
  - build
  - deploy

build:
  stage: build
  image: alpine
  script:
    - echo "building for $ENVIRONMENT"

deploy:
  stage: deploy
  image: alpine
  script:
    - echo "deploying to $ENVIRONMENT"

To use this for deployments to multiple environments I’d like to use a .gitlab-ci.yml file like this:

stages:
 - deploy-pipeline

deploy-test:
  stage: deploy-pipeline
  environment: "test"
  trigger:
    include: "deploy.yml"
    strategy: depend

deploy-prod:
  stage: deploy-pipeline
  environment: "prod"
  trigger:
    include: "deploy.yml"
    strategy: depend

By setting the environment on the trigger jobs I want each child pipeline to have the correct $ENVIRONMENT variable set dynamically based on the selected environment in the trigger job. Instead Gitlab CI throws this error: jobs:deploy-test config contains unknown keys: environment when using the linter.

I understand that Gitlab CI does not handle setting the environment on a trigger job, but I think this functionality would be nice to have for creating a clean and de-duplicated CI configuration.

We get around this currently by side-stepping Gitlab CI environments and instead setting $ENVIRONMENT manually. With this workaround the above .gitlab-ci.yml would look like this:

stages:
 - deploy-pipeline

deploy-test:
  stage: deploy-pipeline
  variables:
    ENVIRONMENT: "test"
  trigger:
    include: "deploy.yml"
    strategy: depend

deploy-prod:
  stage: deploy-pipeline
  variables:
    ENVIRONMENT: "prod"
  trigger:
    include: "deploy.yml"
    strategy: depend

Is there a way to use Gitlab CI environments to do this that I am missing? I wanted to check here before making an issue.

Thank you for your help, and have a nice day!

I don’t think the environment keyword works like that. I would go with variables as well and if you want to have that visible in the Environments in the UI you can use that variable for child job environment keyword. So your deploy job in deploy.yml would look like this

deploy:
  stage: deploy
  image: alpine
  environment: $ENVIRONMENT
  script:
    - echo "deploying to $ENVIRONMENT"

All right, thanks!