Skip to content

Commit 056565b

Browse files
committed
[shell-operator] update publish-release workflow to build image tags (#832)
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
1 parent 5360439 commit 056565b

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

.github/workflows/publish-release.yaml

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Publish flant/shell-operator image on hub.docker.com.
2-
# Build 'latest' tag when release is published.
2+
# Build tag from release name when release is published.
3+
# Build 'latest' tag when release is published and marked as 'latest'.
34
name: Publish release image
45

56
on:
@@ -43,28 +44,25 @@ jobs:
4344
imageTag = 'latest';
4445
}
4546
46-
// Check for 'skip' label on pull request merge.
47-
if (eventName == 'pull_request' && event.action == 'closed') {
48-
if (!event.pull_request.merged) {
49-
return console.log(`PR ${event.number} not merged. Skip 'publish latest image'.`);
50-
}
51-
const hasSkipLabel = event.pull_request.labels.some(l => l.name === SKIP_LATEST_LABEL_NAME);
52-
if (hasSkipLabel) {
53-
return console.log(`Detect skip label '${SKIP_LATEST_LABEL_NAME}' on merged PR ${event.number}. Skip 'publish latest image'.`);
54-
}
55-
console.log(`No skip labels on merged PR ${event.number}. Run 'publish latest image'.`);
47+
// Check for release is published.
48+
if (eventName === 'release' && event.action === 'published') {
5649
runPublish = true;
57-
imageTag = 'latest';
58-
}
50+
imageTag = event.release.tag_name;
5951
60-
// Check if tag is pushed.
61-
if (eventName == 'push') {
62-
if (!event.ref.startsWith('refs/tags/')) {
63-
return console.log(`Detect non-release ref: ${event.ref}. Skip 'publish latest image'.`);
52+
// Fetch the "latest" release as defined by GitHub
53+
const { data: latestRelease } = await github.rest.repos.getLatestRelease({
54+
owner: context.repo.owner,
55+
repo: context.repo.repo,
56+
});
57+
58+
let isLatest = latestRelease.tag_name === imageTag;
59+
console.log(`DEBUG: latestRelease.tag_name=${latestRelease.tag_name} imageTag=${imageTag} is_latest=${isLatest}`);
60+
61+
// If the release is the "latest" release, build the "latest" tag.
62+
if (isLatest) {
63+
console.log(`Detect current release is the "latest" release. Run 'publish latest image'.`);
64+
additionalTag = 'latest';
6465
}
65-
runPublish = true;
66-
imageTag = context.ref.replace('refs/tags/', '');
67-
additionalTag = 'latest';
6866
}
6967
7068
console.log(`Outputs: run_publish=${runPublish} image_tag=${imageTag} additional_tag=${additionalTag}`);
@@ -182,27 +180,31 @@ jobs:
182180
DOCKER_USER: ${{ secrets.DOCKER_USER }}
183181
DOCKER_PASS: ${{ secrets.DOCKER_PASS }}
184182
run: |
183+
echo "Download crane tool ..."
184+
185+
CRANE_VERSION=$(curl -s "https://api.github.com/repos/google/go-containerregistry/releases/latest" | jq -r '.tag_name')
186+
CRANE_OS=Linux # or Darwin, Windows
187+
CRANE_ARCH=x86_64 # or arm64, x86_64, armv6, i386, s390x, riscv64
188+
189+
echo "Crane version: ${CRANE_VERSION}, OS: ${CRANE_OS}, ARCH: ${CRANE_ARCH}"
190+
curl -sL "https://github.com/google/go-containerregistry/releases/download/${CRANE_VERSION}/go-containerregistry_${CRANE_OS}_${CRANE_ARCH}.tar.gz" > go-containerregistry.tar.gz
191+
echo "Extract crane tool ..."
192+
tar -zxvf go-containerregistry.tar.gz -C /usr/local/bin/ crane
193+
194+
export PATH=$PATH:/usr/local/bin
195+
185196
echo "Copy ${GHCR_IO_IMAGE_NAME} to ${DOCKER_HUB_IMAGE_NAME} ..."
186-
docker run \
187-
-e "DOCKER_USER=${DOCKER_USER}" \
188-
-e "DOCKER_PASS=${DOCKER_PASS}" \
189-
-e "GHCR_IO_IMAGE_NAME=${GHCR_IO_IMAGE_NAME}" \
190-
-e "DOCKER_HUB_IMAGE_NAME=${DOCKER_HUB_IMAGE_NAME}" \
191-
quay.io/skopeo/stable:latest \
192-
copy --all \
193-
--dest-creds ${DOCKER_USER}:${DOCKER_PASS} \
194-
docker://${GHCR_IO_IMAGE_NAME} \
195-
docker://${DOCKER_HUB_IMAGE_NAME}
196-
197-
[ -n "$ADDITIONAL_DOCKER_HUB_IMAGE_NAME" ] && \
198-
echo "Copy ${ADDITIONAL_GHCR_IO_IMAGE_NAME} to ${ADDITIONAL_DOCKER_HUB_IMAGE_NAME} ..." && \
199-
docker run \
200-
-e "DOCKER_USER=${DOCKER_USER}" \
201-
-e "DOCKER_PASS=${DOCKER_PASS}" \
202-
-e "GHCR_IO_IMAGE_NAME=${ADDITIONAL_GHCR_IO_IMAGE_NAME}" \
203-
-e "DOCKER_HUB_IMAGE_NAME=${ADDITIONAL_DOCKER_HUB_IMAGE_NAME}" \
204-
quay.io/skopeo/stable:latest \
205-
copy --all \
206-
--dest-creds ${DOCKER_USER}:${DOCKER_PASS} \
207-
docker://${GHCR_IO_IMAGE_NAME} \
208-
docker://${DOCKER_HUB_IMAGE_NAME}
197+
198+
crane auth login -u ${DOCKER_USER} -p ${DOCKER_PASS} docker.io && \
199+
crane copy ${GHCR_IO_IMAGE_NAME} ${DOCKER_HUB_IMAGE_NAME}
200+
201+
echo "Copy completed."
202+
203+
if [ -n "$ADDITIONAL_DOCKER_HUB_IMAGE_NAME" ] ; then
204+
echo "Copy ${ADDITIONAL_GHCR_IO_IMAGE_NAME} to ${ADDITIONAL_DOCKER_HUB_IMAGE_NAME} ..."
205+
206+
crane auth login -u ${DOCKER_USER} -p ${DOCKER_PASS} docker.io && \
207+
crane copy ${ADDITIONAL_GHCR_IO_IMAGE_NAME} ${ADDITIONAL_DOCKER_HUB_IMAGE_NAME}
208+
209+
echo "Copy completed."
210+
fi

0 commit comments

Comments
 (0)