PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 20, 2022

[FIXED] How to pass environment variables to NestJS application from gitlab pipeline (gitlab.yml file)?

 August 20, 2022     docker, environment-variables, gitlab-ci, heroku, node.js     No comments   

Issue

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.


Solution

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.



Answered By - Diaboloxx
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing