CI environment variables from dotenv in repository?

Problem to solve

Is there a way to define a .env file in your repository that the CI pipeline can injest?

My reasoning: I use various non-sensitive CI/CD variables in the project settings that are critical to the operation of my pipelines. One example is the current version of a package published by this repo.

As such, if someone with access were to accidentally delete these variables from the project settings, it would break my pipelines. Committing them instead to the repository, it seems, would make them more persistent and less susceptible to accidental deletion.

These variables also need to be able to be automatically updated. For example, each time the pipeline publishes the package, it also automatically increments the version number.

Currently I have a job that updates the version CI variable in the project settings via an API call. I was considering instead, having it update the variable in the .env and commit the update to the repo.

I’m aware of the dotenv artifacts, but it would be nice to be able to–as an alternative to/in conjunction with the project CI/CD variables–commit .env files directly in the repository and have the CI/CD pipeline injest them and make them available to jobs.

I realize the general view is that .env shouldn’t be stored in the repo because it could contain sensitive information, but I obviously won’t be committing any sensitive information to these files so I don’t think that is a concern.

Steps to reproduce

Commit the following file to the root of the repository:

variables.env

SOME_VAR=some value

Configure the following job:

.gitlab-ci.yml

job:
  script: 'echo "SOME_VAR: $SOME_VAR"'

The job outputs this:

SOME_VAR:

I suppose I could create a job in .pre to injest those variables

dotenv:
  stage: .pre
  artifacts:
    reports:
      dotenv: .env
  script: cat .env

But it would be nice to be able to do it without having the extra job.

Another option would be to keep the variables in a CI file like this:

variables.yml

variables:
  SOME_VAR: some value

Then in .gitlab-ci.yml:

include:
  - variables.yml

Although the .env format is a bit more appealing and probably a bit simpler to update that var.

SOME_VAR=some value

vs

variables:
  SOME_VAR: some value

Hi there,

Well, GitLab job runs nothing more special then a bash - perhaps try using some bash magic like this? TLDR:

my-job:
  script:
    - set -a && source .env && set +a
    - # some script that uses variables from .env
1 Like