|
| 1 | +name: Publish Container Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - "containers/**" |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_PREFIX: ${{ github.repository_owner }}/lab |
| 13 | + |
| 14 | +jobs: |
| 15 | + list-containers: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + matrix: ${{ steps.list.outputs.matrix }} |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - id: list |
| 23 | + run: | |
| 24 | + containers=$(ls -d containers/*/ \ |
| 25 | + | xargs -n1 basename \ |
| 26 | + | jq -R -s -c 'split("\n") | map(select(length > 0))') |
| 27 | + echo "matrix=${containers}" >> "$GITHUB_OUTPUT" |
| 28 | +
|
| 29 | + build: |
| 30 | + needs: list-containers |
| 31 | + if: needs.list-containers.outputs.matrix != '[]' |
| 32 | + strategy: |
| 33 | + matrix: |
| 34 | + container: ${{ fromJson(needs.list-containers.outputs.matrix) }} |
| 35 | + arch: [amd64, arm64] |
| 36 | + include: |
| 37 | + - arch: amd64 |
| 38 | + runner: ubuntu-24.04 |
| 39 | + - arch: arm64 |
| 40 | + runner: ubuntu-24.04-arm |
| 41 | + runs-on: ${{ matrix.runner }} |
| 42 | + permissions: |
| 43 | + contents: read |
| 44 | + packages: write |
| 45 | + steps: |
| 46 | + - uses: actions/checkout@v4 |
| 47 | + |
| 48 | + - uses: docker/setup-buildx-action@v3 |
| 49 | + |
| 50 | + - uses: docker/login-action@v3 |
| 51 | + with: |
| 52 | + registry: ${{ env.REGISTRY }} |
| 53 | + username: ${{ github.actor }} |
| 54 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + |
| 56 | + - uses: docker/build-push-action@v6 |
| 57 | + with: |
| 58 | + context: containers/${{ matrix.container }} |
| 59 | + push: true |
| 60 | + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/${{ matrix.container }}:main-${{ matrix.arch }} |
| 61 | + cache-from: type=gha,scope=${{ matrix.container }}-${{ matrix.arch }} |
| 62 | + cache-to: type=gha,mode=max,scope=${{ matrix.container }}-${{ matrix.arch }} |
| 63 | + |
| 64 | + merge-manifest: |
| 65 | + needs: [list-containers, build] |
| 66 | + if: needs.list-containers.outputs.matrix != '[]' |
| 67 | + strategy: |
| 68 | + matrix: |
| 69 | + container: ${{ fromJson(needs.list-containers.outputs.matrix) }} |
| 70 | + runs-on: ubuntu-latest |
| 71 | + permissions: |
| 72 | + contents: read |
| 73 | + packages: write |
| 74 | + steps: |
| 75 | + - uses: docker/setup-buildx-action@v3 |
| 76 | + |
| 77 | + - uses: docker/login-action@v3 |
| 78 | + with: |
| 79 | + registry: ${{ env.REGISTRY }} |
| 80 | + username: ${{ github.actor }} |
| 81 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 82 | + |
| 83 | + - name: Create multi-arch manifest |
| 84 | + run: | |
| 85 | + docker buildx imagetools create \ |
| 86 | + -t ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/${{ matrix.container }}:main \ |
| 87 | + ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/${{ matrix.container }}:main-amd64 \ |
| 88 | + ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/${{ matrix.container }}:main-arm64 |
0 commit comments