Get package.json value in gitLab CI YML

I am using gitLab CI for my nodejs application. In my YML file, I need to call a script to create a docker image. But instead of using latest I need to use the current version of the project.

This version value can be found in the package.json file of the repository.

Can I read the version value of the package.json file to replace latest with 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 
+5
source share
2 answers

If you do not mind installing additional packages, you can use jq , which provides much more flexibility (available in the repository for Ubuntu and Alpine). After installing it (e.g. apt-get update && apt-get install -yqq jq in 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 
+4
source

You may not be able to do this purely in gitlab.yml , unfortunately, you can create a shell script as follows and check this with your control source

 #!/bin/sh args=(" $@ ") CI_REGISTRY_IMAGE=${args[0]} PACKAGE_VERSION=$(cat package.json \ | grep version \ | head -1 \ | awk -F: '{ print $2 }' \ | sed 's/[",]//g' \ | tr -d '[[:space:]]') CONTAINER_RELEASE_IMAGE=$CI_REGISTRY_IMAGE\:$PACKAGE_VERSION cd /opt/core/bundle && docker build -t $CONTAINER_RELEASE_IMAGE . docker push $CONTAINER_RELEASE_IMAGE 

Then execute this script with $CI_REGISTRY_IMAGE argument in gitlab.yml

 # ... build: stage: build script: # ... - chmod +x script.sh - ./script.sh $CI_REGISTRY_IMAGE 

As far as I know, this should work for you.

Thanks to DarrenN and dbaba on Github for its version of the package.json shell.

+2
source

Source: https://habr.com/ru/post/1266222/


All Articles