From 5d2131bc80a5ae37a10dd6a5a112a1da8dac35f0 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Tue, 14 Jan 2020 10:05:45 +0100 Subject: [PATCH] Publish docker image(s) from Travis --- .travis.yml | 8 +++++++- hooks/build | 34 +++++++++++++++++++++++++--------- hooks/push | 21 +++++++++++++++++++++ requirements-ci.txt | 1 + 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100755 hooks/push diff --git a/.travis.yml b/.travis.yml index d480836..48b6393 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ sudo: required language: python python: - - "3.6" + - "3.8" services: - docker @@ -41,3 +41,9 @@ install: script: - python -m unittest -v tests + +deploy: + provider: script + script: ./hooks/push + on: + branch: master diff --git a/hooks/build b/hooks/build index 7491bea..cffbcfa 100755 --- a/hooks/build +++ b/hooks/build @@ -1,10 +1,26 @@ -#!/bin/bash -set -ex +#!/usr/bin/env python3 +from plumbum import FG, local -# See http://label-schema.org/rc1/#build-time-labels -time docker image build \ - --build-arg VCS_REF="$GIT_SHA1" \ - --build-arg BUILD_DATE="$(date --rfc-3339 ns)" \ - --build-arg VERSION="$DOCKER_TAG" \ - --tag "$IMAGE_NAME" \ - . +COMMIT = local.env.get("GIT_SHA1", local.env.get("TRAVIS_COMMIT", "")) +IMAGE_NAME = local.env["IMAGE_NAME"] +ROOT = local.path(__file__).up(2) +VERSION = IMAGE_NAME.split(":")[1] + +# Shortcuts +build = local["time"]["docker", "image", "build"] +date = local["date"]["--rfc-3339", "ns"] +tag = local["docker"]["image", "tag"] + +# Build base and onbuild images +build[ + "--tag", + IMAGE_NAME, + # See http://label-schema.org/rc1/#build-time-labels + "--build-arg", + "VCS_REF=%s" % COMMIT, + "--build-arg", + "BUILD_DATE=%s" % date().strip(), + "--build-arg", + "VERSION=%s" % VERSION, + ROOT, +] & FG diff --git a/hooks/push b/hooks/push new file mode 100755 index 0000000..93e9fee --- /dev/null +++ b/hooks/push @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +from plumbum import FG, local +from plumbum.cmd import docker # noqa pylint: disable=import-error + +IMAGE_NAME = local.env["IMAGE_NAME"] +REPO = IMAGE_NAME.split(":")[0] + +# Log all locally available images; will help to pin images +docker["image", "ls", "--digests", REPO] & FG + +# Login in Docker Hub +docker( + "login", + "--username", + local.env["DOCKER_HUB_USERNAME"], + "--password", + local.env["DOCKER_HUB_TOKEN"], +) + +# Push built image +docker["image", "push", IMAGE_NAME] & FG diff --git a/requirements-ci.txt b/requirements-ci.txt index bdb9670..73ab738 100644 --- a/requirements-ci.txt +++ b/requirements-ci.txt @@ -1 +1,2 @@ docker +plumbum