Skip to content

Commit 1ebdb21

Browse files
authored
Fixes docker image tagging (#2224)
This attempts to simplify and tidy-up the logic for image labelling. We would like the following behaviour: - A build on `master` updates the `unstable` tag - A build on a tag updates the `tag` tag, and also sets it to `latest` if it has decided it's the latest tag. The caveat above is that if we have released _0.23.0_ and then choose to release _0.22.100_`, we don't want _0.22.100_ to become the `latest` release. Only the "next" version. Consider: ```sh > cat a.txt 0.22.100 0.23.00 0.22.1 > sort -V a.txt 0.22.1 0.22.100 0.23.00 ``` Fixes #2224 --- <!-- Consider each and tick it off one way or the other --> * [x] CHANGELOG updated or not needed * [x] Documentation updated or not needed * [x] Haddocks updated or not needed * [x] No new TODOs introduced or explained herafter
2 parents e21238b + 671c105 commit 1ebdb21

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

.github/workflows/docker.yaml

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,43 +49,78 @@ jobs:
4949
with:
5050
authToken: '${{ secrets.CACHIX_CARDANO_SCALING_AUTH_TOKEN }}'
5151

52-
- name: 🔨 Build image using nix
52+
- name: 🔨 🏷️ Build and tag images
5353
run: |
54+
5455
IMAGE_NAME=ghcr.io/${{github.repository_owner}}/${{matrix.target}}
5556
echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV
5657
nix build .#docker-${{ matrix.target }}
5758
docker load < result
5859
59-
# Determine whether we are building a tag and if yes, set a VERSION_NAME
60+
# Compute tags. We want the following behaviour:
61+
#
62+
# 1. Building on tag:
63+
#
64+
# a. Image tagged `<tag>`
65+
#
66+
# b. If the tag is the "latest" one for the present major release,
67+
# then also tag it as "latest"
68+
#
69+
# 3. Building on master: image tagged `unstable`.
70+
71+
# Let's default to 'unstable'.
72+
IMAGE_LABEL=unstable
73+
# And the version as the git commit.
74+
VERSION=${{github.sha}}
75+
76+
# Determine whether we are building a tag and if yes, set the label
77+
# name to be the tag name, and the version to be the tag.
6078
BUILDING_TAG=${{github.ref_type == 'tag'}}
6179
[[ ${BUILDING_TAG} = true ]] && \
62-
VERSION_NAME=${{github.ref_name}}
80+
IMAGE_LABEL=${{github.ref_name}} && \
81+
VERSION=${{github.ref_name}}
6382
64-
BUILDING_ON_MASTER=false
65-
if (git merge-base --is-ancestor HEAD origin/master); then
66-
BUILDING_ON_MASTER=true
67-
fi
83+
# If we're running on master, tag it as "unstable" and
84+
# and leave the version as the git commit.
85+
[[ ${GITHUB_REF_NAME} = "master" ]] && \
86+
IMAGE_LABEL=unstable
6887
69-
# Use 'FROM' instruction to use docker build with --label
88+
# Use 'FROM' instruction to use docker build with --label, as
89+
# well as setting the tag directly.
7090
echo "FROM ${{matrix.target}}" | docker build \
7191
--label org.opencontainers.image.source=https://github.com/cardano-scaling/hydra \
7292
--label org.opencontainers.image.licenses=Apache-2.0 \
7393
--label org.opencontainers.image.created=$(date -Is) \
7494
--label org.opencontainers.image.revision=${{github.sha}} \
75-
--label org.opencontainers.image.version=${VERSION_NAME:-unstable} \
76-
--tag ${IMAGE_NAME}:unstable -
77-
78-
# Also tag with 'latest' if we are building on master
79-
[[ ${BUIDING_TAG} && ${BUILDING_ON_MASTER} = true ]] && \
80-
docker tag ${IMAGE_NAME}:unstable ${IMAGE_NAME}:latest
81-
# Also tag with version if we are building a tag
82-
[[ ${BUILDING_TAG} = true && ${{matrix.target}} != "hydraw" ]] && \
83-
docker tag ${IMAGE_NAME}:unstable ${IMAGE_NAME}:${VERSION_NAME}
95+
--label org.opencontainers.image.version=${VERSION} \
96+
--tag ${IMAGE_NAME}:${IMAGE_LABEL} -
97+
98+
# Tag the build as 'latest' if it's the latest tag.
99+
if [[ ${BUILDING_TAG} = true ]]; then
100+
# Build a sorted list of tags . We have to grep-exclude some tags
101+
# that we don't want to include; i.e. ones that don't point to our
102+
# actual releases.
103+
git tag -l | grep -P '^\d+.\d+.\d+$' >tags.txt
104+
105+
# Get the largest one (sort -V sorts as versions).
106+
sort -V tags.txt | tail -n1 >latest.txt
107+
108+
# Compare it to mine; and if it's the same, then I am the latest, so
109+
# we can tag it as such.
110+
echo ${{ github.ref_name }}>mine.txt
111+
112+
# Note: This will also print the diff if it fails, which is useful
113+
# for debugging.
114+
if (diff mine.txt latest.txt); then
115+
docker tag ${IMAGE_NAME}:${IMAGE_LABEL} ${IMAGE_NAME}:latest
116+
fi
117+
fi
118+
84119
# Also tag with ref name when manually dispatched
85120
[[ ${{github.event_name == 'workflow_dispatch'}} = true ]] && \
86-
docker tag ${IMAGE_NAME}:unstable ${IMAGE_NAME}:workflow_dispatch-${{github.event.inputs.ref_name}}
121+
docker tag ${IMAGE_NAME}:${IMAGE_LABEL} ${IMAGE_NAME}:workflow_dispatch-${{github.event.inputs.ref_name}}
87122
88-
docker inspect ${IMAGE_NAME}:unstable
123+
docker inspect ${IMAGE_NAME}:${VERSION_NAME}
89124
docker images
90125
91126
- name: 📤 Push to registry

0 commit comments

Comments
 (0)