From 9c2d741749ffd38ae39e87179de0fa8a8628ee76 Mon Sep 17 00:00:00 2001 From: Jeremy Lin Date: Mon, 18 Jan 2021 00:19:00 -0800 Subject: [PATCH] Add `org.opencontainers` labels to Docker images --- hooks/build | 31 +++++++++++++++++++++++++++++++ hooks/pre_build | 24 +++++++++++++++++------- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/hooks/build b/hooks/build index 8680a6f1..eeead8b4 100755 --- a/hooks/build +++ b/hooks/build @@ -4,10 +4,41 @@ echo ">>> Building images..." source ./hooks/arches.sh +if [[ -z "${SOURCE_COMMIT}" ]]; then + # This var is typically predefined by Docker Hub, but it won't be + # when testing locally. + SOURCE_COMMIT="$(git rev-parse HEAD)" +fi + +# Construct a version string in the style of `build.rs`. +GIT_EXACT_TAG="$(git describe --tags --abbrev=0 --exact-match 2>/dev/null)" +if [[ -n "${GIT_EXACT_TAG}" ]]; then + SOURCE_VERSION="${GIT_EXACT_TAG}" +else + GIT_LAST_TAG="$(git describe --tags --abbrev=0)" + SOURCE_VERSION="${GIT_LAST_TAG}-${SOURCE_COMMIT:0:8}" +fi + +LABELS=( + # https://github.com/opencontainers/image-spec/blob/master/annotations.md + org.opencontainers.image.created="$(date --utc --iso-8601=seconds)" + org.opencontainers.image.documentation="https://github.com/dani-garcia/bitwarden_rs/wiki" + org.opencontainers.image.licenses="GPL-3.0-only" + org.opencontainers.image.revision="${SOURCE_COMMIT}" + org.opencontainers.image.source="${SOURCE_REPOSITORY_URL}" + org.opencontainers.image.url="https://hub.docker.com/r/${DOCKER_REPO#*/}" + org.opencontainers.image.version="${SOURCE_VERSION}" +) +LABEL_ARGS=() +for label in "${LABELS[@]}"; do + LABEL_ARGS+=(--label "${label}") +done + set -ex for arch in "${arches[@]}"; do docker build \ + "${LABEL_ARGS[@]}" \ -t "${DOCKER_REPO}:${DOCKER_TAG}-${arch}" \ -f docker/${arch}/Dockerfile${distro_suffix} \ . diff --git a/hooks/pre_build b/hooks/pre_build index b331c8f1..6cb50ab1 100755 --- a/hooks/pre_build +++ b/hooks/pre_build @@ -2,13 +2,15 @@ set -ex -# Print some environment info in case it's useful for troubleshooting. -id -pwd -df -h -env -docker info -docker version +# If requested, print some environment info for troubleshooting. +if [[ -n "${DOCKER_HUB_DEBUG}" ]]; then + id + pwd + df -h + env + docker info + docker version +fi # Install build dependencies. deps=( @@ -16,3 +18,11 @@ deps=( ) apt-get update apt-get install -y "${deps[@]}" + +# Docker Hub uses a shallow clone and doesn't fetch tags, which breaks some +# Git operations that we perform later, so fetch the complete history and +# tags first. Note that if the build is cached, the clone may have been +# unshallowed already; if so, unshallowing will fail, so skip it. +if [[ -f .git/shallow ]]; then + git fetch --unshallow --tags +fi