|
| 1 | +name: "server build, push" |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - dev |
| 9 | + pull_request: |
| 10 | + |
| 11 | +env: |
| 12 | + REGISTRY_IMAGE: ghcr.io/42core-team/game-server |
| 13 | + |
| 14 | +concurrency: |
| 15 | + group: "${{ github.workflow }} @ ${{ github.ref_name }}" |
| 16 | + cancel-in-progress: true |
| 17 | + |
| 18 | +jobs: |
| 19 | + # Job for pull requests - only compile and test, don't push |
| 20 | + test-build: |
| 21 | + if: github.event_name == 'pull_request' |
| 22 | + strategy: |
| 23 | + fail-fast: false |
| 24 | + matrix: |
| 25 | + include: |
| 26 | + - architecture: amd64 |
| 27 | + runner: ubuntu-24.04 |
| 28 | + platform: linux/amd64 |
| 29 | + - architecture: arm64 |
| 30 | + runner: ubuntu-24.04-arm |
| 31 | + platform: linux/arm64 |
| 32 | + |
| 33 | + runs-on: |
| 34 | + - ${{ matrix.runner }} |
| 35 | + steps: |
| 36 | + - name: Checkout repository |
| 37 | + uses: actions/checkout@v4 |
| 38 | + |
| 39 | + - name: Set up Docker Buildx |
| 40 | + uses: docker/setup-buildx-action@v3 |
| 41 | + |
| 42 | + - name: Build (test only) |
| 43 | + uses: docker/build-push-action@v6 |
| 44 | + with: |
| 45 | + context: ${{ github.workspace }} |
| 46 | + file: .github/workflows/server-Dockerfile |
| 47 | + platforms: ${{ matrix.platform }} |
| 48 | + push: false |
| 49 | + cache-from: type=gha |
| 50 | + cache-to: type=gha,mode=max |
| 51 | + |
| 52 | + # Job for pushes to main branches - build and push |
| 53 | + build: |
| 54 | + if: github.event_name != 'pull_request' |
| 55 | + strategy: |
| 56 | + fail-fast: false |
| 57 | + matrix: |
| 58 | + include: |
| 59 | + - architecture: amd64 |
| 60 | + runner: ubuntu-24.04 |
| 61 | + platform: linux/amd64 |
| 62 | + - architecture: arm64 |
| 63 | + runner: ubuntu-24.04-arm |
| 64 | + platform: linux/arm64 |
| 65 | + runs-on: |
| 66 | + - ${{ matrix.runner }} |
| 67 | + steps: |
| 68 | + - name: Checkout repository |
| 69 | + uses: actions/checkout@v4 |
| 70 | + |
| 71 | + - name: Prepare |
| 72 | + run: | |
| 73 | + platform=${{ matrix.platform }} |
| 74 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 75 | +
|
| 76 | + # - name: Set up QEMU |
| 77 | + # uses: docker/setup-qemu-action@v3 |
| 78 | + |
| 79 | + - name: Set up Docker Buildx |
| 80 | + uses: docker/setup-buildx-action@v3 |
| 81 | + |
| 82 | + - name: Docker meta |
| 83 | + id: meta |
| 84 | + uses: docker/metadata-action@v5 |
| 85 | + with: |
| 86 | + images: ${{ env.REGISTRY_IMAGE }} |
| 87 | + |
| 88 | + - name: Login to GitHub Container Registry |
| 89 | + uses: docker/login-action@v3 |
| 90 | + with: |
| 91 | + registry: ghcr.io |
| 92 | + username: ${{ github.actor }} |
| 93 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 94 | + |
| 95 | + - name: Build and push by digest |
| 96 | + id: build |
| 97 | + uses: docker/build-push-action@v6 |
| 98 | + with: |
| 99 | + context: ${{ github.workspace }} |
| 100 | + file: .github/workflows/server-Dockerfile |
| 101 | + platforms: ${{ matrix.platform }} |
| 102 | + labels: ${{ steps.meta.outputs.labels }} |
| 103 | + tags: ${{ env.REGISTRY_IMAGE }} |
| 104 | + outputs: type=image,push-by-digest=true,name-canonical=true,push=true |
| 105 | + cache-from: type=gha |
| 106 | + cache-to: type=gha,mode=max |
| 107 | + |
| 108 | + - name: Export digest |
| 109 | + run: | |
| 110 | + mkdir -p ${{ runner.temp }}/digests |
| 111 | + digest="${{ steps.build.outputs.digest }}" |
| 112 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 113 | +
|
| 114 | + - name: Upload digest |
| 115 | + uses: actions/upload-artifact@v4 |
| 116 | + with: |
| 117 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 118 | + path: ${{ runner.temp }}/digests/* |
| 119 | + if-no-files-found: error |
| 120 | + retention-days: 1 |
| 121 | + |
| 122 | + merge: |
| 123 | + if: github.event_name != 'pull_request' |
| 124 | + runs-on: ubuntu-24.04 |
| 125 | + needs: build |
| 126 | + steps: |
| 127 | + - name: Download digests |
| 128 | + uses: actions/download-artifact@v4 |
| 129 | + with: |
| 130 | + path: ${{ runner.temp }}/digests |
| 131 | + pattern: digests-* |
| 132 | + merge-multiple: true |
| 133 | + |
| 134 | + - name: Login to GitHub Container Registry |
| 135 | + uses: docker/login-action@v3 |
| 136 | + with: |
| 137 | + registry: ghcr.io |
| 138 | + username: ${{ github.actor }} |
| 139 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 140 | + |
| 141 | + - name: Set up Docker Buildx |
| 142 | + uses: docker/setup-buildx-action@v3 |
| 143 | + |
| 144 | + - name: Docker meta |
| 145 | + id: meta |
| 146 | + uses: docker/metadata-action@v5 |
| 147 | + with: |
| 148 | + images: ${{ env.REGISTRY_IMAGE }} |
| 149 | + tags: | |
| 150 | + type=raw,value=${{ github.ref_name }} |
| 151 | + type=raw,value=${{ github.ref_name }}-${{ github.sha }} |
| 152 | +
|
| 153 | + - name: Create manifest list and push |
| 154 | + working-directory: ${{ runner.temp }}/digests |
| 155 | + run: | |
| 156 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 157 | + $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) |
| 158 | +
|
| 159 | + - name: Inspect image |
| 160 | + run: | |
| 161 | + docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }} |
0 commit comments