|
| 1 | +name: Continuous Integration |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + |
| 7 | +env: |
| 8 | + REGISTRY: ghcr.io |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +jobs: |
| 15 | + prepare: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + permissions: |
| 18 | + contents: read |
| 19 | + steps: |
| 20 | + - name: Get lowercase GitHub username |
| 21 | + id: repository |
| 22 | + uses: ASzc/change-string-case-action@v6 |
| 23 | + with: |
| 24 | + string: ${{ github.repository }} |
| 25 | + |
| 26 | + - name: Set outputs |
| 27 | + id: set-outputs |
| 28 | + run: | |
| 29 | + echo 'image=ghcr.io/${{ steps.repository.outputs.lowercase }}' >> "${GITHUB_OUTPUT}" |
| 30 | + # Only enable push on push events or pull requests coming from the same repository, except from dependabot |
| 31 | + echo 'push=${{ github.event_name == 'push' || github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' }}' >> "${GITHUB_OUTPUT}" |
| 32 | +
|
| 33 | + - name: Get short SHA |
| 34 | + id: short-sha |
| 35 | + run: echo "short_sha=${GITHUB_SHA:0:12}" >> $GITHUB_ENV && echo "short_sha=${GITHUB_SHA:0:12}" >> $GITHUB_OUTPUT |
| 36 | + |
| 37 | + - name: Extract branch name |
| 38 | + shell: bash |
| 39 | + run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT |
| 40 | + id: extract_branch |
| 41 | + |
| 42 | + - name: Docker meta |
| 43 | + id: meta |
| 44 | + uses: docker/metadata-action@v5 |
| 45 | + with: |
| 46 | + images: | |
| 47 | + ${{ steps.set-outputs.outputs.image }} |
| 48 | + tags: | |
| 49 | + type=raw,value=${{ steps.extract_branch.outputs.branch }}-${{ github.run_number }}-${{ steps.short-sha.outputs.short_sha }} |
| 50 | + outputs: |
| 51 | + image: ${{ steps.set-outputs.outputs.image }} |
| 52 | + push: ${{ steps.set-outputs.outputs.push }} |
| 53 | + meta-version: ${{ steps.meta.outputs.version }} |
| 54 | + meta-labels: ${{ steps.meta.outputs.labels }} |
| 55 | + meta-json: ${{ steps.meta.outputs.json }} |
| 56 | + |
| 57 | + build: |
| 58 | + needs: |
| 59 | + - prepare |
| 60 | + runs-on: ubuntu-latest |
| 61 | + strategy: |
| 62 | + fail-fast: false |
| 63 | + matrix: |
| 64 | + platform: |
| 65 | + - linux/amd64 |
| 66 | +# - linux/arm64 |
| 67 | + permissions: |
| 68 | + contents: read |
| 69 | + packages: write |
| 70 | + |
| 71 | + steps: |
| 72 | + - name: Checkout |
| 73 | + uses: actions/checkout@v4 |
| 74 | + with: |
| 75 | + # Needed to calculate branch for tag |
| 76 | + fetch-depth: 0 |
| 77 | + |
| 78 | + - name: Set up QEMU |
| 79 | + uses: docker/setup-qemu-action@v3 |
| 80 | + |
| 81 | + - name: Set up Docker Buildx |
| 82 | + uses: docker/setup-buildx-action@v3 |
| 83 | + |
| 84 | + - name: Log in to GitHub Container Registry |
| 85 | + if: needs.prepare.outputs.push == 'true' |
| 86 | + uses: docker/login-action@v3 |
| 87 | + with: |
| 88 | + registry: ${{ env.REGISTRY }} |
| 89 | + username: ${{ github.actor }} |
| 90 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 91 | + |
| 92 | + - name: Set cache flags |
| 93 | + id: cache-flags |
| 94 | + run: | |
| 95 | + # Set the cache-to output |
| 96 | + echo 'cache-to=type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }}' >> "${GITHUB_OUTPUT}" |
| 97 | +
|
| 98 | + # Set the cache-from output |
| 99 | + if [[ '${{ github.event_name }}' == 'push' ]]; then |
| 100 | + if [[ '${{ github.ref }}' == 'refs/tags/v'* ]]; then |
| 101 | + # Use cache from the branch when building a tag |
| 102 | + branch="$(git branch -r --contains '${{ github.ref }}')" |
| 103 | + branch="${branch##*/}" |
| 104 | + echo "cache-from=type=gha,scope=${branch}-${{ matrix.platform }}" >> "${GITHUB_OUTPUT}" |
| 105 | + else |
| 106 | + # Use cache from the same branch when not building a tag |
| 107 | + echo 'cache-from=type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }}' >> "${GITHUB_OUTPUT}" |
| 108 | + fi |
| 109 | + else |
| 110 | + # Use cache from target branch too when building a pull request |
| 111 | +
|
| 112 | + # In this case, it has to be a multiline string |
| 113 | + # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings |
| 114 | + EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
| 115 | +
|
| 116 | + echo "cache-from<<${EOF}" >> "${GITHUB_OUTPUT}" |
| 117 | +
|
| 118 | + printf '%s\n' \ |
| 119 | + "type=gha,scope=${{ github.ref_name }}-${{ matrix.platform }}" \ |
| 120 | + "type=gha,scope=${{ github.base_ref }}-${{ matrix.platform }}" \ |
| 121 | + >> "${GITHUB_OUTPUT}" |
| 122 | +
|
| 123 | + echo "${EOF}" >> "${GITHUB_OUTPUT}" |
| 124 | + fi |
| 125 | +
|
| 126 | + - name: Build and push by digest |
| 127 | + id: build |
| 128 | + uses: docker/build-push-action@v6 |
| 129 | + with: |
| 130 | + context: . |
| 131 | + platforms: ${{ matrix.platform }} |
| 132 | + labels: ${{ needs.prepare.outputs.meta-labels }} |
| 133 | + outputs: | |
| 134 | + type=image,name=${{ needs.prepare.outputs.image }},push-by-digest=true,name-canonical=true,push=${{ needs.prepare.outputs.push }} |
| 135 | + cache-from: | |
| 136 | + ${{ steps.cache-flags.outputs.cache-from }} |
| 137 | + cache-to: | |
| 138 | + ${{ steps.cache-flags.outputs.cache-to }} |
| 139 | +
|
| 140 | + - name: Export digest |
| 141 | + run: | |
| 142 | + mkdir -p /tmp/digests |
| 143 | + digest='${{ steps.build.outputs.digest }}' |
| 144 | + touch "/tmp/digests/${digest#sha256:}" |
| 145 | +
|
| 146 | + - name: Upload digest |
| 147 | + uses: actions/upload-artifact@v4 |
| 148 | + with: |
| 149 | + name: digests |
| 150 | + path: /tmp/digests/* |
| 151 | + if-no-files-found: error |
| 152 | + retention-days: 1 |
| 153 | + |
| 154 | + push: |
| 155 | + needs: |
| 156 | + - prepare |
| 157 | + - build |
| 158 | + runs-on: ubuntu-latest |
| 159 | + permissions: |
| 160 | + contents: read |
| 161 | + packages: write |
| 162 | + steps: |
| 163 | + - name: Download digests |
| 164 | + uses: actions/download-artifact@v4 |
| 165 | + with: |
| 166 | + name: digests |
| 167 | + path: /tmp/digests |
| 168 | + |
| 169 | + - name: Set up Docker Buildx |
| 170 | + uses: docker/setup-buildx-action@v3 |
| 171 | + |
| 172 | + - name: Login to GitHub Container Registry |
| 173 | + if: needs.prepare.outputs.push == 'true' |
| 174 | + uses: docker/login-action@v3 |
| 175 | + with: |
| 176 | + registry: ghcr.io |
| 177 | + username: ${{ github.repository_owner }} |
| 178 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 179 | + |
| 180 | + - name: Create manifest list and push |
| 181 | + if: needs.prepare.outputs.push == 'true' |
| 182 | + working-directory: /tmp/digests |
| 183 | + run: | |
| 184 | + docker buildx imagetools create $(jq -r '"-t " + (.tags | join(" -t "))' <<< '${{ needs.prepare.outputs.meta-json }}') \ |
| 185 | + $(printf '${{ needs.prepare.outputs.image }}@sha256:%s ' *) |
| 186 | +
|
| 187 | + - name: Inspect image |
| 188 | + if: needs.prepare.outputs.push == 'true' |
| 189 | + run: | |
| 190 | + docker buildx imagetools inspect '${{ needs.prepare.outputs.image }}:${{ needs.prepare.outputs.meta-version }}' |
0 commit comments