name: Prepare release
on:
push:
branches:
- main
release:
types: [published]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
permissions:
contents: read
packages: write
jobs:
pre-check:
runs-on: ubuntu-22.04
outputs:
skip-workflow: ${{ steps.check.outputs.skip-workflow }}
steps:
- name: Check if workflow should be skipped
id: check
run: |
if [[ "${{ github.event.head_commit.author.name }}" == "github-actions[bot]" ]] && [[ "${{ github.event.head_commit.message }}" == Release\ version* ]]; then
echo "skip-workflow=true" >> $GITHUB_OUTPUT
echo "Skipping this workflow..."
else
echo "skip-workflow=false" >> $GITHUB_OUTPUT
echo "Proceeding with this workflow..."
fi
build-and-push-image:
runs-on: ubuntu-22.04
needs: [pre-check]
if: ${{ !startsWith(github.ref, 'refs/tags/') && needs.pre-check.outputs.skip-workflow == 'false' }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix=sha-
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
tag-release-image:
runs-on: ubuntu-22.04
needs: [pre-check]
if: ${{ startsWith(github.event.release.tag_name, 'v') && needs.pre-check.outputs.skip-workflow == 'false' }}
steps:
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get latest SHA image
id: get-sha
run: |
# Get the latest commit SHA from the main branch
LATEST_SHA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/commits/main" | \
jq -r '.sha' | cut -c1-7)
echo "latest_sha=$LATEST_SHA" >> $GITHUB_OUTPUT
echo "Latest SHA: $LATEST_SHA"
- name: Pull, retag and push release image
run: |
# Ensure image names are all lowercase
REPO_NAME_LOWER=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]')
# Source image with SHA tag
SOURCE_IMAGE="${{ env.REGISTRY }}/${REPO_NAME_LOWER}:sha-${{ steps.get-sha.outputs.latest_sha }}"
# Target image with release tag
TARGET_IMAGE="${{ env.REGISTRY }}/${REPO_NAME_LOWER}:${{ github.event.release.tag_name }}"
# Latest tag
LATEST_IMAGE="${{ env.REGISTRY }}/${REPO_NAME_LOWER}:latest"
echo "Pulling source image: $SOURCE_IMAGE"
docker pull $SOURCE_IMAGE
echo "Tagging as: $TARGET_IMAGE"
docker tag $SOURCE_IMAGE $TARGET_IMAGE
echo "Tagging as: $LATEST_IMAGE"
docker tag $SOURCE_IMAGE $LATEST_IMAGE
echo "Pushing release image: $TARGET_IMAGE"
docker push $TARGET_IMAGE
echo "Pushing latest image: $LATEST_IMAGE"
docker push $LATEST_IMAGE
upload-release-assets:
runs-on: ubuntu-22.04
if: ${{ startsWith(github.event.release.tag_name, 'v') }}
needs: [tag-release-image]
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Create versioned Dockerfile
run: |
# Create a versioned Dockerfile with the release tag as default ARG value
sed "s/^ARG VERSION$/ARG VERSION=${{ github.event.release.tag_name }}/" Dockerfile > Dockerfile.release
mv Dockerfile.release Dockerfile
- name: Upload release asset files
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
files: Dockerfile
bump-package-version:
runs-on: ubuntu-22.04
if: ${{ startsWith(github.event.release.tag_name, 'v') }}
needs: [tag-release-image]
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.GH_ACTIONS_PUSH_TO_MAIN }}
persist-credentials: true
- name: Update package.json
run: |
version_number=$(echo "${{ github.event.release.tag_name }}" | sed 's/^v//')
yq -i -o=json ".version = \"${version_number}\"" package.json
- name: Update version.ts
run: |
version_number=$(echo "${{ github.event.release.tag_name }}" | sed 's/^v//')
sed -i "s/export const VERSION = \".*\";/export const VERSION = \"${version_number}\";/" src/version.ts
- name: Commit and push changes
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git add package.json src/version.ts
git commit -m "Release version '${{ github.event.release.tag_name }}'"
- name: Push changes
uses: ad-m/github-push-action@77c5b412c50b723d2a4fbc6d71fb5723bcd439aa
with:
ssh: true
branch: main
tags: true
cleanup:
runs-on: ubuntu-22.04
if: always()
steps:
- uses: dataaxiom/ghcr-cleanup-action@cd0cdb900b5dbf3a6f2cc869f0dbb0b8211f50c4
with:
token: ${{ secrets.GITHUB_TOKEN }}
exclude-tags: '^v[0-9]+\.[0-9]+\.[0-9]+$'
use-regex: true
keep-n-tagged: 5
log-level: info