0

I have a npm module project that also provides a docker image. I want to trigger the build/test/push of a docker image when package.json version changes.

I'm after a bash script that will identify whether the version in my package.json has changed.

I can tell if the file has changed with something such as $(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD) scripts that use node are ok since I have the node bin on the build server already, for example, PACKAGE_VERSION=$(node -p -e "require('./package.json').version")

Matt
  • 68,711
  • 7
  • 155
  • 158
Tom
  • 981
  • 11
  • 24

1 Answers1

1

You could compare the version to your current image...

#!/usr/bin/env bash

DOCKER_PACKAGE_VERSION=$(docker run my/image node -pe 'require("./package.json").version')

NEW_PACKAGE_VERSION=$(node -pe 'require("./package.json").version')

if [ "$NEW_PACKAGE_VERSION" == "$DOCKER_PACKAGE_VERSION" ]; then
  printf "Same version [%s]\n" "$NEW_PACKAGE_VERSION"
  exit 1
fi

printf "New version [%s] != [%s]\n" "$NEW_PACKAGE_VERSION" "$DOCKER_PACKAGE_VERSION"
exit 0

Then

$ ./should_i_build.sh && docker build -t my/image .
Matt
  • 68,711
  • 7
  • 155
  • 158
  • interesting and doable. what about looking at git rather than trying to pull a docker image from a repo? – Tom Aug 11 '16 at 18:44