skip to Main Content

I new to gitlab CI/CD and I am looking for a way to pass environment variables to my NestJs application deployed to Heroku.

This is my .gitlab.yml file

...


image: node:latest

before_script:
  - apt-get update -qy
  - apt-get install -y ruby-dev
  - gem install dpl

stages:
  - testing
  - staging

testing:
  stage: testing
  image: salesforce/salesforcedx:latest-slim
  script:
    - accessToken=accessToken

    - echo TEST_ACCESS_TOKEN=${accessToken} > .env.test
    - echo dummmy=test >> .env
    - echo dummmyWithQuotes=test >> ".env"

  only:
    - staging
    - main

staging:
  stage: staging
  image: ruby:latest
  script:
    - dpl --provider=heroku --app=$HEROKU_APP_STAGING --api-key=$HEROKU_API_KEY
  only:
    - staging

This is my app controller to test the deployed result

@Get()
  getHello() {
    const temoin = this.configService.get('PROD_LOGIN_URL');
    const tested = this.configService.get('TEST_ACCESS_TOKEN');
    const tested2 = this.configService.get('dummmy');
    const tested3 = this.configService.get('dummmyWithQuotes');
    return {
      temoin,
      tested,
      tested2,
      tested3,
    };
  }

Of course I have the following in the app module

ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env', '.env.test'],
    }),

What I get in the response from the deployed application is the folowing

{"temoin":"https://login.salesforce.com"}

I think this have to do with the docker image. What I can think of is that the files are getting created in the docker container and stay in the gitlab job context (Maybe I don’t know)

Edit:

I added an ls -a in the staging job’s scripts and there is no .env.test file

Edit 2:

I added the .env.test and .env files to the job’s artifacts and it became available to the staging job. But when deploying the application with the dpl command the .env.test is not present in Heoku application files.

PS: I forgot to mention that the .env.test isn’t present in the Git project, it’s created in the pipeline.

2

Answers


  1. Chosen as BEST ANSWER

    After adding the artifact to make the .env and .env.test files available to the testing job, as Benjamin said, I found that those files are not getting deployed in the application. And the reason is that dpl performs a cleanup (stash all) before deploying. I found this issue that adds the --skip_cleanup flag to dpl to avoid the cleanup.

    Works like a charm.


  2. It is not completely clear to me what you want to accomplish, but I think you would like the .env file to be available in the staging job.

    Pass it by adding it as a job artifact

    testing:
      stage: testing
      image: salesforce/salesforcedx:latest-slim
      script:
        - accessToken=accessToken
        - echo TEST_ACCESS_TOKEN=${accessToken} > .env.test
        - echo dummmy=test >> .env
        - echo dummmyWithQuotes=test >> ".env"
      artifacts:
        paths:
          - .env
      only:
        - staging
        - main
    

    Another option, if you would like dynamically generated variables to be available to subsequent jobs, would be to use the built-in dotenv report.

    artifacts:
        reports:
          dotenv: .env
    

    This will make any vars in the .env file available in the environment of subsequent jobs as the dotenv report gets loaded into the environment automatically.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search