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 - name: Set up Docker Buildx
uses: docker/setup-qemu-action@v3 uses: docker/setup-buildx-action@v2
- name: Set up Docker Buildx - name: Log in to Docker Hub
uses: docker/setup-buildx-action@v3 if: ${{ inputs.push_dockerhub == 'true' }}
uses: docker/login-action@v2
with:
registry: docker.io
username: ${{ github.actor }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Normalize tags - name: Log in to GitHub Container Registry
id: vars if: ${{ inputs.push_ghcr == 'true' }}
run: | uses: docker/login-action@v2
TAGS_INPUT="${{ github.event.inputs.tags }}" with:
IFS=',' read -ra TAGS_ARRAY <<< "$TAGS_INPUT" registry: ghcr.io
CLEAN_TAGS=() username: ${{ github.actor }}
for t in "${TAGS_ARRAY[@]}"; do password: ${{ secrets.GHCR_TOKEN }}
CLEAN_TAGS+=("$(echo "$t" | xargs)")
done
echo "tags_list=${CLEAN_TAGS[*]}" >> "$GITHUB_OUTPUT"
# Logins - name: Log in to Quay.io
- name: Login to Docker Hub if: ${{ inputs.push_quay == 'true' }}
if: ${{ github.event.inputs.push_dockerhub == 'true' }} uses: docker/login-action@v2
uses: docker/login-action@v3 with:
with: registry: quay.io
username: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.QUAY_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.QUAY_PASSWORD }}
- name: Login to GHCR - name: Prepare tags
if: ${{ github.event.inputs.push_ghcr == 'true' }} id: prepare_tags
uses: docker/login-action@v3 run: |
with: I="${{ inputs.image_name }}"
registry: ghcr.io # Remove spaces from tags and split by comma
username: ${{ github.actor }} TAGS_RAW="${{ inputs.tags }}"
password: ${{ secrets.GHCR_TOKEN }} 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: Login to Quay.io - name: Build Docker image (multi-arch)
if: ${{ github.event.inputs.push_quay == 'true' }} id: build_image
uses: docker/login-action@v3 run: |
with: set -e
registry: quay.io image_name="${{ inputs.image_name }}"
username: ${{ secrets.QUAY_USERNAME }} tags_raw="${{ inputs.tags }}"
password: ${{ secrets.QUAY_TOKEN }} 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 \
--platform linux/amd64,linux/arm64 \
--load \
$(for t in $tags; do echo "-t $image_name:$t "; done) \
.
shell: bash
# Build once and save to local cache - name: Push to Docker Hub
- name: Build multi-arch image if: ${{ inputs.push_dockerhub == 'true' }}
run: | run: |
docker buildx build \ set +e
--platform linux/amd64,linux/arm64 \ image_name="${{ inputs.image_name }}"
--output=type=docker \ tags_raw="${{ inputs.tags }}"
-t tempimage:build . 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
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 Docker Hub - name: Push to GitHub Container Registry
- name: Push to Docker Hub if: ${{ inputs.push_ghcr == 'true' }}
if: ${{ github.event.inputs.push_dockerhub == 'true' }} run: |
run: | set +e
for tag in ${{ steps.vars.outputs.tags_list }}; do image_name="${{ inputs.image_name }}"
docker tag tempimage:build docker.io/${{ github.event.inputs.image_name }}:$tag tags_raw="${{ inputs.tags }}"
docker push docker.io/${{ github.event.inputs.image_name }}:$tag || echo "Docker Hub push failed for tag $tag" tags=$(echo $tags_raw | tr -d ' ' | tr ',' ' ')
done error=0
continue-on-error: true for tag in $tags; do
docker tag "$image_name:$tag" "ghcr.io/${{ github.repository_owner }}/$image_name:$tag"
done
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 GHCR - name: Push to Quay.io
- name: Push to GHCR if: ${{ inputs.push_quay == 'true' }}
if: ${{ github.event.inputs.push_ghcr == 'true' }} run: |
run: | set +e
for tag in ${{ steps.vars.outputs.tags_list }}; do image_name="${{ inputs.image_name }}"
docker tag tempimage:build ghcr.io/${{ github.event.inputs.image_name }}:$tag tags_raw="${{ inputs.tags }}"
docker push ghcr.io/${{ github.event.inputs.image_name }}:$tag || echo "GHCR push failed for tag $tag" tags=$(echo $tags_raw | tr -d ' ' | tr ',' ' ')
done error=0
continue-on-error: true for tag in $tags; do
docker tag "$image_name:$tag" "quay.io/$image_name:$tag"
# Push to Quay.io done
- name: Push to Quay.io for tag in $tags; do
if: ${{ github.event.inputs.push_quay == 'true' }} echo "Pushing quay.io/$image_name:$tag"
run: | docker push "quay.io/$image_name:$tag" || error=1
for tag in ${{ steps.vars.outputs.tags_list }}; do done
docker tag tempimage:build quay.io/${{ github.event.inputs.image_name }}:$tag if [ $error -ne 0 ]; then
docker push quay.io/${{ github.event.inputs.image_name }}:$tag || echo "Quay push failed for tag $tag" echo "Warning: Some pushes to Quay.io failed."
done fi
continue-on-error: true shell: bash