From 9de52ac2434f856360c37666cef268264fecd400 Mon Sep 17 00:00:00 2001 From: Ricardo Campos Date: Sun, 18 Jan 2026 17:15:35 -0300 Subject: [PATCH] feat: improve CI to use GHCR and single versions in the backend --- .github/workflows/main-client.yml | 12 +++-- .github/workflows/main-server.yml | 79 ++++++++++++++++++------------- Taskfile.yml | 5 +- server/pom.xml | 2 +- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/.github/workflows/main-client.yml b/.github/workflows/main-client.yml index 7c0061d..a14ae92 100644 --- a/.github/workflows/main-client.yml +++ b/.github/workflows/main-client.yml @@ -21,6 +21,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: write + packages: write steps: - name: Checkout code @@ -39,20 +40,21 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Log in to Docker Hub + - name: Log in to GitHub Container Registry uses: docker/login-action@v3 with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata for Docker id: meta uses: docker/metadata-action@v5 with: - images: ${{ secrets.DOCKER_USERNAME }}/tasknote + images: ghcr.io/${{ github.repository }}/app tags: | type=raw,value=${{ steps.version.outputs.tag }} - type=raw,value=app-latest,enable={{is_default_branch}} + type=raw,value=latest,enable={{is_default_branch}} - name: Build and push Docker image uses: docker/build-push-action@v5 diff --git a/.github/workflows/main-server.yml b/.github/workflows/main-server.yml index 95f7fc0..ab7eb26 100644 --- a/.github/workflows/main-server.yml +++ b/.github/workflows/main-server.yml @@ -8,13 +8,14 @@ on: paths: - 'server/**/*.java' - 'server/**/*.xml' - - 'server/Dockerfile' + - 'server/pom.xml' jobs: build-and-push: runs-on: ubuntu-latest permissions: contents: write + packages: write steps: - name: Checkout code @@ -22,47 +23,61 @@ jobs: with: fetch-depth: 0 - - name: Generate version tag + - name: Set up Java + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: '21' + cache: 'maven' + + - name: Increment version in pom.xml id: version + working-directory: ./server run: | - DATE=$(date +'%Y.%m.%d') - TAG="api-v${DATE}.${{ github.run_number }}" - echo "tag=${TAG}" >> $GITHUB_OUTPUT - echo "Generated tag: ${TAG}" + # Extract current version from pom.xml + CURRENT_VERSION=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout) + echo "Current version: ${CURRENT_VERSION}" - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 + # Increment version + NEW_VERSION=$((CURRENT_VERSION + 1)) + echo "New version: ${NEW_VERSION}" - - name: Log in to Docker Hub + # Update pom.xml with new version + ./mvnw versions:set -DnewVersion=${NEW_VERSION} -DgenerateBackupFiles=false -q + + # Output for later steps + echo "version=${NEW_VERSION}" >> $GITHUB_OUTPUT + + - name: Commit version bump + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add server/pom.xml + git commit -m "chore: bump api version to ${{ steps.version.outputs.version }} [skip ci]" + git push + + - name: Log in to GitHub Container Registry uses: docker/login-action@v3 with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata for Docker - id: meta - uses: docker/metadata-action@v5 - with: - images: ${{ secrets.DOCKER_USERNAME }}/tasknote - tags: | - type=raw,value=${{ steps.version.outputs.tag }} - type=raw,value=api-latest,enable={{is_default_branch}} + - name: Build Docker image with Spring Boot + working-directory: ./server + run: | + ./mvnw -Pnative -DskipTests spring-boot:build-image \ + -Dspring-boot.build-image.imageName=ghcr.io/${{ github.repository }}/api:latest - - name: Build and push Docker image - uses: docker/build-push-action@v5 - with: - context: ./server - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max - build-args: | - BUILD=v${{ steps.version.outputs.tag }} + - name: Tag and push Docker image + run: | + docker tag ghcr.io/${{ github.repository }}/api:latest ghcr.io/${{ github.repository }}/api:${{ steps.version.outputs.version }} + docker push ghcr.io/${{ github.repository }}/api:latest + docker push ghcr.io/${{ github.repository }}/api:${{ steps.version.outputs.version }} - name: Create and push Git tag run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - git tag -a ${{ steps.version.outputs.tag }} -m "Release ${{ steps.version.outputs.tag }}" - git push origin ${{ steps.version.outputs.tag }} + git tag -a api-v${{ steps.version.outputs.version }} -m "Release API v${{ steps.version.outputs.version }}" + git push origin api-v${{ steps.version.outputs.version }} diff --git a/Taskfile.yml b/Taskfile.yml index 1df5c3c..f1f7530 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -8,11 +8,12 @@ vars: tasks: docker-build-web: desc: Build the tasknote-web prod-ready docker image, tagging it as candidate - cmd: docker build --no-cache --build-arg VITE_BUILD="v999-$(date '+%Y-%m-%d-%H%M%S')" --build-arg SOURCE_PR="v999-123456789-$(date '+%Y-%m-%d-%H%M%S')" -t docker.io/rmcampos/tasknote:app-latest ./client + cmd: docker build --no-cache --build-arg VITE_BUILD="v999-$(date '+%Y-%m-%d-%H%M%S')" --build-arg SOURCE_PR="v999-123456789-$(date '+%Y-%m-%d-%H%M%S')" -t ghcr.io/rmcampos/tasknote/app:latest ./client docker-build-api: desc: Build the tasknote-api prod-ready docker image, tagging it as candidate - cmd: docker build --no-cache --build-arg BUILD="v999-$(date '+%Y-%m-%d-%H%M%S')" --build-arg SOURCE_PR="v999-123456789-$(date '+%Y-%m-%d-%H%M%S')" -t docker.io/rmcampos/tasknote:api-latest ./server + cmd: cd server && mvn -Pnative -DskipTests spring-boot:build-image -Dspring-boot.build-image.imageName=ghcr.io/rmcampos/tasknote/api:latest + prod-up-web: desc: Speed up the tasknote-web prod-like image, building it if required cmd: docker compose -f docker-compose.prod.yml up -d tasknote-web diff --git a/server/pom.xml b/server/pom.xml index 96e801d..b4e3d4e 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -11,7 +11,7 @@ br.com.tasknoteapp server - 2.0.0 + 1 tasknote-api Java backend REST API to serve TaskNote frontend client