0

I want to be able to set dynamic environment url in gitlab yml.

...
environment:
    url: $ENVIRONMENT_URL/?env=${echo $CI_COMMIT_TAG | grep -o '[^@]*$'}
...

I know that I can't use bash command in url, it should be string or variable. Is there a way to somehow set echo $CI_COMMIT_TAG | grep -o '[^@]*$' as variable, ie in script and use it here? Variables declared in script are local but maybe there is a way to export it to outer scope and use it in environment.url.

I've tried

  script:
    - VERSION_SHORT=`echo $CI_COMMIT_TAG | grep -o '[^@]*$'`
  environment:
    url: $ENVIRONMENT_URL/?env=${echo $CI_COMMIT_TAG | grep -o '[^@]*$'}
  variables:
    VERSION_SHORT: $VERSION_SHORT
kndev
  • 1

1 Answers1

0

It is possible to set a dynamic environment url in Gitlab(depending on version of Gitlab you are using)

Make use of

artifacts:reports:dotenv

Introduced in GitLab 12.9

The dotenv report collects a set of environment variables as artifacts.

Gitlab parses the deploy.env report artifact, registers a list of variables as runtime-created, expands the environment:url: $DYNAMIC_ENVIRONMENT_URL and sets it to the environment URL.

  script:
    - VERSION_SHORT=`echo $CI_COMMIT_TAG | grep -o '[^@]*$'`                         
    - echo "DYNAMIC_ENVIRONMENT_URL=$ENVIRONMENT_URL/?env=$VERSION_SHORT" >> deploy.env
  artifacts:
    reports:
      dotenv: deploy.env                                                       
  environment:
    name: XYZ
    url: $DYNAMIC_ENVIRONMENT_URL                                              
Raghu
  • 123
  • 7