在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和Alpine的存储库).
安装后(例如在Ubuntu上apt-get update && apt-get install -yqq jq
)
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=$(cat package.json | jq -r .version)
- cd /opt/core/bundle && docker build -t $CI_REGISTRY_IMAGE:$VERSION .
- docker push $CI_REGISTRY_IMAGE:$VERSION
这篇关于在gitLab CI YML中获取package.json的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!