Files
booklore/.github/workflows/docker-build-publish.yml
2025-08-05 16:48:11 -06:00

178 lines
6.6 KiB
YAML

name: Build, Tag, Push, and Release to GitHub Container Registry
on:
push:
branches:
- '**'
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
pull-requests: write
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up QEMU for multi-arch builds
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Get Latest Master Version
id: get_version
run: |
latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1)
latest_tag=${latest_tag:-"v0.0.0"}
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
echo "Latest master tag: $latest_tag"
- name: Determine Version Bump (Only for Master)
if: github.ref == 'refs/heads/master'
id: determine_bump
run: |
# Step 1: Check if the PR has a label (priority over commit message)
pr_number=$(gh pr list --state merged --base master --limit 1 --json number --jq '.[0].number')
echo "PR Number: $pr_number"
labels=$(gh pr view "$pr_number" --json labels --jq '.labels[].name' || echo "")
if echo "$labels" | grep -q 'major'; then
bump="major"
elif echo "$labels" | grep -q 'minor'; then
bump="minor"
elif echo "$labels" | grep -q 'patch'; then
bump="patch"
fi
# Step 2: If no label, fallback to commit message keywords
if [ -z "$bump" ]; then
last_commit_msg=$(git log -1 --pretty=%B)
echo "Last commit message: $last_commit_msg"
if echo "$last_commit_msg" | grep -iq '#major'; then
bump="major"
elif echo "$last_commit_msg" | grep -iq '#minor'; then
bump="minor"
elif echo "$last_commit_msg" | grep -iq '#patch'; then
bump="patch"
else
bump="patch" # Default to patch if no keyword or label
fi
fi
echo "bump=$bump" >> $GITHUB_ENV
echo "Version bump type: $bump"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Auto-Increment Version (Only for Master)
if: github.ref == 'refs/heads/master'
id: bump_version
run: |
old_version="${{ env.latest_tag }}"
IFS='.' read -r -a parts <<< "${old_version//[!0-9.]/}"
if [ "${{ env.bump }}" = "major" ]; then
new_version="$((parts[0] + 1)).0.0"
elif [ "${{ env.bump }}" = "minor" ]; then
new_version="${parts[0]}.$((parts[1] + 1)).0"
else
new_version="${parts[0]}.${parts[1]}.$((parts[2] + 1))"
fi
new_tag="v${new_version}"
echo "new_tag=$new_tag" >> $GITHUB_ENV
echo "New version: $new_tag"
- name: Generate Image Tag
id: set_image_tag
run: |
branch="${GITHUB_REF#refs/heads/}"
if [[ "$branch" == "master" ]]; then
image_tag="${{ env.new_tag }}"
elif [[ "$branch" == "develop" ]]; then
short_sha=$(git rev-parse --short HEAD)
image_tag="${{ env.latest_tag }}-develop-${short_sha}"
else
short_sha=$(git rev-parse --short HEAD)
image_tag="${short_sha}"
fi
echo "image_tag=$image_tag" >> $GITHUB_ENV
echo "Image tag: $image_tag"
- name: Create Git Tag (Only for Master)
if: github.ref == 'refs/heads/master'
run: |
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git tag ${{ env.new_tag }}
git push origin ${{ env.new_tag }}
- name: Build and Push Docker Image (Docker Hub & GHCR)
run: |
docker buildx create --use
docker buildx build \
--platform linux/amd64,linux/arm64 \
--build-arg APP_VERSION=${{ env.image_tag }} \
--tag booklore/booklore-app:${{ env.image_tag }} \
--tag ghcr.io/${{ github.repository }}/booklore-app:${{ env.image_tag }} \
--push .
- name: Push Latest Tag (Only for Master)
if: github.ref == 'refs/heads/master'
run: |
docker buildx build \
--platform linux/amd64,linux/arm64 \
--build-arg APP_VERSION=${{ env.new_tag }} \
--tag booklore/booklore-app:latest \
--tag ghcr.io/${{ github.repository }}/booklore-app:latest \
--push .
- name: Update Release Draft (Only for Master)
if: github.ref == 'refs/heads/master'
uses: release-drafter/release-drafter@v6
with:
tag: ${{ env.new_tag }}
name: "Release ${{ env.new_tag }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish Draft Release (Only for Master)
if: github.ref == 'refs/heads/master'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release edit ${{ env.new_tag }} --draft=false
- name: Notify Discord of New Release
if: github.ref == 'refs/heads/master'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
release_json=$(gh release view ${{ env.new_tag }} --json name,body,url)
release_name=$(echo "$release_json" | jq -r '.name')
release_body=$(echo "$release_json" | jq -r '.body')
release_url=$(echo "$release_json" | jq -r '.url')
dockerhub_image="https://hub.docker.com/r/booklore/booklore-app/tags"
ghcr_image="https://github.com/${{ github.repository }}/packages"
content=":tada: **New Release:** [$release_name]($release_url)\n\n$release_body\n\n**Docker Images:**\n- Docker Hub: \`booklore/booklore-app:${{ env.new_tag }}\` ([View]($dockerhub_image))\n- GHCR: \`ghcr.io/${{ github.repository }}/booklore-app:${{ env.new_tag }}\` ([View]($ghcr_image))"
payload="{\"content\": \"$content\"}"
curl -H "Content-Type: application/json" -X POST -d "$payload" $DISCORD_WEBHOOK_URL