From 65428aab7c9684b9af9eaa460a203b3ab7e9d41a Mon Sep 17 00:00:00 2001 From: Oskar Pearson Date: Mon, 6 Feb 2017 16:13:30 +0100 Subject: [PATCH] Build docker images with Travis - Add Docker hub secrets to Travis - Push to Docker Hub - Ignore .git directory and things like coverage logs, so as to minimise the container size I placed the Docker hub login commands in the commands in the .travis.yml file, rather than in the Makefile. This is because the Makefile would display the Docker password to stdout, which would then become publically available in the Travis logs. I also run `git clean -fd .` and `git checkout -- .` to ensure that anything written to the repo directory during tests isn't added to the docker container. I do this in the Travis file rather than the Makefile, since running things with `make` reverted my changes while experimenting, and I want to save others the same issue. To make this work on Travis, we need to set secrets for the DOCKER_USER and DOCKER_PASS environment variable. --- .dockerignore | 18 ++++++++++++++++++ .travis.yml | 8 +++++++- Dockerfile.example | 24 ++++++++++++++++++++++++ Makefile | 26 +++++++++++++++++++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile.example diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..e462a2e5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,18 @@ +# Don't include the .git history in the Docker image: +.git + +# Items from .gitignore +bower_components/ +node_modules/ +bundle/bundle.out.js +.idea/ +*.iml +my.env +*.env +static/bower_components/ +.*.sw? +.DS_Store +.vagrant +/iisnode +coverage/ +npm-debug.log diff --git a/.travis.yml b/.travis.yml index 4ec5f103..f67daf89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,9 +14,15 @@ services: - mongodb - docker script: make travis +after_success: + - nvm version + - if [[ ! -z "$DOCKER_USER" ]] && [[ "$(nvm version)" = "v0.12."* ]]; then docker login -u ${DOCKER_USER} -p ${DOCKER_PASS} && git checkout -- . && git clean -fd . && make docker_release; fi after_script: make report env: - - CXX=g++-4.8 + global: + - CXX=g++-4.8 + - secure: "QW33rAsCulSORXkSQK9fQotS5GuXwsr3P0KX+R0CE88N0rNE1gZuKZL5cIfYvHTdd4BiReMWja6Cox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Ja3AikqIdEDnI0eodKmkTa+Do9nrLgZLWhG8HXm06SySICClnhqUmbAAHuliQxSdnDixuwhgcBkkPX/5Ta/pHIz0u5r9mdGKShKg1irC1alV4jtr6dJU7ly/orhW8zvoV1mu6C75lnc3aeqc1CVPMyowLUS+y+XQpOiGds57n0UlRFq38LE5n06ODv1x2eMFf8Wk+pbxgaOI4Co338A4opRjLPbULs=" addons: apt: sources: diff --git a/Dockerfile.example b/Dockerfile.example new file mode 100644 index 00000000..3610ba1c --- /dev/null +++ b/Dockerfile.example @@ -0,0 +1,24 @@ +FROM node:0.12 + +MAINTAINER Nightscout Contributors + +RUN apt-get update && \ + apt-get -y dist-upgrade && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y \ + git \ + nodejs \ + nodejs-legacy \ + npm && \ + apt-get -y clean + +RUN mkdir -p /opt/app +ADD . /opt/app +WORKDIR /opt/app + +RUN npm install && \ + npm run postinstall && \ + npm run env + +EXPOSE 1337 + +CMD ["node", "server.js"] diff --git a/Makefile b/Makefile index 44e16a9d..919cb6b2 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,8 @@ ISTANBUL=./node_modules/.bin/istanbul ANALYZED=./coverage/lcov.info export CODACY_REPO_TOKEN=e29ae5cf671f4f918912d9864316207c +DOCKER_IMAGE=oskarpearson/cgm-remote-monitor-testing + all: test coverage: @@ -45,4 +47,26 @@ travis: NODE_ENV=test ${MONGO_SETTINGS} \ ${ISTANBUL} cover ${MOCHA} --report lcovonly -- -R tap ${TESTS} -.PHONY: all coverage report test travis +docker_release: + # Get the version from the package.json file + $(eval DOCKER_TAG=$(shell cat package.json | jq '.version' | tr -d '"')) + # + # Rebuild the image. We do this with no-cache so that we have all security upgrades, + # since that's more important than fewer layers in the Docker image. + docker build --no-cache=true -f Dockerfile.example -t $(DOCKER_IMAGE):$(DOCKER_TAG) . + # Push an image to Docker Hub with the version from package.json: + docker push $(DOCKER_IMAGE):$(DOCKER_TAG) + # + # Push the master branch to Docker hub as 'latest' + if [ "$(TRAVIS_BRANCH)" = "master" ]; then \ + docker tag $(DOCKER_IMAGE):$(DOCKER_TAG) $(DOCKER_IMAGE):latest && \ + docker push $(DOCKER_IMAGE):latest; \ + fi + # + # Push the dev branch to Docker Hub as 'latest_dev' + if [ "$(TRAVIS_BRANCH)" = "dev" ]; then \ + docker tag $(DOCKER_IMAGE):$(DOCKER_TAG) $(DOCKER_IMAGE):latest_dev && \ + docker push $(DOCKER_IMAGE):latest_dev; \ + fi + +.PHONY: all coverage docker_release report test travis