|
| 1 | +name: Build a new production release |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [released] # This workflow only runs when a new GitHub release is *actually* released publicly |
| 6 | + |
| 7 | +env: |
| 8 | + REGISTRY: ghcr.io |
| 9 | + IMAGE_NAME: ${{ github.repository }} |
| 10 | + |
| 11 | +jobs: |
| 12 | + build_test_run: |
| 13 | + runs-on: ubuntu-24.04 |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 # Checkout the repository |
| 16 | + |
| 17 | + - name: Set up Rust + QEMU |
| 18 | + uses: docker/setup-qemu-action@v2 |
| 19 | + with: |
| 20 | + platforms: linux/amd64,linux/arm64 # Enable multi-arch builds |
| 21 | + |
| 22 | + - name: Set up Rust |
| 23 | + uses: dtolnay/rust-toolchain@stable |
| 24 | + with: |
| 25 | + toolchain: stable # Install stable Rust toolchain |
| 26 | + |
| 27 | + - uses: Swatinem/rust-cache@v2 |
| 28 | + with: |
| 29 | + shared-key: ${{ runner.os }}-cargo-${{ github.sha }} # Caches Rust build artifacts |
| 30 | + |
| 31 | + - name: Install sea-orm-cli |
| 32 | + run: cargo install sea-orm-cli # If needed for migrations / seeds |
| 33 | + |
| 34 | + - name: Run tests |
| 35 | + run: cargo test --release # Basic test step |
| 36 | + |
| 37 | + build_and_push_docker: |
| 38 | + runs-on: ubuntu-24.04 |
| 39 | + needs: build_test_run |
| 40 | + permissions: |
| 41 | + contents: read |
| 42 | + packages: write |
| 43 | + attestations: write |
| 44 | + id-token: write |
| 45 | + |
| 46 | + steps: |
| 47 | + - uses: actions/checkout@v4 # Checkout code again for the Docker build |
| 48 | + |
| 49 | + - name: Docker login |
| 50 | + uses: docker/login-action@v2 |
| 51 | + with: |
| 52 | + registry: ${{ env.REGISTRY }} |
| 53 | + username: ${{ github.actor }} |
| 54 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + |
| 56 | + - uses: docker/setup-buildx-action@v3 |
| 57 | + with: |
| 58 | + install: true # Set up Docker Buildx for multi-arch |
| 59 | + |
| 60 | + - name: Show Docker Build Cache (Before) |
| 61 | + run: | |
| 62 | + echo "🔍 Checking buildx cache BEFORE build..." |
| 63 | + docker buildx du || echo "No cache found yet." |
| 64 | +
|
| 65 | + # Compute stable tag instead of using branch name |
| 66 | + - name: Determine Image Tags |
| 67 | + id: tags |
| 68 | + run: | |
| 69 | + IMAGE_NAME="${{ env.REGISTRY }}/${{ github.repository }}" |
| 70 | + echo "backend_tags=$IMAGE_NAME:stable" >> $GITHUB_OUTPUT # Tag image as "stable" |
| 71 | + echo "backend_image_name=$IMAGE_NAME" >> $GITHUB_OUTPUT |
| 72 | +
|
| 73 | + - name: Build + Push Backend |
| 74 | + id: push_backend |
| 75 | + uses: docker/build-push-action@v5 |
| 76 | + with: |
| 77 | + context: . |
| 78 | + file: ./Dockerfile |
| 79 | + platforms: linux/amd64,linux/arm64 # Multi-arch build |
| 80 | + push: true |
| 81 | + provenance: true |
| 82 | + tags: ${{ steps.tags.outputs.backend_tags }} # Use "stable" tag |
| 83 | + cache-from: type=gha |
| 84 | + cache-to: type=gha,mode=max |
| 85 | + |
| 86 | + - name: Show Docker Build Cache (After) |
| 87 | + run: | |
| 88 | + echo "📦 Checking buildx cache AFTER build..." |
| 89 | + docker buildx du || echo "Failed to get updated cache info." |
| 90 | +
|
| 91 | + # Optionally attest build provenance (can be kept if desired) |
| 92 | + - name: Attest Backend |
| 93 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 94 | + uses: actions/attest-build-provenance@v2 |
| 95 | + with: |
| 96 | + subject-name: ${{ steps.tags.outputs.backend_image_name }} |
| 97 | + subject-digest: ${{ steps.push_backend.outputs.digest }} |
| 98 | + push-to-registry: true |
| 99 | + |
| 100 | + - name: Print Usage Instructions |
| 101 | + run: | |
| 102 | + echo "Backend Image Pushed to ghcr.io as STABLE:" |
| 103 | + echo " docker pull ${{ steps.tags.outputs.backend_image_name }}:stable" |
| 104 | + echo "Run it locally:" |
| 105 | + echo " docker run --rm --env-file .env -p 8000:8000 ${{ steps.tags.outputs.backend_image_name }}:stable" |
0 commit comments