Skip to content

Merge pull request #959 from kristof-mattei/warn-when-ignoring #4

Merge pull request #959 from kristof-mattei/warn-when-ignoring

Merge pull request #959 from kristof-mattei/warn-when-ignoring #4

name: Update semantic tags on repo & image after release
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
concurrency:
group: "${{ github.workflow }}"
cancel-in-progress: false # last one must win in case of multiple releases
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
permissions:
contents: write
packages: write
jobs:
repo-has-container:
name: Repo has container?
runs-on: ubuntu-latest
outputs:
has_container: ${{ steps.determine.outputs.has_container }}
steps:
- name: Repo has docker container?
id: determine
shell: bash
run: |
HAS_CONTAINER="${{ vars.HAS_CONTAINER }}"
echo "has_container=${HAS_CONTAINER:-false}" >> ${GITHUB_OUTPUT}
retag-containers:
name: Retag the containers
runs-on: ubuntu-latest
needs:
- repo-has-container
if: |
fromJSON(needs.repo-has-container.outputs.has_container) == true
steps:
- name: Check out repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false
- name: Setup Node.js
uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version-file: ".nvmrc"
cache: "npm"
cache-dependency-path: "**/package-lock.json"
- name: Install dependencies
shell: bash
run: |
npm ci --ignore-scripts
- name: Download crane tar, extract, and add folder to path.
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const tc = require("@actions/tool-cache");
const release = await github.rest.repos.getLatestRelease({
owner: "google",
repo: "go-containerregistry"
});
const asset = release.data.assets.find(asset => {
return asset["content_type"] === "application/gzip" && asset.name === "go-containerregistry_Linux_x86_64.tar.gz";
});
const urlToCraneTar = asset.browser_download_url
const craneTarPath = await tc.downloadTool(urlToCraneTar);
const craneExtractedFolder = await tc.extractTar(craneTarPath, null, ["--extract", "--gzip"]);
core.addPath(craneExtractedFolder);
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set full image name
shell: bash
run: |
echo "FULL_IMAGE_NAME=${REGISTRY,,}/${IMAGE_NAME,,}" >> ${GITHUB_ENV}
- name: Find all tags for ${{ env.FULL_IMAGE_NAME }}
shell: bash
run: |
crane ls ${FULL_IMAGE_NAME} >> existing_tags
echo "These are the existing tags on ${FULL_IMAGE_NAME}:"
cat existing_tags
- name: Check if the incoming PR has a Docker container, which will be our old tag
shell: bash
run: |
old_tag=$(cat existing_tags | grep "^sha-${{ github.sha }}-.*\$") # .* is actual or retag
echo "OLD_TAG=${old_tag}" >> ${GITHUB_ENV}
- name: Set the new TAGs
id: meta
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
with:
images: "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
tags: |
type=raw,value=latest
type=semver,pattern=v{{version}}
- name: Actually re-tag the container
shell: bash
run: |
echo "${{ steps.meta.outputs.tags }}" | while read new_tag
do
crane cp "${FULL_IMAGE_NAME}:${OLD_TAG}" ${new_tag}
done