Skip to content

Commit e656995

Browse files
authored
Add labels, use builkit locally, clean up (#70)
* Ensure DOCKER_BUILDKIT=1 is set locally to match CI. * Add labels to be able to identify build timestamp and git commit hash. * Support testing locally from a detached head. * Minor clean up.
1 parent 563993d commit e656995

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
password: ${{ secrets.GITHUB_TOKEN }}
3535
- name: Build images
3636
id: build
37-
run: DOCKER_BUILDKIT=1 ./build
37+
run: ./build
3838
- name: Test images
3939
run: ./build --test
4040
- name: Push images

build

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ readonly VARIANTS=(
2424
# of the git branch otherwise. Sanitization is a conversion to lowercase, and
2525
# replacing all slashes with underscores, which is the same sanitization Docker
2626
# Hub does for their own builds.
27-
readonly GIT_BRANCH="${GITHUB_REF_NAME:-$(git branch --show-current)}"
27+
GIT_BRANCH="${GITHUB_REF_NAME:-$(git branch --show-current)}"
28+
# Fallback to local if there is no branch (e.g. dettached development)
29+
readonly GIT_BRANCH="${GIT_BRANCH:-local}"
2830
if [[ ${GIT_BRANCH} = master ]]; then
2931
TAG_PREFIX=""
3032
else
@@ -33,22 +35,43 @@ else
3335
TAG_PREFIX="${TAG_PREFIX//\//_}"
3436
fi
3537

38+
# Cache the same RFC 3339 timestamp for re-use in all images built in the same batch.
39+
BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
40+
GIT_HEAD_REF="$(git show-ref --head --hash ^HEAD)"
41+
42+
# Use buildkit to match CI as closely as possible.
43+
export DOCKER_BUILDKIT=1
44+
45+
# docker build wrapper with common arguments
46+
# See https://github.com/opencontainers/image-spec/blob/main/annotations.md for common labels
47+
# See https://docs.github.com/en/packages/learn-github-packages/connecting-a-repository-to-a-package
48+
function docker_build() {
49+
local target="$1"
50+
local tag="$2"
51+
shift
52+
shift
53+
docker build \
54+
--platform linux/amd64 \
55+
--label org.opencontainers.image.created="$BUILD_DATE" \
56+
--label org.opencontainers.image.source=https://github.com/DataDog/dd-trace-java-docker-build \
57+
--label org.opencontainers.image.revision="$GIT_HEAD_REF" \
58+
--target "$target" \
59+
--tag "$tag" \
60+
"$@" \
61+
.
62+
}
63+
3664
function image_name() {
3765
local variant="${1}"
3866
echo -n "${IMAGE_NAME}:${TAG_PREFIX}${variant}"
3967
}
4068

4169
function do_build() {
42-
docker build \
43-
--platform linux/amd64 \
44-
--target base \
45-
--tag "$(image_name base)" .
46-
docker build \
47-
--platform linux/amd64 \
48-
--target full \
49-
--tag "$(image_name latest)" .
70+
docker_build base "$(image_name base)"
71+
docker_build full "$(image_name latest)"
5072
if [ -n "${GITHUB_OUTPUT+unset}" ]; then
51-
echo "LATEST_IMAGE_TAG=$(image_name latest)" >> $GITHUB_OUTPUT
73+
# Make LATEST_IMAGE_TAG available to later GitHub Actions jobs
74+
echo "LATEST_IMAGE_TAG=$(image_name latest)" >>"$GITHUB_OUTPUT"
5275
fi
5376
for variant in "${BASE_VARIANTS[@]}"; do
5477
variant_lower="${variant,,}"
@@ -57,12 +80,9 @@ function do_build() {
5780
for variant in "${VARIANTS[@]}"; do
5881
variant_upper="${variant^^}"
5982
variant_lower="${variant,,}"
60-
docker build \
61-
--build-arg "VARIANT_UPPER=${variant_upper}" \
62-
--build-arg "VARIANT_LOWER=${variant_lower}" \
63-
--platform linux/amd64 \
64-
--target variant \
65-
--tag "$(image_name "${variant_lower}")" .
83+
docker_build variant "$(image_name "$variant_lower")" \
84+
--build-arg "VARIANT_UPPER=$variant_upper" \
85+
--build-arg "VARIANT_LOWER=$variant_lower"
6686
done
6787
}
6888

0 commit comments

Comments
 (0)