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 curren
You might not be able to do this purely in the gitlab.yml
unfortunately, you could create a shell script as follows and check this into your source control
#!/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 the argument of $CI_REGISTRY_IMAGE
in gitlab.yml
# ...
build:
stage: build
script:
# ...
- chmod +x script.sh
- ./script.sh $CI_REGISTRY_IMAGE
To the best of my knowledge this should work for you.
Thank you to DarrenN and dbaba on Github for his package.json version extract shell function
variables:
PACKAGE_VERSION: $(cat package.json | grep version | head -1 | awk -F= "{ print $2 }" | sed 's/[version:,\",]//g' | tr -d '[[:space:]]') `
in your job or template
.package-template: &package_template
image: docker-hub.registry.integ.fr.auchan.com/docker:latest
stage: package
tags:
- stocks
script:
- export VERSION= ``eval $PACKAGE_VERSION``
- echo "======> Getting VERSION: $VERSION" `
You can use docker to get the package.json version. In this example we tag and push with "latest" and package.json version:
.gitlab-ci.yml
docker-build-master:
# Official docker image.
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- export VERSION=$(docker run --rm -v "$PWD":/app:ro -w /app node:slim node -p "require('./package.json').version")
- docker build --pull -t "$CI_REGISTRY_IMAGE" .
- docker push "$CI_REGISTRY_IMAGE:latest"
- docker image tag "$CI_REGISTRY_IMAGE:latest" "$CI_REGISTRY_IMAGE:$VERSION"
- docker push "$CI_REGISTRY_IMAGE:$VERSION"
only:
- master
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
I would argue the easiest way to retrieve version
from your package.json
is to use node itself.
build:
stage: build
script:
- export VERSION=$(node -p "require('./package.json').version")
- export CONTAINER_RELEASE_IMAGE=$CI_REGISTRY_IMAGE:$VERSION
- cd /opt/core/bundle && docker build -t $CONTAINER_RELEASE_IMAGE .
- docker push $CONTAINER_RELEASE_IMAGE
You can use YAML anchor feature to set a variable that you can use in a job scripts.
.get_version: &get_version
- "export SERVICE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')"
- "Current version is: $SERVICE_VERSION"
and then somewhere in script part:
.gitlab-ci.yml
:
variables:
CONTAINER_NAME: myService
script:
- echo "My own script"
- *get_version
- docker build --iidfile imageid.txt --cache-from $CONTAINER_NAME:$SERVICE_VERSION -t $CONTAINER_NAME:$SERVICE_VERSION .
- docker push $CONTAINER_NAME:$SERVICE_VERSION