Build Multi-Architecture Docker Image #96
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build Multi-Architecture Docker Image | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| repository: | |
| description: "Repository name to clone" | |
| required: true | |
| type: string | |
| env: | |
| REGISTRY: ghcr.io | |
| ORGANIZATION: BeaverHouse | |
| jobs: | |
| generate-sha: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| outputs: | |
| sha: ${{ steps.commit-sha.outputs.sha }} | |
| steps: | |
| - name: Checkout target repository | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ env.ORGANIZATION }}/${{ inputs.repository }} | |
| token: ${{ secrets.GH_PAT_ORGANIZATION }} | |
| - name: Get short commit SHA | |
| id: commit-sha | |
| run: echo "sha=$(git rev-parse --short=8 HEAD)" >> $GITHUB_OUTPUT | |
| build-amd64: | |
| runs-on: ubuntu-latest | |
| needs: generate-sha | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout target repository | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ env.ORGANIZATION }}/${{ inputs.repository }} | |
| token: ${{ secrets.GH_PAT_ORGANIZATION }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GH_PAT_ORGANIZATION }} | |
| - name: Set organization name | |
| id: org-name | |
| run: echo "org_lower=$(echo "${{ env.ORGANIZATION }}" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT | |
| - name: Build and push AMD64 image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| platforms: linux/amd64 | |
| push: true | |
| provenance: false | |
| tags: ${{ env.REGISTRY }}/${{ steps.org-name.outputs.org_lower }}/${{ inputs.repository }}:${{ needs.generate-sha.outputs.sha }}-amd64 | |
| cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ steps.org-name.outputs.org_lower }}/${{ inputs.repository }}:buildcache-amd64 | |
| cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ steps.org-name.outputs.org_lower }}/${{ inputs.repository }}:buildcache-amd64,mode=max | |
| secrets: | | |
| gitlab_token=${{ secrets.GITLAB_TOKEN }} | |
| build-args: | | |
| TARGETARCH=amd64 | |
| APP_NAME=${{ inputs.repository }} | |
| build-arm64: | |
| runs-on: ubuntu-24.04-arm | |
| needs: generate-sha | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout target repository | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ env.ORGANIZATION }}/${{ inputs.repository }} | |
| token: ${{ secrets.GH_PAT_ORGANIZATION }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GH_PAT_ORGANIZATION }} | |
| - name: Set organization name | |
| id: org-name | |
| run: echo "org_lower=$(echo "${{ env.ORGANIZATION }}" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT | |
| - name: Build and push ARM64 image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| platforms: linux/arm64 | |
| push: true | |
| provenance: false | |
| tags: ${{ env.REGISTRY }}/${{ steps.org-name.outputs.org_lower }}/${{ inputs.repository }}:${{ needs.generate-sha.outputs.sha }}-arm64 | |
| cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ steps.org-name.outputs.org_lower }}/${{ inputs.repository }}:buildcache-arm64 | |
| cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ steps.org-name.outputs.org_lower }}/${{ inputs.repository }}:buildcache-arm64,mode=max | |
| secrets: | | |
| gitlab_token=${{ secrets.GITLAB_TOKEN }} | |
| build-args: | | |
| TARGETARCH=arm64 | |
| APP_NAME=${{ inputs.repository }} | |
| create-manifest: | |
| runs-on: ubuntu-latest | |
| needs: [generate-sha, build-amd64, build-arm64] | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GH_PAT_ORGANIZATION }} | |
| - name: Create and push multi-arch manifest | |
| run: | | |
| SHA=${{ needs.generate-sha.outputs.sha }} | |
| ORG_LOWER=$(echo "${{ env.ORGANIZATION }}" | tr '[:upper:]' '[:lower:]') | |
| docker manifest create ${{ env.REGISTRY }}/${ORG_LOWER}/${{ inputs.repository }}:${SHA} \ | |
| ${{ env.REGISTRY }}/${ORG_LOWER}/${{ inputs.repository }}:${SHA}-amd64 \ | |
| ${{ env.REGISTRY }}/${ORG_LOWER}/${{ inputs.repository }}:${SHA}-arm64 | |
| docker manifest push ${{ env.REGISTRY }}/${ORG_LOWER}/${{ inputs.repository }}:${SHA} | |
| update-helm-values: | |
| runs-on: ubuntu-latest | |
| needs: [generate-sha, create-manifest] | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GH_PAT_ORGANIZATION }} | |
| - name: Extract service name | |
| id: service-name | |
| run: | | |
| SERVICE_NAME=$(echo "${{ inputs.repository }}" | sed 's/^tinyclover-//') | |
| echo "service=${SERVICE_NAME}" >> $GITHUB_OUTPUT | |
| # yq is installed in Ubuntu runner by default | |
| - name: Check if service exists in values | |
| id: check-service | |
| run: | | |
| if yq eval '. | has("${{ steps.service-name.outputs.service }}")' charts/app-tiny-clover/values/oke.yaml | grep -q "true"; then | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| echo "Service '${{ steps.service-name.outputs.service }}' not found in values.yaml, skipping update" | |
| fi | |
| - name: Update image tag | |
| if: steps.check-service.outputs.exists == 'true' | |
| run: | | |
| yq eval '.${{ steps.service-name.outputs.service }}.image.tag = "${{ needs.generate-sha.outputs.sha }}"' -i charts/app-tiny-clover/values/oke.yaml | |
| echo "Updated ${{ steps.service-name.outputs.service }} tag to ${{ needs.generate-sha.outputs.sha }}" | |
| - name: Commit and push changes | |
| if: steps.check-service.outputs.exists == 'true' | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git add charts/app-tiny-clover/values/oke.yaml | |
| git commit -m "Update ${{ steps.service-name.outputs.service }} image tag to ${{ needs.generate-sha.outputs.sha }}" | |
| git push |