diff --git a/.github/workflows/docker-build-publish.yml b/.github/workflows/docker-build-publish.yml index c5a4356a9..c8949128d 100644 --- a/.github/workflows/docker-build-publish.yml +++ b/.github/workflows/docker-build-publish.yml @@ -105,13 +105,14 @@ jobs: with: java-version: '21' distribution: 'temurin' + cache: 'gradle' - name: Run Backend Tests id: backend_tests working-directory: ./booklore-api run: | echo "Running backend tests with testcontainers..." - ./gradlew test + ./gradlew test --no-daemon --parallel --build-cache continue-on-error: true - name: Publish Test Results @@ -204,42 +205,36 @@ jobs: echo "bump=$bump" >> $GITHUB_ENV echo "new_tag=$next_version" >> $GITHUB_ENV - - 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: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + booklore/booklore + ghcr.io/booklore-app/booklore + tags: | + type=sha,enable=${{ github.ref != github.event.repository.default_branch }} + type=raw,value={{branch}}-{{sha_short}},enable=${{ github.ref_name == 'develop' }} + type=raw,value=${{ env.new_tag }},enable=${{ github.ref_name == 'master' }} + type=raw,value=latest,enable=${{ github.ref_name == 'master' }} - - name: Build and Push Docker Image - run: | - docker buildx create --use - docker buildx build \ - --platform linux/amd64,linux/arm64 \ - --build-arg APP_VERSION=${{ env.image_tag }} \ - --build-arg APP_REVISION=${{ github.sha }} \ - --tag booklore/booklore:${{ env.image_tag }} \ - --tag ghcr.io/booklore-app/booklore:${{ 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:latest \ - --tag ghcr.io/booklore-app/booklore:latest \ - --push . + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + APP_VERSION=${{ steps.meta.outputs.version }} + APP_REVISION=${{ github.sha }} + cache-from: | + type=gha + type=registry,ref=ghcr.io/booklore-app/booklore:buildcache + cache-to: | + type=gha,mode=max + type=registry,ref=ghcr.io/booklore-app/booklore:buildcache,mode=max - name: Update Release Draft (Only for Master) if: github.ref == 'refs/heads/master' @@ -255,4 +250,4 @@ jobs: env: GITHUB_TOKEN: ${{ github.token }} run: | - gh release edit ${{ env.new_tag }} --draft=true \ No newline at end of file + gh release edit ${{ env.new_tag }} --draft=true diff --git a/Dockerfile b/Dockerfile index dfa920da2..79c8c7bd6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,10 @@ FROM node:22-alpine AS angular-build WORKDIR /angular-app COPY ./booklore-ui/package.json ./booklore-ui/package-lock.json ./ -RUN npm config set registry http://registry.npmjs.org/ \ - && npm config set fetch-retries 5 \ - && npm config set fetch-retry-mintimeout 20000 \ - && npm config set fetch-retry-maxtimeout 120000 \ - && npm install --force +RUN --mount=type=cache,target=/root/.npm \ + npm config set registry http://registry.npmjs.org/ \ + && npm ci --force + COPY ./booklore-ui /angular-app/ RUN npm run build --configuration=production @@ -18,7 +17,13 @@ FROM gradle:8.14.3-jdk21-alpine AS springboot-build WORKDIR /springboot-app +# Copy only build files first to cache dependencies COPY ./booklore-api/build.gradle ./booklore-api/settings.gradle /springboot-app/ + +# Download dependencies (cached layer) +RUN --mount=type=cache,target=/home/gradle/.gradle \ + gradle dependencies --no-daemon + COPY ./booklore-api/src /springboot-app/src # Inject version into application.yaml using yq @@ -26,7 +31,8 @@ ARG APP_VERSION RUN apk add --no-cache yq && \ yq eval '.app.version = strenv(APP_VERSION)' -i /springboot-app/src/main/resources/application.yaml -RUN gradle clean build -x test +RUN --mount=type=cache,target=/home/gradle/.gradle \ + gradle clean build -x test --no-daemon --parallel # Stage 3: Final image FROM eclipse-temurin:21.0.9_10-jre-alpine