Search code examples
gitlabyamlenvironment-variablesgitlab-cipipeline

Can I pass a variable from .env file into .gitlab-ci.yml


I'm quite new to CI/CD and basically I'm trying to add this job to Gitlab CI/CD that will run through the repo looking for secret leaks. It requires some API key to be passed there. I was able to directly insert this key into .gitlab-ci.yml and it worked as it was supposed to - failing the job and showing that this happened due to this key being in that file.

But I would like to have this API key to be stored in .env file that won't be pushed to a remote repo and to pull it somehow into .gitlab-ci.yml file from there.

Here's mine

stages:
  - scanning

gitguardian scan:
  variables:
    GITGUARDIAN_API_KEY: ${process.env.GITGUARDIAN_API_KEY}
  image: gitguardian/ggshield:latest
  stage: scanning
  script: ggshield scan ci

The pipeline fails with this message: Error: Invalid API key. so I assume that the way I'm passing it into variables is wrong.


Solution

  • CI variables should be available in gitlab-runner(machine or container) as environment variables, they are either predefined and populated by Gitlab like the list of predefined variables here, or added by you in the settings of the repository or the gitlab group Settings > CI/CD > Add Variable.

    After adding variables you can use the following syntax, you can test if the variable has the correct value by echoing it.

    variables:
       GITGUARDIAN_API_KEY: "$GITGUARDIAN_API_KEY"
    script:
       - echo "$GITGUARDIAN_API_KEY"
       - ggshield scan ci