|
| 1 | +name: Docker Build and Publish |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ main ] |
| 6 | + paths: |
| 7 | + - 'src/**' |
| 8 | + - 'Dockerfile.*' |
| 9 | + - 'docker/**' |
| 10 | + - '.github/workflows/docker-publish.yml' |
| 11 | + release: |
| 12 | + types: [published] |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + tag: |
| 16 | + description: 'Tag to build and push (e.g., v1.0.0 or latest)' |
| 17 | + required: true |
| 18 | + default: 'latest' |
| 19 | + |
| 20 | +env: |
| 21 | + REGISTRY: ghcr.io |
| 22 | + IMAGE_NAME_API: ${{ github.repository }}/api |
| 23 | + IMAGE_NAME_CATALOGSYNC: ${{ github.repository }}/catalogsync |
| 24 | + |
| 25 | +jobs: |
| 26 | + # Verify builds on PRs (no push) |
| 27 | + verify-build: |
| 28 | + name: Verify Docker Builds |
| 29 | + runs-on: ubuntu-latest |
| 30 | + if: github.event_name == 'pull_request' |
| 31 | + |
| 32 | + strategy: |
| 33 | + matrix: |
| 34 | + service: [api, catalogsync] |
| 35 | + |
| 36 | + steps: |
| 37 | + - name: Checkout code |
| 38 | + uses: actions/checkout@v4 |
| 39 | + |
| 40 | + - name: Set up Docker Buildx |
| 41 | + uses: docker/setup-buildx-action@v3 |
| 42 | + |
| 43 | + - name: Build ${{ matrix.service }} (verification only) |
| 44 | + uses: docker/build-push-action@v5 |
| 45 | + with: |
| 46 | + context: . |
| 47 | + file: ./Dockerfile.${{ matrix.service }} |
| 48 | + push: false |
| 49 | + tags: purdueio-${{ matrix.service }}:test |
| 50 | + cache-from: type=gha |
| 51 | + cache-to: type=gha,mode=max |
| 52 | + |
| 53 | + # Build and push on releases |
| 54 | + build-and-push: |
| 55 | + name: Build and Push Docker Images |
| 56 | + runs-on: ubuntu-latest |
| 57 | + if: github.event_name == 'release' || github.event_name == 'workflow_dispatch' |
| 58 | + permissions: |
| 59 | + contents: read |
| 60 | + packages: write |
| 61 | + |
| 62 | + strategy: |
| 63 | + matrix: |
| 64 | + include: |
| 65 | + - service: api |
| 66 | + image_name_var: IMAGE_NAME_API |
| 67 | + - service: catalogsync |
| 68 | + image_name_var: IMAGE_NAME_CATALOGSYNC |
| 69 | + |
| 70 | + steps: |
| 71 | + - name: Checkout code |
| 72 | + uses: actions/checkout@v4 |
| 73 | + |
| 74 | + - name: Set up Docker Buildx |
| 75 | + uses: docker/setup-buildx-action@v3 |
| 76 | + |
| 77 | + - name: Log in to Container Registry |
| 78 | + uses: docker/login-action@v3 |
| 79 | + with: |
| 80 | + registry: ${{ env.REGISTRY }} |
| 81 | + username: ${{ github.actor }} |
| 82 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 83 | + |
| 84 | + - name: Extract metadata (tags, labels) for Docker |
| 85 | + id: meta |
| 86 | + uses: docker/metadata-action@v5 |
| 87 | + with: |
| 88 | + images: ${{ env.REGISTRY }}/${{ matrix.service == 'api' && env.IMAGE_NAME_API || env.IMAGE_NAME_CATALOGSYNC }} |
| 89 | + tags: | |
| 90 | + type=semver,pattern={{version}} |
| 91 | + type=semver,pattern={{major}}.{{minor}} |
| 92 | + type=semver,pattern={{major}} |
| 93 | + type=raw,value=latest,enable={{is_default_branch}} |
| 94 | + type=raw,value=${{ github.event.inputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }} |
| 95 | +
|
| 96 | + - name: Build and push ${{ matrix.service }} |
| 97 | + uses: docker/build-push-action@v5 |
| 98 | + with: |
| 99 | + context: . |
| 100 | + file: ./Dockerfile.${{ matrix.service }} |
| 101 | + push: true |
| 102 | + tags: ${{ steps.meta.outputs.tags }} |
| 103 | + labels: ${{ steps.meta.outputs.labels }} |
| 104 | + cache-from: type=gha |
| 105 | + cache-to: type=gha,mode=max |
| 106 | + |
| 107 | + - name: Output image tags |
| 108 | + run: | |
| 109 | + echo "### Published ${{ matrix.service }} image :rocket:" >> $GITHUB_STEP_SUMMARY |
| 110 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 111 | + echo "**Tags:**" >> $GITHUB_STEP_SUMMARY |
| 112 | + echo '```' >> $GITHUB_STEP_SUMMARY |
| 113 | + echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY |
| 114 | + echo '```' >> $GITHUB_STEP_SUMMARY |
0 commit comments