GreedyAi
GreedyAi

Reputation: 2839

Angular proxy.conf.js pass environment variables from CI/CD

Is there a way to pass the server URL using environment variables of CI/CD?

I am trying to make testing CI/CD for my project. The problem is that for angular cypress test I need access to server. depending on who runs the pipelines they would have access to different servers, so I want them to be able to change proxy.conf.json using environment variables on bitbucket.

my current proxy.conf.json is:

{
  "/api": {
    "target": "localhost:3000",
    "secure": false,
    "pathRewrite": {
      "^/api": "/api"
    },
    "changeOrigin": true
  }
}

I want to achieve:

{
  "/api": {
    "target": $MY_SERVER_ENV_VARIABLE,
    "secure": false,
    "pathRewrite": {
      "^/api": "/api"
    },
    "changeOrigin": true
  }
}

So I want to pass $MY_SERVER_ENV_VARIABLE from Bitbucket CI/CD. Is it possible?

Upvotes: 0

Views: 2137

Answers (1)

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7279

On of the options is to make js conf file instead of json file. Here is the docs.

Now you are can use NodeJS process global variable. You can get env variables through it.

So, the code of proxy.conf.js will be:

const DEFAULT_TARGET = "localhost:3000";
const PROXY_CONFIG = {
  "/api": {
    "target": process.env.YOUR_ENV_VARIABLE || DEFAULT_TARGET,
    "secure": false,
    "pathRewrite": {
      "^/api": "/api"
    },
    "changeOrigin": true
  }
};

module.exports = PROXY_CONFIG;

Don't forget to change a path to the config file in angular.json file.

Upvotes: 3

Related Questions