Update build.yml

This commit is contained in:
drifty
2025-08-11 15:00:04 +05:30
committed by GitHub
parent e9c14dd30d
commit b8af573c93

View File

@@ -1,117 +1,153 @@
name: Multi-Arch Docker Build & Push name: Build and Push Multi-Arch Docker Images
on: on:
workflow_dispatch: workflow_dispatch:
inputs: inputs:
image_name: image_name:
description: "Image name without registry (e.g. myuser/myimage)" description: 'Docker image name (e.g. user/image)'
required: true required: true
type: string type: string
tags: tags:
description: "Comma-separated list of tags (e.g. v1.0.1,latest,dev,beta)" description: 'Comma-separated tags (e.g. v1.01,latest,dev,beta)'
required: true required: true
type: string type: string
push_dockerhub: push_dockerhub:
description: "Push to Docker Hub" description: 'Push to Docker Hub'
required: true required: false
type: boolean type: boolean
default: false default: true
push_ghcr: push_ghcr:
description: "Push to GitHub Container Registry" description: 'Push to GitHub Container Registry'
required: true required: false
type: boolean type: boolean
default: false default: true
push_quay: push_quay:
description: "Push to Quay.io" description: 'Push to Quay.io'
required: true required: false
type: boolean type: boolean
default: false default: false
jobs: jobs:
build-and-push: build-and-push:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v2
- name: Normalize tags - name: Log in to Docker Hub
id: vars if: ${{ inputs.push_dockerhub == 'true' }}
run: | uses: docker/login-action@v2
TAGS_INPUT="${{ github.event.inputs.tags }}"
IFS=',' read -ra TAGS_ARRAY <<< "$TAGS_INPUT"
CLEAN_TAGS=()
for t in "${TAGS_ARRAY[@]}"; do
CLEAN_TAGS+=("$(echo "$t" | xargs)")
done
echo "tags_list=${CLEAN_TAGS[*]}" >> "$GITHUB_OUTPUT"
# Logins
- name: Login to Docker Hub
if: ${{ github.event.inputs.push_dockerhub == 'true' }}
uses: docker/login-action@v3
with: with:
username: ${{ secrets.DOCKERHUB_USERNAME }} registry: docker.io
username: ${{ github.actor }}
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GHCR - name: Log in to GitHub Container Registry
if: ${{ github.event.inputs.push_ghcr == 'true' }} if: ${{ inputs.push_ghcr == 'true' }}
uses: docker/login-action@v3 uses: docker/login-action@v2
with: with:
registry: ghcr.io registry: ghcr.io
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }} password: ${{ secrets.GHCR_TOKEN }}
- name: Login to Quay.io - name: Log in to Quay.io
if: ${{ github.event.inputs.push_quay == 'true' }} if: ${{ inputs.push_quay == 'true' }}
uses: docker/login-action@v3 uses: docker/login-action@v2
with: with:
registry: quay.io registry: quay.io
username: ${{ secrets.QUAY_USERNAME }} username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.QUAY_TOKEN }} password: ${{ secrets.QUAY_PASSWORD }}
# Build once and save to local cache - name: Prepare tags
- name: Build multi-arch image id: prepare_tags
run: | run: |
I="${{ inputs.image_name }}"
# Remove spaces from tags and split by comma
TAGS_RAW="${{ inputs.tags }}"
TAGS=$(echo $TAGS_RAW | tr -d ' ' | tr ',' '\n')
echo "image_name=$I" >> $GITHUB_OUTPUT
echo "tags=$TAGS_RAW" >> $GITHUB_OUTPUT
echo "::set-output name=tags_list::$TAGS_RAW"
echo "tag_array<<EOF" >> $GITHUB_OUTPUT
echo "$TAGS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Build Docker image (multi-arch)
id: build_image
run: |
set -e
image_name="${{ inputs.image_name }}"
tags_raw="${{ inputs.tags }}"
tags=$(echo $tags_raw | tr -d ' ' | tr ',' ' ')
# Build with all tags on local docker cache (no push)
# Use buildx to build for linux/amd64,linux/arm64
docker buildx build \ docker buildx build \
--platform linux/amd64,linux/arm64 \ --platform linux/amd64,linux/arm64 \
--output=type=docker \ --load \
-t tempimage:build . $(for t in $tags; do echo "-t $image_name:$t "; done) \
.
shell: bash
# Push to Docker Hub
- name: Push to Docker Hub - name: Push to Docker Hub
if: ${{ github.event.inputs.push_dockerhub == 'true' }} if: ${{ inputs.push_dockerhub == 'true' }}
run: | run: |
for tag in ${{ steps.vars.outputs.tags_list }}; do set +e
docker tag tempimage:build docker.io/${{ github.event.inputs.image_name }}:$tag image_name="${{ inputs.image_name }}"
docker push docker.io/${{ github.event.inputs.image_name }}:$tag || echo "Docker Hub push failed for tag $tag" tags_raw="${{ inputs.tags }}"
tags=$(echo $tags_raw | tr -d ' ' | tr ',' ' ')
error=0
for tag in $tags; do
docker tag "$image_name:$tag" "docker.io/$image_name:$tag"
done done
continue-on-error: true for tag in $tags; do
echo "Pushing docker.io/$image_name:$tag"
docker push "docker.io/$image_name:$tag" || error=1
done
if [ $error -ne 0 ]; then
echo "Warning: Some pushes to Docker Hub failed."
fi
shell: bash
# Push to GHCR - name: Push to GitHub Container Registry
- name: Push to GHCR if: ${{ inputs.push_ghcr == 'true' }}
if: ${{ github.event.inputs.push_ghcr == 'true' }}
run: | run: |
for tag in ${{ steps.vars.outputs.tags_list }}; do set +e
docker tag tempimage:build ghcr.io/${{ github.event.inputs.image_name }}:$tag image_name="${{ inputs.image_name }}"
docker push ghcr.io/${{ github.event.inputs.image_name }}:$tag || echo "GHCR push failed for tag $tag" tags_raw="${{ inputs.tags }}"
tags=$(echo $tags_raw | tr -d ' ' | tr ',' ' ')
error=0
for tag in $tags; do
docker tag "$image_name:$tag" "ghcr.io/${{ github.repository_owner }}/$image_name:$tag"
done done
continue-on-error: true for tag in $tags; do
echo "Pushing ghcr.io/${{ github.repository_owner }}/$image_name:$tag"
docker push "ghcr.io/${{ github.repository_owner }}/$image_name:$tag" || error=1
done
if [ $error -ne 0 ]; then
echo "Warning: Some pushes to GHCR failed."
fi
shell: bash
# Push to Quay.io
- name: Push to Quay.io - name: Push to Quay.io
if: ${{ github.event.inputs.push_quay == 'true' }} if: ${{ inputs.push_quay == 'true' }}
run: | run: |
for tag in ${{ steps.vars.outputs.tags_list }}; do set +e
docker tag tempimage:build quay.io/${{ github.event.inputs.image_name }}:$tag image_name="${{ inputs.image_name }}"
docker push quay.io/${{ github.event.inputs.image_name }}:$tag || echo "Quay push failed for tag $tag" tags_raw="${{ inputs.tags }}"
tags=$(echo $tags_raw | tr -d ' ' | tr ',' ' ')
error=0
for tag in $tags; do
docker tag "$image_name:$tag" "quay.io/$image_name:$tag"
done done
continue-on-error: true for tag in $tags; do
echo "Pushing quay.io/$image_name:$tag"
docker push "quay.io/$image_name:$tag" || error=1
done
if [ $error -ne 0 ]; then
echo "Warning: Some pushes to Quay.io failed."
fi
shell: bash