|
1 | 1 | # 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'. |
3 | 4 | name: Publish release image |
4 | 5 |
|
5 | 6 | on: |
@@ -43,28 +44,25 @@ jobs: |
43 | 44 | imageTag = 'latest'; |
44 | 45 | } |
45 | 46 |
|
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') { |
56 | 49 | runPublish = true; |
57 | | - imageTag = 'latest'; |
58 | | - } |
| 50 | + imageTag = event.release.tag_name; |
59 | 51 |
|
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'; |
64 | 65 | } |
65 | | - runPublish = true; |
66 | | - imageTag = context.ref.replace('refs/tags/', ''); |
67 | | - additionalTag = 'latest'; |
68 | 66 | } |
69 | 67 |
|
70 | 68 | console.log(`Outputs: run_publish=${runPublish} image_tag=${imageTag} additional_tag=${additionalTag}`); |
@@ -182,27 +180,31 @@ jobs: |
182 | 180 | DOCKER_USER: ${{ secrets.DOCKER_USER }} |
183 | 181 | DOCKER_PASS: ${{ secrets.DOCKER_PASS }} |
184 | 182 | 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 | +
|
185 | 196 | 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