From bbd3beb22a2e73a5032b2e9a2d166098f8a362f8 Mon Sep 17 00:00:00 2001 From: almazlar Date: Sat, 21 Feb 2026 10:28:10 +0300 Subject: [PATCH] feat: Automate versioning, changelog updates, and Git tagging, using the generated version for Docker image tags. --- .gitea/workflows/docker-build.yml | 89 +++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/docker-build.yml b/.gitea/workflows/docker-build.yml index 2fabd4c..c453f43 100644 --- a/.gitea/workflows/docker-build.yml +++ b/.gitea/workflows/docker-build.yml @@ -1,4 +1,4 @@ -name: Build and Push Docker Images +name: Release and Build Docker Images on: push: @@ -12,11 +12,92 @@ env: OWNER: ${{ gitea.repository_owner }} jobs: - build-and-push: + release-and-build: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.ACCESS_TOKEN }} + + - name: Git Configuration + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + - name: Generate Version and Changelog + id: generate + run: | + # 1. Fetch the latest tag + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") + echo "Latest tag: $LATEST_TAG" + + # 2. Calculate the next patch version + if [[ $LATEST_TAG == v* ]]; then + VERSION_NUM=${LATEST_TAG:1} + IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION_NUM" + PATCH=$((PATCH + 1)) + NEW_TAG="v$MAJOR.$MINOR.$PATCH" + else + # Fallback if the tag format is unrecognized + NEW_TAG="v0.0.1" + fi + + echo "New tag: $NEW_TAG" + echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT + + # 3. Create a temporary file for the new changelog entry + TEMP_CHANGELOG=$(mktemp) + DATE=$(date +'%Y-%m-%d') + + echo "## [$NEW_TAG] - $DATE" > "$TEMP_CHANGELOG" + echo "" >> "$TEMP_CHANGELOG" + + # Fetch all commit messages since the latest tag to HEAD + if [ "$LATEST_TAG" = "v0.0.0" ]; then + git log --pretty=format:"* %s (%h)" >> "$TEMP_CHANGELOG" + else + git log ${LATEST_TAG}..HEAD --pretty=format:"* %s (%h)" >> "$TEMP_CHANGELOG" + fi + + echo "" >> "$TEMP_CHANGELOG" + echo "" >> "$TEMP_CHANGELOG" + + # 4. Prepend the new entry to the existing CHANGELOG.md + if [ -f CHANGELOG.md ]; then + cat CHANGELOG.md >> "$TEMP_CHANGELOG" + fi + + mv "$TEMP_CHANGELOG" CHANGELOG.md + + echo "Changelog generated successfully." + + - name: Commit and Push Changes + run: | + NEW_TAG=${{ steps.generate.outputs.new_tag }} + + # Add the updated changelog + git add CHANGELOG.md + + # Only commit if there are changes + if ! git diff-index --quiet HEAD; then + git commit -m "chore(release): bump version to $NEW_TAG and update changelog [skip ci]" + + # Create the new tag + git tag -a "$NEW_TAG" -m "Release $NEW_TAG" + + # Push the commit and the new tag + git push origin main + git push origin "$NEW_TAG" + else + echo "No changes to commit." + # If we haven't already tagged this commit, do it now + if ! git ls-remote --tags origin | grep -q "refs/tags/$NEW_TAG"; then + git tag -a "$NEW_TAG" -m "Release $NEW_TAG" + git push origin "$NEW_TAG" + fi + fi - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -35,7 +116,7 @@ jobs: images: ${{ env.REGISTRY }}/${{ env.OWNER }}/todo-backend tags: | type=raw,value=latest - type=sha,prefix={{branch}}- + type=raw,value=${{ steps.generate.outputs.new_tag }} - name: Build and push Backend image uses: docker/build-push-action@v5 @@ -52,7 +133,7 @@ jobs: images: ${{ env.REGISTRY }}/${{ env.OWNER }}/todo-frontend tags: | type=raw,value=latest - type=sha,prefix={{branch}}- + type=raw,value=${{ steps.generate.outputs.new_tag }} - name: Build and push Frontend image uses: docker/build-push-action@v5