1#!/bin/bash
2
3echo ">>> Building images..."
4
5source ./hooks/arches.sh
6
7if [[ -z "${SOURCE_COMMIT}" ]]; then
8    # This var is typically predefined by Docker Hub, but it won't be
9    # when testing locally.
10    SOURCE_COMMIT="$(git rev-parse HEAD)"
11fi
12
13# Construct a version string in the style of `build.rs`.
14GIT_EXACT_TAG="$(git describe --tags --abbrev=0 --exact-match 2>/dev/null)"
15if [[ -n "${GIT_EXACT_TAG}" ]]; then
16    SOURCE_VERSION="${GIT_EXACT_TAG}"
17else
18    GIT_LAST_TAG="$(git describe --tags --abbrev=0)"
19    SOURCE_VERSION="${GIT_LAST_TAG}-${SOURCE_COMMIT:0:8}"
20fi
21
22LABELS=(
23    # https://github.com/opencontainers/image-spec/blob/master/annotations.md
24    org.opencontainers.image.created="$(date --utc --iso-8601=seconds)"
25    org.opencontainers.image.documentation="https://github.com/dani-garcia/vaultwarden/wiki"
26    org.opencontainers.image.licenses="GPL-3.0-only"
27    org.opencontainers.image.revision="${SOURCE_COMMIT}"
28    org.opencontainers.image.source="${SOURCE_REPOSITORY_URL}"
29    org.opencontainers.image.url="https://hub.docker.com/r/${DOCKER_REPO#*/}"
30    org.opencontainers.image.version="${SOURCE_VERSION}"
31)
32LABEL_ARGS=()
33for label in "${LABELS[@]}"; do
34    LABEL_ARGS+=(--label "${label}")
35done
36
37# Check if DOCKER_BUILDKIT is set, if so, use the Dockerfile.buildx as template
38if [[ -n "${DOCKER_BUILDKIT}" ]]; then
39    buildx_suffix=.buildx
40fi
41
42set -ex
43
44for arch in "${arches[@]}"; do
45    docker build \
46           "${LABEL_ARGS[@]}" \
47           -t "${DOCKER_REPO}:${DOCKER_TAG}-${arch}" \
48           -f docker/${arch}/Dockerfile${buildx_suffix}${distro_suffix} \
49           .
50done
51