Files
grimmory/.github/workflows/docker-build-publish.yml
T
2025-03-13 09:34:41 -06:00

83 lines
2.6 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
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: Get Latest Tag
id: get_version
run: |
branch="${GITHUB_REF#refs/heads/}"
if [[ "$branch" == "master" ]]; then
latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1)
else
latest_tag=$(git tag --list "develop-v*" --sort=-v:refname | head -n 1)
fi
latest_tag=${latest_tag:-"v0.0.0"}
echo "latest_tag=$latest_tag" >> $GITHUB_ENV
echo "Latest tag: $latest_tag"
- name: Auto-Increment Version
id: bump_version
run: |
old_version=${{ env.latest_tag }}
IFS='.' read -r -a parts <<< "${old_version//[!0-9.]/}"
new_version="${parts[0]}.${parts[1]}.$((parts[2] + 1))"
branch="${GITHUB_REF#refs/heads/}"
if [[ "$branch" == "master" ]]; then
new_tag="v${new_version}"
else
new_tag="develop-v${new_version}"
fi
echo "new_tag=$new_tag" >> $GITHUB_ENV
echo "New version: $new_tag"
- name: Create Git Tag
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 Docker Image
run: |
docker build -t ghcr.io/${{ github.actor }}/booklore-app:${{ env.new_tag }} .
- name: Push Docker Image to GHCR
run: |
docker push ghcr.io/${{ github.actor }}/booklore-app:${{ env.new_tag }}
- name: Push Latest Tag (only for master)
if: github.ref == 'refs/heads/master'
run: |
docker tag ghcr.io/${{ github.actor }}/booklore-app:${{ env.new_tag }} ghcr.io/${{ github.actor }}/booklore-app:latest
docker push ghcr.io/${{ github.actor }}/booklore-app:latest
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create ${{ env.new_tag }} \
--title "Release ${{ env.new_tag }}" \
--notes "Automated release for ${{ env.new_tag }}"