|
| 1 | +# This GitHub Action workflow builds the Spring Boot application into a Docker image |
| 2 | +# and publishes it to Docker Hub. |
| 3 | +# |
| 4 | +# It triggers on two events: |
| 5 | +# 1. A push to the 'master' branch, which tags the image as 'latest'. |
| 6 | +# 2. Publishing a new release, which tags the image with the release version (e.g., 'v1.0.0'). |
| 7 | + |
| 8 | +name: Publish Docker Image to Docker Hub |
| 9 | + |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: [ "master" ] |
| 13 | + release: |
| 14 | + types: [ published ] |
| 15 | + |
| 16 | +jobs: |
| 17 | + build-and-publish: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + permissions: |
| 20 | + contents: read |
| 21 | + packages: write |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout repository |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Set up JDK 25 |
| 28 | + uses: actions/setup-java@v4 |
| 29 | + with: |
| 30 | + java-version: '25' |
| 31 | + distribution: 'liberica' # Liberica provides builds for JDK 25 |
| 32 | + |
| 33 | + - name: Extract metadata for Docker |
| 34 | + id: meta |
| 35 | + uses: docker/metadata-action@v5 |
| 36 | + with: |
| 37 | + images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }} |
| 38 | + tags: | |
| 39 | + # type=schedule |
| 40 | + type=ref,event=branch |
| 41 | + type=ref,event=pr |
| 42 | + type=semver,pattern={{version}} |
| 43 | + type=semver,pattern={{major}}.{{minor}} |
| 44 | + # Set 'latest' tag when pushing to the master branch |
| 45 | + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }} |
| 46 | +
|
| 47 | + - name: Log in to Docker Hub |
| 48 | + uses: docker/login-action@v3 |
| 49 | + with: |
| 50 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 51 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 52 | + |
| 53 | + - name: Build and push Docker image |
| 54 | + run: | |
| 55 | + # The 'spring-boot:build-image' goal uses Paketo Buildpacks to create a Docker image. |
| 56 | + # The 'pom.xml' is already configured with the image name. |
| 57 | + # We enable publishing and pass the generated tags from the metadata step. |
| 58 | + ./mvnw spring-boot:build-image \ |
| 59 | + -DskipTests \ |
| 60 | + -Dspring-boot.build-image.publish=true \ |
| 61 | + -Dspring-boot.build-image.imageName=${{ steps.meta.outputs.tags }} |
0 commit comments