在 gitLab CI YML 中获取 package.json 的值 [英] Get value of package.json in gitLab CI YML
问题描述
我正在为我的 nodejs 应用程序使用 gitLab CI.在我的 YML 文件中,我需要调用一个脚本来构建一个 docker 映像.但我需要使用项目的当前版本,而不是使用 latest
.
I'm using gitLab CI for my nodejs application. In my YML file I need to call a script to build a docker image. But instead of using latest
I need to use the current version of the project.
这个版本值可以在仓库的package.json
文件中找到.
This version value can be find in the package.json
file of the repository.
是否可以读取 package.json 文件的版本值,将 latest
替换为当前版本?
Is it possible to read the version value of the package.json file to replace latest
by the current version?
# ...
variables:
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest # need version value instead of latest
build:
stage: build
script:
# ...
- cd /opt/core/bundle && docker build -t $CONTAINER_RELEASE_IMAGE .
- docker push $CONTAINER_RELEASE_IMAGE
推荐答案
如果你不反对安装额外的包,你可以使用 jq
,它提供了更大的灵活性(在 Ubuntu 和阿尔卑斯山).安装后(例如 apt-get update && apt-get install -yqq jq
在 Ubuntu 上):
If you are not against installing additional packages you can use jq
which allows for much more flexibility (available in repository for both Ubuntu and Alpine).
Once you install it (for example apt-get update && apt-get install -yqq jq
on Ubuntu):
- export VERSION=$(jq -r .version package.json)
- cd /opt/core/bundle && docker build -t $CI_REGISTRY_IMAGE:$VERSION .
- docker push $CI_REGISTRY_IMAGE:$VERSION
这篇关于在 gitLab CI YML 中获取 package.json 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!