|
| 1 | +name: Build, Test and Publish Container |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + packages: write |
| 15 | + |
| 16 | +env: |
| 17 | + REGISTRY: ghcr.io |
| 18 | + IMAGE_NAME: erhardtconsulting/database-development-app |
| 19 | + |
| 20 | +jobs: |
| 21 | + maven: |
| 22 | + name: Maven build and tests |
| 23 | + runs-on: ubuntu-24.04 |
| 24 | + outputs: |
| 25 | + app-version: ${{ steps.project-version.outputs.version }} |
| 26 | + steps: |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@v4 |
| 29 | + |
| 30 | + - name: Set up Java 24 |
| 31 | + uses: actions/setup-java@v4 |
| 32 | + with: |
| 33 | + distribution: temurin |
| 34 | + java-version: "24" |
| 35 | + cache: maven |
| 36 | + |
| 37 | + - name: Read project version |
| 38 | + id: project-version |
| 39 | + run: echo "version=$(./mvnw -q -DforceStdout help:evaluate -Dexpression=project.version)" >> "$GITHUB_OUTPUT" |
| 40 | + |
| 41 | + - name: Run fast tests |
| 42 | + run: ./mvnw -B test |
| 43 | + |
| 44 | + - name: Run Testcontainers integration tests |
| 45 | + run: ./mvnw -B -Ptestcontainers test |
| 46 | + |
| 47 | + - name: Build application package |
| 48 | + run: ./mvnw -B package -DskipTests |
| 49 | + |
| 50 | + container: |
| 51 | + name: Container build and publish |
| 52 | + runs-on: ubuntu-24.04 |
| 53 | + needs: maven |
| 54 | + steps: |
| 55 | + - name: Checkout repository |
| 56 | + uses: actions/checkout@v4 |
| 57 | + |
| 58 | + - name: Set up Docker Buildx |
| 59 | + uses: docker/setup-buildx-action@v3 |
| 60 | + |
| 61 | + - name: Log in to GitHub Container Registry |
| 62 | + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 63 | + uses: docker/login-action@v3 |
| 64 | + with: |
| 65 | + registry: ${{ env.REGISTRY }} |
| 66 | + username: ${{ github.actor }} |
| 67 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + |
| 69 | + - name: Prepare image tags |
| 70 | + id: image-tags |
| 71 | + run: | |
| 72 | + SHORT_SHA="${GITHUB_SHA::7}" |
| 73 | + VERSION="${{ needs.maven.outputs.app-version }}" |
| 74 | + IMAGE="${REGISTRY}/${IMAGE_NAME}" |
| 75 | + { |
| 76 | + echo "tags<<EOF" |
| 77 | + echo "${IMAGE}:${VERSION}-${SHORT_SHA}" |
| 78 | + if [ "${GITHUB_EVENT_NAME}" = "push" ] && [ "${GITHUB_REF}" = "refs/heads/main" ]; then |
| 79 | + echo "${IMAGE}:latest" |
| 80 | + fi |
| 81 | + echo "EOF" |
| 82 | + } >> "$GITHUB_OUTPUT" |
| 83 | +
|
| 84 | + - name: Build container image |
| 85 | + uses: docker/build-push-action@v6 |
| 86 | + with: |
| 87 | + context: . |
| 88 | + file: Dockerfile |
| 89 | + platforms: linux/amd64 |
| 90 | + push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 91 | + tags: ${{ steps.image-tags.outputs.tags }} |
0 commit comments