This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Add GitHub Action to publish Docker image #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This GitHub Action workflow builds the Spring Boot application into a Docker image | |
| # and publishes it to Docker Hub. | |
| # | |
| # It triggers on two events: | |
| # 1. A push to the 'master' branch, which tags the image as 'latest'. | |
| # 2. Publishing a new release, which tags the image with the release version (e.g., 'v1.0.0'). | |
| name: Publish Docker Image to Docker Hub | |
| on: | |
| push: | |
| branches: [ "master" ] | |
| release: | |
| types: [ published ] | |
| jobs: | |
| build-and-publish: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 25 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '25' | |
| distribution: 'liberica' # Liberica provides builds for JDK 25 | |
| - name: Extract metadata for Docker | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }} | |
| tags: | | |
| # type=schedule | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| # Set 'latest' tag when pushing to the master branch | |
| type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }} | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| run: | | |
| # The 'spring-boot:build-image' goal uses Paketo Buildpacks to create a Docker image. | |
| # The 'pom.xml' is already configured with the image name. | |
| # We enable publishing and pass the generated tags from the metadata step. | |
| ./mvnw spring-boot:build-image \ | |
| -DskipTests \ | |
| -Dspring-boot.build-image.publish=true \ | |
| -Dspring-boot.build-image.imageName=${{ steps.meta.outputs.tags }} |