如何在 Angular 2+ 项目中调用 circleCI 环境变量?
How to call circleCI environment variable in an Angular 2+ project?
我有一个 angular 项目,它有一个 api-keys.ts 文件,如下所示:
export var masterFirebaseConfig = {apiKey: $fireBaseApiKey, authDomain: 'dataJitsu.firebaseapp.com',databaseURL: 'https://datajitsu.firebaseio.com',storageBucket: '',messagingSenderId: '495992924984'};
我认为 $fireBaseApiKey
作为环境变量存储在我在 circleCI 上的项目中,正如您在此处的图片中看到的那样:
但是,当我在 circleCI 上 运行 配置时,仍然出现以下错误:
ERROR in src/app/api-keys.ts(1,44): error TS2304: Cannot find name
'$fireBaseApiKey'. src/app/app.module.ts(75,11): error TS2304: Cannot
find name 'apiKey'.
(app.module.ts中的错误直接来自api-keys.ts中的错误)
我试图弄清楚问题是出在 circleCI 方面,还是与我将其插入 Angular 的方式有关,所以我试图 echo
找出配置文件中的环境变量:
version: 2.1
orbs:
cypress: cypress-io/cypress@1.0.1
steps:
- run:
name: Setup Environment Variables
command: |
echo '$fireBaseApiKey'
workflows:
build:
jobs:
- cypress/install:
build: 'npm run build'
context: fireBaseApiKey
- cypress/run:
requires:
- cypress/install
start: 'npm start'
context: fireBaseApiKey
我在会话出错后 ssh 进入了会话,但我看不到任何指示
我的 echo 命令甚至被确认了。
我希望 echo
方面或 Angular 方面的帮助能够有效解决 question/problem.
此外,我使用 cypress 进行集成测试,因此我使用 Cypress 的 orb 来设置和 运行ning 测试。我不确定 to/whether 环境变量如何渗透 orb 作业,所以我还将变量添加到项目的上下文中(上下文和单独键值对的键具有相同的名称) :
更新:这里是 circleCI 日志文件的输出:
0 info it worked if it ends with ok 1 verbose cli [
'/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'build' ] 2 info
using npm@6.4.1 3 info using node@v10.13.0 4 verbose run-script [
'prebuild', 'build', 'postbuild' ] 5 info lifecycle
data-jitsu@0.0.0~prebuild: data-jitsu@0.0.0 6 info lifecycle
data-jitsu@0.0.0~build: data-jitsu@0.0.0 7 verbose lifecycle
data-jitsu@0.0.0~build: unsafe-perm in lifecycle true 8 verbose
lifecycle data-jitsu@0.0.0~build: PATH:
/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/root/project/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
9 verbose lifecycle data-jitsu@0.0.0~build: CWD: /root/project 10
silly lifecycle data-jitsu@0.0.0~build: Args: [ '-c', 'ng build' ] 11
silly lifecycle data-jitsu@0.0.0~build: Returned: code: 1 signal:
null 12 info lifecycle data-jitsu@0.0.0~build: Failed to exec build
script 13 verbose stack Error: data-jitsu@0.0.0 build: ng build
13
verbose stack Exit status 1 13 verbose stack at
EventEmitter.
(/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack at EventEmitter.emit (events.js:182:13) 13
verbose stack at ChildProcess.
(/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:182:13) 13
verbose stack at maybeClose (internal/child_process.js:962:16) 13
verbose stack at Process.ChildProcess._handle.onexit
(internal/child_process.js:251:5) 14 verbose pkgid data-jitsu@0.0.0 15
verbose cwd /root/project 16 verbose Linux 4.4.0-141-generic 17
verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "build"
18 verbose node v10.13.0 19 verbose npm v6.4.1 20 error code
ELIFECYCLE 21 error errno 1 22 error data-jitsu@0.0.0 build: ng
build
22 error Exit status 1 23 error Failed at the data-jitsu@0.0.0
build script. 23 error This is probably not a problem with npm. There
is likely additional logging output above. 24 verbose exit [ 1, true ]
您尝试过 echo $fireBaseApiKey
(不带引号)吗?
要对 CircleCI 进行故障排除,在本地启动映像很有用。
示例:下面的命令将启动本地 ubuntu 实例,环境变量 fireBaseApiKey
设置为 asdf-asdf-asdf
。您的本地文件将安装在 /usr/src/app
.
docker run -it -e fireBaseApiKey=asdf-asdf-asdf -v $PWD:/usr/src/app ubuntu bash
要检查您的环境变量,请尝试:
echo $fireBaseApiKey
cd /usr/src/app
和 运行 您的构建脚本,一步一步。我发现对因未知原因而失败的构建进行故障排除很有用。
好吧,我终于弄明白了,虽然可能是以一种 hacky 的方式。我刚刚使用 sed
将我的环境变量替换为我的 api-keys.ts 文件。
所以,这是新的 config.yml 脚本(请注意大量更改,包括 wait-on: 'http-get://localhost:4200'
(请注意 http-get 那里而不是 http!)。
version: 2.1
orbs:
cypress: cypress-io/cypress@1.5.1
jobs:
build:
working_directory: ~/project
docker:
- image: circleci/node:9.6.1-browsers
environment:
circleCiApiKey: fireBaseApiKey
steps:
- checkout
- run:
name: Show current branch
command: |
echo ${CIRCLE_BRANCH}
ls -larth
echo $fireBaseApiKey
cat src/app/api-keys.ts
sed -i "s/circleCiApiKey/$fireBaseApiKey/g" src/app/api-keys.ts
cat src/app/api-keys.ts
- restore_cache:
keys:
- v1-dependencies-{{checksum "package.json"}}
- v1-dependencies-
- run:
name: Install local dependencies
command: |
npm install
- save_cache:
key: v1-dependencies-{{checksum "package.json"}}
paths:
- node_modules
- run:
name: Building
command: npm run build
- save_cache:
key: v1-dist-{{ .Environment.CIRCLE_BRANCH}}-{{ .Environment.CIRCLE_SHA1}}
paths:
- dist
workflows:
version: 2.1
build:
jobs:
- build
- cypress/install:
requires:
- build
build: 'npm run build'
- cypress/run:
requires:
- cypress/install
- build
start: 'npm start'
store_artifacts: true
wait-on: 'http-get://localhost:4200'
替换发生在 sed -i "s/circleCiApiKey/$fireBaseApiKey/g" src/app/api-keys.ts
行。
api-keys.ts 文件依次包含:
export var masterFirebaseConfig = {
apiKey: "circleCiApiKey",
authDomain: "dataJitsu.firebaseapp.com",
databaseURL: "https://datajitsu.firebaseio.com",
storageBucket: "",
messagingSenderId: "495992924984"
};
export var masterStripeConfig = {
publicApiTestKey: "pk_test_NKyjLSwnMosdX0mIgQaRRHbS",
secretApiTestKey: "sk_test_6YWZDNhzfMq3UWZwdvcaOwSa",
publicApiKey: "",
secretApiKey: ""
};
我有一个 angular 项目,它有一个 api-keys.ts 文件,如下所示:
export var masterFirebaseConfig = {apiKey: $fireBaseApiKey, authDomain: 'dataJitsu.firebaseapp.com',databaseURL: 'https://datajitsu.firebaseio.com',storageBucket: '',messagingSenderId: '495992924984'};
我认为 $fireBaseApiKey
作为环境变量存储在我在 circleCI 上的项目中,正如您在此处的图片中看到的那样:
但是,当我在 circleCI 上 运行 配置时,仍然出现以下错误:
ERROR in src/app/api-keys.ts(1,44): error TS2304: Cannot find name '$fireBaseApiKey'. src/app/app.module.ts(75,11): error TS2304: Cannot find name 'apiKey'.
(app.module.ts中的错误直接来自api-keys.ts中的错误)
我试图弄清楚问题是出在 circleCI 方面,还是与我将其插入 Angular 的方式有关,所以我试图 echo
找出配置文件中的环境变量:
version: 2.1
orbs:
cypress: cypress-io/cypress@1.0.1
steps:
- run:
name: Setup Environment Variables
command: |
echo '$fireBaseApiKey'
workflows:
build:
jobs:
- cypress/install:
build: 'npm run build'
context: fireBaseApiKey
- cypress/run:
requires:
- cypress/install
start: 'npm start'
context: fireBaseApiKey
我在会话出错后 ssh 进入了会话,但我看不到任何指示 我的 echo 命令甚至被确认了。
我希望 echo
方面或 Angular 方面的帮助能够有效解决 question/problem.
此外,我使用 cypress 进行集成测试,因此我使用 Cypress 的 orb 来设置和 运行ning 测试。我不确定 to/whether 环境变量如何渗透 orb 作业,所以我还将变量添加到项目的上下文中(上下文和单独键值对的键具有相同的名称) :
更新:这里是 circleCI 日志文件的输出:
0 info it worked if it ends with ok 1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'build' ] 2 info using npm@6.4.1 3 info using node@v10.13.0 4 verbose run-script [ 'prebuild', 'build', 'postbuild' ] 5 info lifecycle data-jitsu@0.0.0~prebuild: data-jitsu@0.0.0 6 info lifecycle data-jitsu@0.0.0~build: data-jitsu@0.0.0 7 verbose lifecycle data-jitsu@0.0.0~build: unsafe-perm in lifecycle true 8 verbose lifecycle data-jitsu@0.0.0~build: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/root/project/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 9 verbose lifecycle data-jitsu@0.0.0~build: CWD: /root/project 10 silly lifecycle data-jitsu@0.0.0~build: Args: [ '-c', 'ng build' ] 11 silly lifecycle data-jitsu@0.0.0~build: Returned: code: 1 signal: null 12 info lifecycle data-jitsu@0.0.0~build: Failed to exec build script 13 verbose stack Error: data-jitsu@0.0.0 build:
ng build
13 verbose stack Exit status 1 13 verbose stack at EventEmitter. (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16) 13 verbose stack at EventEmitter.emit (events.js:182:13) 13 verbose stack at ChildProcess. (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14) 13 verbose stack at ChildProcess.emit (events.js:182:13) 13 verbose stack at maybeClose (internal/child_process.js:962:16) 13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5) 14 verbose pkgid data-jitsu@0.0.0 15 verbose cwd /root/project 16 verbose Linux 4.4.0-141-generic 17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "build" 18 verbose node v10.13.0 19 verbose npm v6.4.1 20 error code ELIFECYCLE 21 error errno 1 22 error data-jitsu@0.0.0 build:ng build
22 error Exit status 1 23 error Failed at the data-jitsu@0.0.0 build script. 23 error This is probably not a problem with npm. There is likely additional logging output above. 24 verbose exit [ 1, true ]
您尝试过 echo $fireBaseApiKey
(不带引号)吗?
要对 CircleCI 进行故障排除,在本地启动映像很有用。
示例:下面的命令将启动本地 ubuntu 实例,环境变量 fireBaseApiKey
设置为 asdf-asdf-asdf
。您的本地文件将安装在 /usr/src/app
.
docker run -it -e fireBaseApiKey=asdf-asdf-asdf -v $PWD:/usr/src/app ubuntu bash
要检查您的环境变量,请尝试:
echo $fireBaseApiKey
cd /usr/src/app
和 运行 您的构建脚本,一步一步。我发现对因未知原因而失败的构建进行故障排除很有用。
好吧,我终于弄明白了,虽然可能是以一种 hacky 的方式。我刚刚使用 sed
将我的环境变量替换为我的 api-keys.ts 文件。
所以,这是新的 config.yml 脚本(请注意大量更改,包括 wait-on: 'http-get://localhost:4200'
(请注意 http-get 那里而不是 http!)。
version: 2.1
orbs:
cypress: cypress-io/cypress@1.5.1
jobs:
build:
working_directory: ~/project
docker:
- image: circleci/node:9.6.1-browsers
environment:
circleCiApiKey: fireBaseApiKey
steps:
- checkout
- run:
name: Show current branch
command: |
echo ${CIRCLE_BRANCH}
ls -larth
echo $fireBaseApiKey
cat src/app/api-keys.ts
sed -i "s/circleCiApiKey/$fireBaseApiKey/g" src/app/api-keys.ts
cat src/app/api-keys.ts
- restore_cache:
keys:
- v1-dependencies-{{checksum "package.json"}}
- v1-dependencies-
- run:
name: Install local dependencies
command: |
npm install
- save_cache:
key: v1-dependencies-{{checksum "package.json"}}
paths:
- node_modules
- run:
name: Building
command: npm run build
- save_cache:
key: v1-dist-{{ .Environment.CIRCLE_BRANCH}}-{{ .Environment.CIRCLE_SHA1}}
paths:
- dist
workflows:
version: 2.1
build:
jobs:
- build
- cypress/install:
requires:
- build
build: 'npm run build'
- cypress/run:
requires:
- cypress/install
- build
start: 'npm start'
store_artifacts: true
wait-on: 'http-get://localhost:4200'
替换发生在 sed -i "s/circleCiApiKey/$fireBaseApiKey/g" src/app/api-keys.ts
行。
api-keys.ts 文件依次包含:
export var masterFirebaseConfig = {
apiKey: "circleCiApiKey",
authDomain: "dataJitsu.firebaseapp.com",
databaseURL: "https://datajitsu.firebaseio.com",
storageBucket: "",
messagingSenderId: "495992924984"
};
export var masterStripeConfig = {
publicApiTestKey: "pk_test_NKyjLSwnMosdX0mIgQaRRHbS",
secretApiTestKey: "sk_test_6YWZDNhzfMq3UWZwdvcaOwSa",
publicApiKey: "",
secretApiKey: ""
};