Get value of package.json in gitLab CI YML

user3142695 picture user3142695 · Apr 2, 2017 · Viewed 8.2k times · Source

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.

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

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

Answer

Jakub Kania picture Jakub Kania · Apr 13, 2017

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