Skip to content

Commit

Permalink
Merge branch '23-correctly-push-multi-arch-images' into 'master'
Browse files Browse the repository at this point in the history
23 – Correctly push multi-arch images

Closes #23

See merge request divio/cloud/base-images!22
  • Loading branch information
GaretJax committed May 25, 2023
2 parents 39ff3c8 + da0e9fe commit 4aa226a
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 59 deletions.
49 changes: 18 additions & 31 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@ linting:
-print0 | xargs -0 -I %
sh -c 'LINT_FILE_DOCKER=% /bin/lint --check --run=docker'

build-dev:
.build:
stage: build
needs: []
variables:
TARGET: dev
IMAGE_NAME: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}-dev
before_script:
- wget -O /usr/bin/docker-buildx ${BUILDX_URL}
- chmod +x /usr/bin/docker-buildx
- ls -al /certs /certs/client
- docker info
- docker-buildx version
- docker-buildx create --use
- docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
- >
mkdir -p /usr/libexec/docker/cli-plugins
wget -q -O /usr/libexec/docker/cli-plugins/docker-buildx ${BUILDX_URL}
chmod +x /usr/libexec/docker/cli-plugins/docker-buildx
docker buildx version
docker buildx create --use
docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
script:
- >
docker-buildx build
docker buildx build
--platform ${PLATFORMS}
--build-arg TARGET=${TARGET}
--tag ${IMAGE_NAME}
Expand All @@ -50,29 +47,17 @@ build-dev:
only:
- tags

build-dev:
extends: .build
variables:
TARGET: dev
IMAGE_NAME: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}-dev

build-prod:
stage: build
needs: []
extends: .build
variables:
TARGET: prod
IMAGE_NAME: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}
before_script:
- wget -O /usr/bin/docker-buildx ${BUILDX_URL}
- chmod +x /usr/bin/docker-buildx
- docker-buildx version
- docker-buildx create --use
- docker info
- docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
script:
- >
docker-buildx build
--platform ${PLATFORMS}
--build-arg TARGET=${TARGET}
--tag ${IMAGE_NAME}
--push
${CI_COMMIT_TAG#*-}
only:
- tags

test-dev:
stage: test
Expand Down Expand Up @@ -108,13 +93,15 @@ push:
- >
/skopeo
copy
--multi-arch all
--src-creds=${SRC_REGISTRY_CREDS}
--dest-creds=${DST_REGISTRY_CREDS}
docker://${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}-dev
docker://${PUBLIC_REPOSITORY}:${CI_COMMIT_TAG}-dev
- >
/skopeo
copy
--multi-arch all
--src-creds=${SRC_REGISTRY_CREDS}
--dest-creds=${DST_REGISTRY_CREDS}
docker://${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========


2023-05-24
----------

* Removed `--arch` param from `./build.py` in favor of Docker's
`DOCKER_DEFAULT_PLATFORM` environment variable.


2023-05-11
----------

Expand Down
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ To locally build an image, run the following command::

./build.py --repo divio/base --tag 0.00-py3.6-alpine3.7 build

To build for a specific architecture (eg. ARM64), add `--arch` flag::
To build for a different architecture than your machine's one, set the
`DOCKER_DEFAULT_PLATFORM` environment variable::

./build.py --repo divio/base --arch arm64 --tag 1.1-py3.11-slim-bullseye build
DOCKER_DEFAULT_PLATFORM=linux/arm64 ./build.py --repo divio/base --tag 1.1-py3.11-slim-bullseye build

Check `./build.py --help` for additional information.

Expand Down
13 changes: 2 additions & 11 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ def get_context_path_from_tag(tag):
return directory


def get_build_command(repo, tag, target, arch):
def get_build_command(repo, tag, target):
return [
"docker",
"build",
"-t",
get_image_name(repo, tag, target),
"--build-arg",
"TARGET={}".format(target),
"--build-arg",
"--no-cache",
"TARGETARCH={}".format(arch),
get_context_path_from_tag(tag=tag),
]

Expand Down Expand Up @@ -124,11 +122,6 @@ def main():
default=os.environ.get("TARGET", "prod"),
help="The build target (dev or prod).",
)
parser.add_argument(
"--arch",
default=os.environ.get("ARCH", "amd64"),
help="The build architecture (amd64, arm64, x86_64 etc).",
)

args = parser.parse_args()

Expand All @@ -144,7 +137,6 @@ def main():
repo = str(args.repo)
tag = str(args.tag)
target = str(args.target)
arch = str(args.arch)
if not (repo and tag and target):
print("Missing parameters!")
exit(code=1)
Expand All @@ -157,15 +149,14 @@ def main():

if operation == "build":
command = get_build_command(
repo=repo, tag=tag, target=target, arch=arch
repo=repo, tag=tag, target=target
)
elif operation == "test":
command = get_test_command(repo=repo, tag=tag, target=target)

print("repo: {}".format(repo))
print("tag: {}".format(tag))
print("target: {}".format(target))
print("arch: {}".format(arch))
print("command:")
print(" ".join(command))
os.execvp(command[0], command)
Expand Down
2 changes: 1 addition & 1 deletion py3.10-slim-bullseye/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM python:3.10.11-slim-bullseye AS build

ARG TARGET=prod
ARG TARGETARCH=amd64
ARG TARGETARCH

ENV PYTHONUNBUFFERED=1 \
PATH=/root/.local/bin:$PATH \
Expand Down
2 changes: 1 addition & 1 deletion py3.11-slim-bullseye/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM python:3.11.3-slim-bullseye AS build

ARG TARGET=prod
ARG TARGETARCH=amd64
ARG TARGETARCH

ENV PYTHONUNBUFFERED=1 \
PATH=/root/.local/bin:$PATH \
Expand Down
5 changes: 3 additions & 2 deletions py3.7-alpine3.12/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN apk update && apk upgrade
# Dependencies
# hadolint ignore=DL3018
RUN apk add \
blas \
cairo \
curl \
freetype \
Expand All @@ -22,6 +23,7 @@ RUN apk add \
libxml2 \
libxslt \
mailcap \
openblas \
openjpeg \
pcre \
postgresql-client \
Expand All @@ -35,7 +37,6 @@ RUN apk add \
RUN if [ "$TARGET" = "dev" ] ; then apk add \
autoconf \
automake \
blas-dev \
cairo-dev \
cargo \
cmake \
Expand All @@ -57,7 +58,7 @@ RUN if [ "$TARGET" = "dev" ] ; then apk add \
linux-headers \
make \
musl-dev \
ninja-build \
openblas-dev \
openjpeg-dev \
pcre-dev \
pkgconf \
Expand Down
2 changes: 1 addition & 1 deletion py3.8-slim-buster/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM python:3.8.16-slim-buster AS build

ARG TARGET=prod
ARG TARGETARCH=amd64
ARG TARGETARCH

ENV PYTHONUNBUFFERED=1 \
PATH=/root/.local/bin:$PATH \
Expand Down
4 changes: 3 additions & 1 deletion py3.9-alpine3.13/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN apk update && apk upgrade
# Dependencies
# hadolint ignore=DL3018
RUN apk add \
blas \
curl \
freetype \
gdal \
Expand All @@ -20,6 +21,7 @@ RUN apk add \
libxml2 \
libxslt \
mailcap \
openblas \
openjpeg \
pcre \
postgresql-client \
Expand Down Expand Up @@ -55,7 +57,7 @@ RUN if [ "$TARGET" = "dev" ] ; then apk add \
linux-headers \
make \
musl-dev \
ninja-build \
openblas-dev \
openjpeg-dev \
pcre-dev \
pkgconf \
Expand Down
2 changes: 1 addition & 1 deletion py3.9-slim-buster/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM python:3.9.16-slim-buster AS build

ARG TARGET=prod
ARG TARGETARCH=amd64
ARG TARGETARCH

ENV PYTHONUNBUFFERED=1 \
PATH=/root/.local/bin:$PATH \
Expand Down
16 changes: 8 additions & 8 deletions release.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#! /usr/bin/env python2
# This script uses python 2.7.6 and only the standardlib because that is what
# is available on in the context of the build hook on docker cloud / dockerhub.
#! /usr/bin/env python
# This script uses only the standardlib because that is what
# is available on in the context of the CI/CD pipeline.
import os
import sys
import argparse
import subprocess
from distutils.version import StrictVersion
from packaging.version import Version, parse


def get_tags(suffix=None):
Expand All @@ -17,7 +17,7 @@ def get_tags(suffix=None):


def extract_versions(tags):
return sorted(StrictVersion(t.split("-", 1)[0]) for t in tags)
return sorted(parse(t.split("-", 1)[0]) for t in tags)


def versions(args):
Expand All @@ -41,16 +41,16 @@ def versions(args):
print(flavor)
if versions:
version = versions[-1]
major, minor, patch = version.version
major, minor, patch = version.major, version.minor, version.micro
patch = 0
if args.next == "minor":
minor += 1
elif args.next == "major":
major += 1
minor = 0
version.version = major, minor, patch
version = Version(f"{major}.{minor}")
else:
version = StrictVersion("1.0")
version = Version("1.0")
if args.tag:
tag = "{}-{}".format(version, flavor)
print(
Expand Down

0 comments on commit 4aa226a

Please sign in to comment.