Using environment variables in the build stage

Hello everyone,

I’m using the CI/CD functionality through GitLab.com to build and deploy a single page application written in VueJS. Since this application is entirely client-side I need to use the environment variables set in the UI (Repo → Settings → CI/CD → Variables) during the build stage. This seems to work as expected by setting the environment key for the build stage in the .gitlab-ci.yaml configuration. For example

build prod:
    stage: build
    environment: production
    image: docker:latest

build staging:
    stage: build
    environment: staging
    image: docker:latest

However, I then receive email notifications from GitLab that the project has been deployed even though only the build stage has been executed.

Is there another way to access the configured environment variables set from the UI during the build stage without causing GitLab to think the application has been deployed?

Thanks for taking the time to be thorough in your request, it really helps! :blush:

the environment: production is referring to Deployment Environments ( Repo → Operations → Environments) not CI env_vars.

Try this:

build staging:
  stage: build
  image: docker:latest
  script:
    # export all environment variables
    - export

build prod:
    stage: build
    image: docker:latest
  script:
    - echo "CI/CD env var environment=$environment"

Hey, thanks for the response! I’m not sure that will work in this case because I need to define the same environment variables with different values depending on where it will be deployed to, for example DEBUG would be set to true for the production build and false for the staging build. I guess one option would be to create environment variables that aren’t scopped and prefix their name with the environment e.g. PROD_DEBUG and STAGING_DEBUG though it’s not the ideal solution.

I had a similar issue and found this: `.gitlab-ci.yml` keyword reference | GitLab.