获取Jenkins BUILD_NUMBER变量并将其显示在angular应用程序中 [英] Get Jenkins BUILD_NUMBER variable and display it in angular application
问题描述
我部署了Jenkins的angular应用程序,我想在页面的页脚中显示内部版本号.我知道Jenkins运行作业时,它会发送系统环境变量 BUILD_NUMBER . 我读了一些文章,但没有人说我们如何从OS中获取此变量?
I deploy an angular application by Jenkins and I want to display the build number in the footer of the page. I know that while Jenkins is running the job, it sents a system environment variable BUILD_NUMBER. I read some articles but none is saying how can we get this variable from the OS?
推荐答案
这是一种解决方法.
您可以使用构建前和构建后事件来替换environment.prod.ts
文件中的版本标记.
You can use pre and post build event to replace a version-tag in environment.prod.ts
file.
export const environment = { production: true, system: 'prod', version: '%VERSION%' };
安装npm-replace:
install npm-replace:
npm install replace --save-dev
package.json
package.json
"build:prod": "ng build --env=prod",
"prebuild:prod": "replace '%VERSION%' $VERSION src/environments/environment.prod.ts",
"postbuild:prod": "replace $VERSION '%VERSION%' src/environments/environment.prod.ts",
在jenkins中,运行以下命令
In jenkins run the following command
VERSION="$BUILD_NUMBER" npm run build:prod
更新
实现上述行为的另一种方法是使用Linux中预装的sed
实用程序.
您可以按照以下步骤操作
Another way to achieve mentioned behavior is to use sed
utility which is pre-installed in Linux.
You can follow these steps
-
更新
environment.prod.ts
以具有预定义的密钥.假设jenkinsBuildNO
.例如
Update
environment.prod.ts
to have a pre-defined key. Let's supposejenkinsBuildNO
. e.g
export const environment = { production: true, system: 'prod', version: 'jenkinsBuildNO' };
在jenkins中创建另一个步骤,以使用内部版本号或其他名称更新environment.prod.ts
中的jenkinsBuildNO
.
Create another step in jenkins to update jenkinsBuildNO
in environment.prod.ts
with Build No. or something other.
sed -i -e 's/jenkinsBuildNO/%BUILD_NUMBER%/g' src/environments/environment.prod.ts
开始角度构建. ng buld --prod
使用我们中的预定义键jenkinsBuildNO
更新内部版本号
environment.prod.ts
Update build no with our pre-defined key jenkinsBuildNO
in
environment.prod.ts
sed -i -e 's/%BUILD_NUMBER%/jenkinsBuildNO/g' src/environments/environment.prod.ts
这篇关于获取Jenkins BUILD_NUMBER变量并将其显示在angular应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!