Skip to content

Commit 1d5be19

Browse files
committed
ci: ensure 'push-image' tasks don't overwrite pre-existing tags
1 parent 49cd63b commit 1d5be19

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

taskcluster/docker/skopeo/push_image.sh

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/env bash
22
set -e -x
33

44
test $NAME
@@ -10,6 +10,12 @@ test $TASK_ID
1010
test $VCS_HEAD_REPOSITORY
1111
test $VCS_HEAD_REV
1212

13+
function cleanup() {
14+
echo "=== Clean up ==="
15+
rm -rf $HOME/.docker
16+
}
17+
trap cleanup EXIT
18+
1319
echo "=== Generating dockercfg ==="
1420
PASSWORD_URL="http://taskcluster/secrets/v1/secret/project/taskgraph/level-3/dockerhub"
1521
install -m 600 /dev/null $HOME/.dockercfg
@@ -39,30 +45,33 @@ umoci insert --image ${NAME}:final version.json /version.json
3945
echo "=== Pushing to docker hub ==="
4046
DOCKER_TAG="${NAME}-v${VERSION}"
4147

42-
# Get all remote tags | jq filter only starting with ${NAME}-v | Sort by version | Get the last one
43-
LATEST_REMOTE_VERSION=$(skopeo list-tags docker://$DOCKER_REPO | jq ".Tags[] | select(. | test(\"^${NAME}-v\\\\d\"))" -r | sort -V | tail -1)
48+
# Get all remote tags | jq filter only starting with ${NAME}-v
49+
REMOTE_TAGS=$(skopeo list-tags docker://$DOCKER_REPO | jq ".Tags[] | select(. | test(\"^${NAME}-v\\\\d\"))" -r)
50+
51+
# If remote tag already exists, bail out.
52+
if [[ $REMOTE_TAGS =~ (^|[[:space:]])$DOCKER_TAG($|[[:space:]]) ]]; then
53+
echo "Docker tag '$DOCKER_REPO:$DOCKER_TAG' already exists, aborting!"
54+
exit 1
55+
fi
4456

4557
skopeo copy oci:${NAME}:final docker://$DOCKER_REPO:$DOCKER_TAG
4658
skopeo inspect docker://$DOCKER_REPO:$DOCKER_TAG
4759

60+
# Get latest remote tag | Sort by version | Get the last one
61+
LATEST_REMOTE_VERSION=$(printf "$REMOTE_TAGS" | sort -V | tail -1)
62+
4863
# This bit is intentionally verbose so it's easier to track when we override the latest tag
4964
if [ "${LATEST_REMOTE_VERSION}" = "" ]; then
5065
echo "Couldn't find a remote version. Tagging as latest."
5166
skopeo copy oci:${NAME}:final docker://$DOCKER_REPO:$NAME-latest
52-
elif [ "${LATEST_REMOTE_VERSION}" = "${DOCKER_TAG}" ]; then
53-
echo "Updating latest tag, the latest version on remote matches the provided version."
54-
skopeo copy oci:${NAME}:final docker://$DOCKER_REPO:$NAME-latest
5567
else
5668
# Printf the latest remote version and the current tag | Sort by version | Get the last one
57-
LATEST_VERSION=$(printf "$REMOTE_VERSION\n$DOCKER_TAG" | sort -V | tail -1)
69+
LATEST_VERSION=$(printf "$LATEST_REMOTE_VERSION\n$DOCKER_TAG" | sort -V | tail -1)
5870
# If current tag > latest remote, then we should tag as latest
59-
if [ "${LATEST_VERSION}" != "${REMOTE_VERSION}" ]; then
71+
if [ "${LATEST_VERSION}" != "${LATEST_REMOTE_VERSION}" ]; then
6072
echo "Updating latest tag, the current version is higher than the remote."
6173
skopeo copy oci:${NAME}:final docker://$DOCKER_REPO:$NAME-latest
6274
else
6375
echo "Skipped tagging as current tag is not higher than the remote's latest."
6476
fi
6577
fi
66-
67-
echo "=== Clean up ==="
68-
rm -rf $HOME/.docker

taskcluster/kinds/push-image/kind.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ task-defaults:
1818
using: run-task
1919
checkout: false
2020
use-caches: false
21-
command: "sh /usr/local/bin/push_image.sh"
21+
command: "bash /usr/local/bin/push_image.sh"
2222
fetches:
2323
image:
2424
- artifact: image.tar.zst

0 commit comments

Comments
 (0)