Angular环境变量查找

huangapple go评论74阅读模式
英文:

Angular environment variables lookup

问题

我想从我的CI/CD流水线中注入环境变量到我的Angular应用程序中,以便自动配置environment.tsproxy.config.json

例如,

export const environment = {
  var: $MY_VAR
};
{
  "/api": {
    "target": "$MY_API_URL",
    "secure": true,
    "changeOrigin": true
  }
}

其中MY_VARMY_API_URL是GitLab CI/CD变量。Angular是否像Quarkus/Spring一样查找变量?还是我必须手动配置和声明所有环境到angular.json中?

英文:

I'd like to inject environment variables in my angular app from my CI/CD pipeline so that environment.ts and proxy.config.json can be automatically configured.

For example,

export const environment = {
  var: $MY_VAR
};
{
  "/api": {
    "target": "$MY_API_URL",
    "secure": true,
    "changeOrigin": true
  }
}

where MY_VAR and MY_API_URL are gitlab CI/CD variables. Does Angular lookup for variables as quarkus/spring do? Or I am forced to configure and declare all environments by hand into angular.json?

答案1

得分: 2

1. SED Command

你可以创建一个用于构建你的环境的脚本,放在你的 package.json 中,像这样:

{
  ...
  "scripts": {
    ...
    "build:env": "ng generate environments && sed -i 's|};| production: '$PRODUCTION',\\n apiUrl: '$API_URL',\\n };|' ./src/environments/environment.ts"
    ...
    
  }
  ...
}

假设你有一个 src/environments.ts 文件像这样:

export const environment = {};

那个脚本将让你得到:

export const environment = {
  production: ...
  apiUrl: ...
}

基本上它所做的是替换 environments.ts 中的 '}' 为你提供的内容。

2. 自定义脚本

另一种选择是创建自己的 shell 脚本来为你创建这些文件。这个脚本会创建本地和生产环境两种文件。因为脚本会为你创建文件,所以文件本身不需要存在。

scripts/build-environments.sh 中:

#!/bin/sh

cd src
mkdir environments
cd environments

echo "export const environment = {
  production: false,
  apiUrl: '$API_URL',
  };" > environment.ts

echo "export const environment = {
  production: true,
  apiUrl: '$API_URL',
  };" > environment.prod.ts

echo '环境设置完成'

exit 0

然后你的 package.json 可以像这样:

{
  ...
  "scripts": {
    ...
    "build:env": "sh scripts/build-environments.sh"
    ...
  }
  ...
}

希望这些信息对你有所帮助。

英文:

I've found several ways of doing this in the past and these methods may be a bit strange there are probably much better solutions available.

1. SED Command

You can create a script for building your environment in your package.json like this:

{
  ...
  "scripts": {
    ...
    "build:env": "ng generate environments && sed -i 's|};| production: '$PRODUCTION',\\n apiUrl: '$API_URL',\\n };|' ./src/environments/environment.ts"
    ...
    
  }
  ...
}

Imagine you have src/environments.ts like this:

export const environment = {};

That script would leave you with:

export const environment = {
  production: ...
  apiUrl: ...
}

Basically what it is doing is replacing '}' in environments.ts whatever your provide.

2. Custom script

Another option would be to create your own shell script which created the files for you. This one creates both local and production environments. The file wouldn't need to be created because the script does it for you.

In scripts/build-environments.sh

#!/bin/sh

cd src
mkdir environments
cd environments

echo "export const environment = {
  production: false,
  apiUrl: '"$API_URL"',
  };" > environment.ts

echo "export const environment = {
  production: true,
  apiUrl: '"$API_URL"',
  };" > environment.prod.ts

echo 'Finished setting up environment'

exit 0

Then your package.json could look like this:

{
  ...
  "scripts": {
    ...
    "build:env": "sh scripts/build-environments.sh"
    ...
  }
  ...
}

huangapple
  • 本文由 发表于 2023年6月1日 19:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定