|
| 1 | +name: build-docker-containers |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + build: |
| 7 | + description: "Build and publish native multi-arch image" |
| 8 | + type: boolean |
| 9 | + default: true |
| 10 | + update_readme: |
| 11 | + description: "Also sync DockerHub README" |
| 12 | + type: boolean |
| 13 | + default: false |
| 14 | + push: |
| 15 | + branches: |
| 16 | + - dev |
| 17 | + - master |
| 18 | + tags: |
| 19 | + - "v*" |
| 20 | + paths-ignore: |
| 21 | + - "**.md" |
| 22 | + - ".github/ISSUE_TEMPLATE/**" |
| 23 | + |
| 24 | +jobs: |
| 25 | + test: |
| 26 | + uses: ./.github/workflows/ci.yml |
| 27 | + |
| 28 | + docker-build-arch: |
| 29 | + name: Build Container (${{ matrix.arch }}) |
| 30 | + runs-on: ${{ matrix.os }} |
| 31 | + needs: [test] |
| 32 | + strategy: |
| 33 | + matrix: |
| 34 | + include: |
| 35 | + - os: ubuntu-latest |
| 36 | + arch: amd64 |
| 37 | + - os: ubuntu-latest |
| 38 | + arch: arm64 |
| 39 | + permissions: |
| 40 | + packages: write |
| 41 | + contents: write |
| 42 | + env: |
| 43 | + REGISTRY: ${{ vars.REGISTRY != '' && vars.REGISTRY || 'ghcr.io' }} |
| 44 | + SLUG: ${{ format('{0}/{1}', vars.REGISTRY != '' && vars.REGISTRY || 'ghcr.io', github.repository) }} |
| 45 | + ARCH: ${{ matrix.arch }} |
| 46 | + |
| 47 | + steps: |
| 48 | + - name: Checkout |
| 49 | + uses: actions/checkout@v4 |
| 50 | + with: |
| 51 | + fetch-depth: 0 |
| 52 | + |
| 53 | + - name: Docker meta |
| 54 | + id: meta |
| 55 | + uses: docker/metadata-action@v5 |
| 56 | + with: |
| 57 | + images: ${{ env.SLUG }} |
| 58 | + flavor: | |
| 59 | + latest=false |
| 60 | + suffix=-${{ env.ARCH }} |
| 61 | + tags: | |
| 62 | + type=ref,event=branch |
| 63 | + type=ref,event=tag |
| 64 | + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }} |
| 65 | +
|
| 66 | + - name: Login to Container Registry |
| 67 | + uses: docker/login-action@v3 |
| 68 | + with: |
| 69 | + registry: ${{ env.REGISTRY }} |
| 70 | + username: ${{ github.actor }} |
| 71 | + password: ${{ secrets.GIT_TOKEN && secrets.GIT_TOKEN || secrets.GITHUB_TOKEN }} |
| 72 | + |
| 73 | + - name: Set up Docker Buildx |
| 74 | + uses: docker/setup-buildx-action@v3 |
| 75 | + |
| 76 | + - name: Build Container and push |
| 77 | + uses: docker/build-push-action@v5 |
| 78 | + with: |
| 79 | + context: . |
| 80 | + platforms: linux/${{ env.ARCH }} |
| 81 | + push: true |
| 82 | + tags: ${{ steps.meta.outputs.tags }} |
| 83 | + labels: ${{ steps.meta.outputs.labels }} |
| 84 | + cache-from: | |
| 85 | + type=gha,scope=${{ github.workflow }} |
| 86 | + cache-to: | |
| 87 | + type=gha,mode=max,scope=${{ github.workflow }} |
| 88 | + provenance: true |
| 89 | + |
| 90 | + docker-publish-manifest: |
| 91 | + name: Publish multi-arch manifest |
| 92 | + needs: [docker-build-arch] |
| 93 | + runs-on: ubuntu-latest |
| 94 | + permissions: |
| 95 | + packages: write |
| 96 | + contents: write |
| 97 | + env: |
| 98 | + REGISTRY: ${{ vars.REGISTRY != '' && vars.REGISTRY || 'ghcr.io' }} |
| 99 | + SLUG: ${{ format('{0}/{1}', vars.REGISTRY != '' && vars.REGISTRY || 'ghcr.io', github.repository) }} |
| 100 | + steps: |
| 101 | + - name: Docker meta |
| 102 | + id: meta |
| 103 | + uses: docker/metadata-action@v5 |
| 104 | + with: |
| 105 | + images: ${{ env.SLUG }} |
| 106 | + flavor: | |
| 107 | + latest=false |
| 108 | + tags: | |
| 109 | + type=ref,event=branch |
| 110 | + type=ref,event=tag |
| 111 | + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }} |
| 112 | +
|
| 113 | + - name: Login to Container Registry |
| 114 | + uses: docker/login-action@v3 |
| 115 | + with: |
| 116 | + registry: ${{ env.REGISTRY }} |
| 117 | + username: ${{ github.actor }} |
| 118 | + password: ${{ secrets.GIT_TOKEN && secrets.GIT_TOKEN || secrets.GITHUB_TOKEN }} |
| 119 | + |
| 120 | + - name: Create and push manifest list |
| 121 | + shell: bash |
| 122 | + run: | |
| 123 | + IFS=$'\n' |
| 124 | + for tag in $(echo "${{ steps.meta.outputs.tags }}"); do |
| 125 | + echo "Creating manifest for ${tag}" |
| 126 | + docker buildx imagetools create \ |
| 127 | + --tag "$tag" \ |
| 128 | + "${tag}-amd64" \ |
| 129 | + "${tag}-arm64" |
| 130 | + done |
| 131 | +
|
| 132 | + - name: Overwrite GitHub release notes |
| 133 | + if: startsWith(github.ref, 'refs/tags/v') && startsWith(env.REGISTRY, 'ghcr.io/') |
| 134 | + uses: actions/github-script@v6 |
| 135 | + with: |
| 136 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + script: | |
| 138 | + const tag = context.ref.replace('refs/tags/', ''); |
| 139 | +
|
| 140 | + const latestRelease = await github.rest.repos.listReleases({ |
| 141 | + owner: context.repo.owner, |
| 142 | + repo: context.repo.repo, |
| 143 | + }); |
| 144 | +
|
| 145 | + const release = latestRelease.data.find(r => r.tag_name === tag); |
| 146 | + if (!release) { |
| 147 | + core.setFailed(`Release with tag ${tag} not found`); |
| 148 | + return; |
| 149 | + } |
| 150 | +
|
| 151 | + const { data: comparison } = await github.rest.repos.compareCommits({ |
| 152 | + owner: context.repo.owner, |
| 153 | + repo: context.repo.repo, |
| 154 | + base: latestRelease.data[1]?.tag_name || '', |
| 155 | + head: tag, |
| 156 | + }); |
| 157 | +
|
| 158 | + const commits = comparison.commits.filter(c => 1 === c.parents.length); |
| 159 | +
|
| 160 | + const changelog = commits.map( |
| 161 | + c => `- ${c.sha.substring(0, 7)} ${c.commit.message.split('\n')[0]} by @${c.commit.author.name}` |
| 162 | + ).join('\n'); |
| 163 | +
|
| 164 | + if (!changelog) { |
| 165 | + core.setFailed('No commits found for the changelog'); |
| 166 | + return; |
| 167 | + } |
| 168 | +
|
| 169 | + console.log(`Changelog for ${tag}:\n${changelog}`); |
| 170 | +
|
| 171 | + await github.rest.repos.updateRelease({ |
| 172 | + owner: context.repo.owner, |
| 173 | + repo: context.repo.repo, |
| 174 | + release_id: release.id, |
| 175 | + body: changelog |
| 176 | + }); |
0 commit comments