|
| 1 | +name: Build Container Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - release/v0.19 |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: ${{ github.repository }} |
| 13 | + |
| 14 | +jobs: |
| 15 | + meta: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + timeout-minutes: 5 |
| 18 | + |
| 19 | + permissions: |
| 20 | + contents: read |
| 21 | + |
| 22 | + outputs: |
| 23 | + tags: ${{ steps.meta.outputs.tags }} |
| 24 | + labels: ${{ steps.meta.outputs.labels }} |
| 25 | + annotations: ${{ steps.meta.outputs.annotations }} |
| 26 | + json: ${{ steps.meta.outputs.json }} |
| 27 | + image-name: ${{ steps.custom-meta.outputs.image-name }} |
| 28 | + tag-name: ${{ steps.custom-meta.outputs.tag-name }} |
| 29 | + extra-commits: ${{ steps.check-extra-commits.outputs.commit-count }} |
| 30 | + |
| 31 | + steps: |
| 32 | + - name: Checkout current fork HEAD |
| 33 | + uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + fetch-depth: 0 |
| 36 | + show-progress: false |
| 37 | + |
| 38 | + - name: Set up upstream git remote |
| 39 | + run: | |
| 40 | + upstream="$(gh repo view "${{ github.repository }}" --json parent --jq '.parent.owner.login + "/" + .parent.name')" |
| 41 | + echo "upstream=$upstream" |
| 42 | + git remote add upstream "https://github.com/$upstream.git" |
| 43 | + git fetch upstream "${{ github.event.repository.default_branch }}" |
| 44 | + env: |
| 45 | + GH_TOKEN: ${{ github.token }} |
| 46 | + |
| 47 | + - name: Determine number of commits on top of upstream |
| 48 | + id: check-extra-commits |
| 49 | + run: | |
| 50 | + commit_count="$(git rev-list --count "upstream/${{ github.event.repository.default_branch }}..$GITHUB_REF_NAME")" |
| 51 | + echo "commit-count=$commit_count" | tee -a "$GITHUB_OUTPUT" |
| 52 | +
|
| 53 | + - name: Discard our commits |
| 54 | + run: | |
| 55 | + git reset --hard "HEAD~${{ steps.check-extra-commits.outputs.commit-count }}" |
| 56 | +
|
| 57 | + - name: Docker meta |
| 58 | + id: meta |
| 59 | + uses: docker/metadata-action@v5 |
| 60 | + with: |
| 61 | + context: git |
| 62 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 63 | + tags: | |
| 64 | + type=ref,event=branch |
| 65 | + type=sha,format=long |
| 66 | + type=sha,format=short |
| 67 | +
|
| 68 | + - name: Extract image name |
| 69 | + id: custom-meta |
| 70 | + run: | |
| 71 | + echo "image-name=${DOCKER_TAG%%:*}" | tee -a "$GITHUB_OUTPUT" |
| 72 | + echo "tag-name=${DOCKER_TAG##*:}" | tee -a "$GITHUB_OUTPUT" |
| 73 | + env: |
| 74 | + DOCKER_TAG: ${{ fromJSON(steps.meta.outputs.json).tags[0] }} |
| 75 | + |
| 76 | + build: |
| 77 | + runs-on: ubuntu-24.04${{ matrix.platform == 'linux/arm64' && '-arm' || '' }} |
| 78 | + timeout-minutes: 20 |
| 79 | + |
| 80 | + permissions: |
| 81 | + contents: read |
| 82 | + packages: write |
| 83 | + |
| 84 | + needs: |
| 85 | + - meta |
| 86 | + |
| 87 | + strategy: |
| 88 | + fail-fast: false |
| 89 | + matrix: |
| 90 | + platform: |
| 91 | + - linux/amd64 |
| 92 | + - linux/arm64 |
| 93 | + |
| 94 | + env: |
| 95 | + REGISTRY_IMAGE: ${{ needs.meta.outputs.image-name }} |
| 96 | + |
| 97 | + steps: |
| 98 | + - name: Prepare |
| 99 | + id: meta |
| 100 | + run: | |
| 101 | + platform=${{ matrix.platform }} |
| 102 | + echo "platform-pair=${platform//\//-}" | tee -a "$GITHUB_OUTPUT" |
| 103 | +
|
| 104 | + - name: Set up Docker Buildx |
| 105 | + uses: docker/setup-buildx-action@v3 |
| 106 | + |
| 107 | + - name: Log in to GHCR |
| 108 | + uses: docker/login-action@v3 |
| 109 | + with: |
| 110 | + registry: ${{ env.REGISTRY }} |
| 111 | + username: ${{ github.repository_owner }} |
| 112 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + |
| 114 | + # We can't build directly from git context, as that will prevent .git from being available during the build process. |
| 115 | + # lemmy-ui's Dockerfile requires the .git folder to set the version. |
| 116 | + - name: Checkout git repository |
| 117 | + uses: actions/checkout@v4 |
| 118 | + with: |
| 119 | + fetch-depth: 0 |
| 120 | + show-progress: false |
| 121 | + submodules: recursive |
| 122 | + |
| 123 | + - name: Discard our commits |
| 124 | + run: | |
| 125 | + git reset --hard "HEAD~${{ needs.meta.outputs.extra-commits }}" |
| 126 | +
|
| 127 | + - name: Build Docker image |
| 128 | + id: build |
| 129 | + uses: docker/build-push-action@v5 |
| 130 | + with: |
| 131 | + context: . |
| 132 | + platforms: ${{ matrix.platform }} |
| 133 | + labels: ${{ needs.meta.outputs.labels }} |
| 134 | + annotations: ${{ needs.meta.outputs.annotations }} |
| 135 | + outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true |
| 136 | + cache-from: type=gha |
| 137 | + cache-to: type=gha,mode=max |
| 138 | + env: |
| 139 | + SOURCE_DATE_EPOCH: 0 |
| 140 | + |
| 141 | + - name: Export image digest |
| 142 | + run: | |
| 143 | + mkdir -p /tmp/digests |
| 144 | + digest="${{ steps.build.outputs.digest }}" |
| 145 | + touch "/tmp/digests/${digest#sha256:}" |
| 146 | +
|
| 147 | + - name: Upload digest |
| 148 | + uses: actions/upload-artifact@v4 |
| 149 | + with: |
| 150 | + name: digests-${{ steps.meta.outputs.platform-pair }} |
| 151 | + path: /tmp/digests/* |
| 152 | + if-no-files-found: error |
| 153 | + retention-days: 1 |
| 154 | + |
| 155 | + merge: |
| 156 | + runs-on: ubuntu-latest |
| 157 | + timeout-minutes: 5 |
| 158 | + |
| 159 | + permissions: |
| 160 | + contents: read |
| 161 | + packages: write |
| 162 | + |
| 163 | + needs: |
| 164 | + - meta |
| 165 | + - build |
| 166 | + |
| 167 | + env: |
| 168 | + REGISTRY_IMAGE: ${{ needs.meta.outputs.image-name }} |
| 169 | + |
| 170 | + steps: |
| 171 | + - name: Download digests |
| 172 | + uses: actions/download-artifact@v4 |
| 173 | + with: |
| 174 | + path: /tmp/digests |
| 175 | + pattern: digests-* |
| 176 | + merge-multiple: true |
| 177 | + |
| 178 | + - name: Set up Docker Buildx |
| 179 | + uses: docker/setup-buildx-action@v3 |
| 180 | + |
| 181 | + - name: Log in to GHCR |
| 182 | + uses: docker/login-action@v3 |
| 183 | + with: |
| 184 | + registry: ${{ env.REGISTRY }} |
| 185 | + username: ${{ github.repository_owner }} |
| 186 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 187 | + |
| 188 | + - name: Create manifest list and push |
| 189 | + working-directory: /tmp/digests |
| 190 | + run: | |
| 191 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 192 | + $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) |
| 193 | + env: |
| 194 | + DOCKER_METADATA_OUTPUT_JSON: ${{ needs.meta.outputs.json }} |
| 195 | + |
| 196 | + - name: Inspect image |
| 197 | + run: | |
| 198 | + docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ needs.meta.outputs.tag-name }} |
| 199 | +
|
| 200 | + notify_failure: |
| 201 | + runs-on: ubuntu-latest |
| 202 | + timeout-minutes: 5 |
| 203 | + |
| 204 | + needs: |
| 205 | + - meta |
| 206 | + - build |
| 207 | + - merge |
| 208 | + |
| 209 | + if: ${{ always() && (contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) }} |
| 210 | + |
| 211 | + steps: |
| 212 | + - name: Notify about CI failure |
| 213 | + run: >- |
| 214 | + curl |
| 215 | + -s -o /dev/null |
| 216 | + -H "Title: ${{ github.repository }} workflow failed" |
| 217 | + -H "Content-Type: text/plain" |
| 218 | + -d $'Repo: ${{ github.repository }}\nWorkflow: ${{ github.workflow }}\nCommit: ${{ github.sha }}\nRef: ${{ github.ref }}\nURL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}' |
| 219 | + "$NTFY_URL" |
| 220 | + env: |
| 221 | + NTFY_URL: ${{ secrets.NTFY_URL }} |
0 commit comments