Skip to content

Commit d4b6c8d

Browse files
committed
Configure Travis to lint, build and push images to Docker Hub
1 parent d77f880 commit d4b6c8d

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

.travis.yml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
dist: xenial
2-
3-
sudo: required
1+
os: linux
42

53
language: python
64

@@ -19,19 +17,42 @@ addons:
1917
packages:
2018
- docker-ce
2119

20+
cache:
21+
directories:
22+
- $HOME/.cache/pip
23+
- $HOME/.cache/pre-commit
24+
2225
branches:
2326
only:
2427
- master
2528

29+
stages:
30+
- name: lint
31+
- name: test
32+
2633
env:
27-
matrix:
34+
global:
35+
- LATEST_RELEASE=11-alpine
36+
- DOCKER_REPO=tecnativa/postgres-autoconf
37+
jobs:
2838
- DOCKER_TAG=9.6-alpine
2939
- DOCKER_TAG=10-alpine
3040
- DOCKER_TAG=11-alpine
31-
- DOCKER_TAG=alpine
41+
42+
jobs:
43+
include:
44+
stage: lint
45+
script:
46+
- pre-commit run --all --show-diff-on-failure
3247

3348
install:
3449
- pip install -r tests/ci-requirements.txt
3550

3651
script:
3752
- ./tests/test.py -v
53+
54+
deploy:
55+
provider: script
56+
script: ./hooks/push
57+
on:
58+
branch: master

hooks/build

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
#!/usr/bin/env bash
2-
set -ex
1+
#!/usr/bin/env python3
2+
from plumbum import FG, local
3+
from plumbum.cmd import date, docker
34

45
# Check environment variables are present
5-
test -n "$DOCKER_TAG"
6+
DOCKER_TAG = local.env["DOCKER_TAG"]
7+
COMMIT = local.env.get("GIT_SHA1", local.env.get("TRAVIS_COMMIT"))
8+
DATE = date("--rfc-3339", "ns")
69

7-
# Prepare build args
8-
commit="${GIT_SHA1:-$TRAVIS_COMMIT}"
9-
10-
# To be configured via Docker Hub UI, all labels from postgres image targeting
11-
# this master branch
12-
time docker image build \
13-
--build-arg VCS_REF="$commit" \
14-
--build-arg BUILD_DATE="$(date --rfc-3339 ns)" \
15-
--build-arg BASE_TAG="$DOCKER_TAG" \
16-
--tag "tecnativa/postgres-autoconf:$DOCKER_TAG" \
17-
.
10+
# Build image
11+
docker[
12+
"image",
13+
"build",
14+
"--build-arg",
15+
"VCS_REF={}".format(COMMIT),
16+
"--build-arg",
17+
"BUILD_DATE={}".format(DATE),
18+
"--build-arg",
19+
"BASE_TAG={}".format(DOCKER_TAG),
20+
"--tag",
21+
"tecnativa/postgres-autoconf:{}".format(DOCKER_TAG),
22+
".",
23+
] & FG

hooks/push

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
from plumbum import FG, local
3+
from plumbum.cmd import docker
4+
5+
REPO = local.env["DOCKER_REPO"]
6+
VERSION = local.env["DOCKER_TAG"]
7+
8+
# Log all locally available images; will help to pin images
9+
docker["image", "ls", "--digests", REPO] & FG
10+
11+
# Login in Docker Hub
12+
docker(
13+
"login",
14+
"--username",
15+
local.env["DOCKER_HUB_USERNAME"],
16+
"--password",
17+
local.env["DOCKER_HUB_TOKEN"],
18+
)
19+
20+
# Push built images
21+
versioned_image = "%s:%s" % (REPO, VERSION)
22+
docker["image", "push", versioned_image] & FG
23+
if VERSION == local.env.get("LATEST_RELEASE"):
24+
latest_version = "alpine" if VERSION.endswith("-alpine") else "latest"
25+
latest_image = "%s:%s" % (REPO, latest_version)
26+
docker["image", "tag", versioned_image, latest_image] & FG
27+
docker["image", "push", latest_image] & FG

tests/ci-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
plumbum
2+
pre-commit

0 commit comments

Comments
 (0)