Skip to content

Commit

Permalink
Build on Travis and push from there to Docker Hub
Browse files Browse the repository at this point in the history
- Updated `.travis.yml` file.
- Use pre-commit as a linter stage now.
- Transform some build scripts to plumbum, to make them more maintainable.
  • Loading branch information
yajo committed Oct 25, 2019
1 parent b2286b8 commit 4b36bef
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 47 deletions.
30 changes: 27 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sudo: required
os: linux

language: python

Expand All @@ -11,15 +11,26 @@ services:
git:
depth: 1

cache:
directories:
- $HOME/.cache/pip
- $HOME/.cache/pre-commit

branches:
only:
- master

stages:
- name: lint
- name: test

env:
global:
# Indicates what's the equivalent to tecnativa/doodba:latest image
- LATEST_RELEASE=13.0
# Variables found by default in Docker Hub builder
- DOCKER_REPO=tecnativa/doodba
matrix:
jobs:
- DOCKER_TAG=7.0 PG_VERSIONS=9.6
- DOCKER_TAG=8.0 PG_VERSIONS=10
- DOCKER_TAG=9.0 PG_VERSIONS=11
Expand All @@ -28,10 +39,17 @@ env:
- DOCKER_TAG=12.0
- DOCKER_TAG=13.0

jobs:
include:
stage: lint
install: []
script:
- pre-commit run --all --show-diff-on-failure

before_install:
- sudo apt-get update
- sudo apt-get -y -o Dpkg::Options::=--force-confnew install docker-ce
- pip install -r requirements-ci.txt
- ./hooks/pre_build

install:
- chown -R $USER:$USER .
Expand All @@ -40,3 +58,9 @@ install:

script:
- python -m unittest -v tests

deploy:
provider: script
script: ./hooks/push
on:
branch: master
58 changes: 33 additions & 25 deletions hooks/build
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
#!/bin/bash
set -ex
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from plumbum import FG, local

# Get Odoo version
version="$DOCKER_TAG"
if [ "$version" == latest ]; then
version=12.0
fi
REPO = local.env["DOCKER_REPO"]
VERSION = local.env["DOCKER_TAG"]
ROOT = local.path(__file__).up(2)
COMMIT = local.env.get("GIT_SHA1", local.env.get("TRAVIS_COMMIT", ""))

if [ -z "$IMAGE_NAME" ]; then
IMAGE_NAME="$DOCKER_REPO:$DOCKER_TAG"
fi
# Shortcuts
build = local["time"]["docker", "image", "build"]
date = local["date"]["--rfc-3339", "ns"]
tag = local["docker"]["image", "tag"]

# See http://label-schema.org/rc1/#build-time-labels
for target in base onbuild; do
suffix=''
if [ "$target" == onbuild ]; then
suffix="-$target"
fi
time docker image build \
--build-arg VCS_REF="$GIT_SHA1" \
--build-arg BUILD_DATE="$(date --rfc-3339 ns)" \
--build-arg ODOO_VERSION="$version" \
--file "$version.Dockerfile" \
--tag "$IMAGE_NAME$suffix" \
--target $target \
.
done
# Build base and onbuild images
for target, suffix in (("base", ""), ("onbuild", "-onbuild")):
image_name = "%s:%s%s" % (REPO, VERSION, suffix)
build[
"--build-arg",
"VCS_REF=%s" % COMMIT,
"--build-arg",
"BUILD_DATE=%s" % date().strip(),
"--build-arg",
"ODOO_VERSION=%s" % VERSION,
"--tag",
image_name,
"--target",
target,
"--file",
"%s.Dockerfile" % VERSION,
ROOT,
] & FG

# Tag "latest" image if applicable
if VERSION == local.env.get("LATEST_RELEASE"):
tag[image_name, "%s:latest%s" % (REPO, suffix)] & FG
14 changes: 0 additions & 14 deletions hooks/post_push

This file was deleted.

3 changes: 3 additions & 0 deletions hooks/pre_build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -ex
pip install -r "$(dirname $0)/../requirements-ci.txt"
5 changes: 0 additions & 5 deletions hooks/pre_push

This file was deleted.

25 changes: 25 additions & 0 deletions hooks/push
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from plumbum import FG, local
from plumbum.cmd import docker # noqa pylint: disable=import-error

REPO = local.env["DOCKER_REPO"]
VERSION = local.env["DOCKER_TAG"]

# 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 images
for suffix in ("", "-onbuild"):
docker["image", "push", "%s:%s%s" % (REPO, VERSION, suffix)] & FG
if VERSION == local.env.get("LATEST_RELEASE"):
docker["image", "push", "%s:latest%s" % (REPO, suffix)] & FG
1 change: 1 addition & 0 deletions requirements-ci.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docker-compose
plumbum
pre-commit

0 comments on commit 4b36bef

Please sign in to comment.