mirror of
https://github.com/adityachandelgit/BookLore.git
synced 2026-03-16 16:42:08 -05:00
143 lines
4.9 KiB
YAML
143 lines
4.9 KiB
YAML
name: Build, Tag, Push, and Release to GitHub Container Registry
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
- develop
|
|
|
|
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 GitHub Container Registry
|
|
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
|
|
|
|
- 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: |
|
|
labels=$(gh pr view ${{ github.event.pull_request.number }} --json labels --jq '.labels[].name' || echo "")
|
|
if echo "$labels" | grep -q 'breaking-change'; then
|
|
bump="major"
|
|
elif echo "$labels" | grep -q 'feature\|enhancement'; then
|
|
bump="minor"
|
|
else
|
|
bump="patch"
|
|
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
|
|
run: |
|
|
docker buildx create --use
|
|
docker buildx build \
|
|
--platform linux/amd64,linux/arm64 \
|
|
--tag ghcr.io/${{ github.actor }}/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 \
|
|
--tag ghcr.io/${{ github.actor }}/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: Delete Merged Branches (Only for Master and Develop)
|
|
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/develop'
|
|
run: |
|
|
git fetch --prune
|
|
merged_branches=$(git branch -r --merged origin/master origin/develop | sed 's/ *origin\///')
|
|
for branch in $merged_branches; do
|
|
if [[ "$branch" != "master" && "$branch" != "develop" ]]; then
|
|
echo "Deleting merged branch: $branch"
|
|
git push origin --delete "$branch"
|
|
else
|
|
echo "Skipping protected branch: $branch"
|
|
fi
|
|
done |