Created
October 10, 2025 05:57
-
-
Save DeepanshKhurana/36b8f888d1d29ecf00c8b793ae11851d to your computer and use it in GitHub Desktop.
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
# multiarch-docker-build | |
# Usage: | |
# multiarch-docker-build [options] <registry/image> [extra-tag] | |
# | |
# Options: | |
# --builder <name> buildx builder name (default: multiarch-builder) | |
# --platforms <list> target platforms (default: linux/amd64,linux/arm64) | |
# --dockerfile <path> Dockerfile path (default: Dockerfile) | |
# --context <path> build context (default: .) | |
# --push push multi-arch (default) | |
# --load load single-arch locally | |
# --github-pat <token> forward a GitHub PAT as build arg | |
# --ssh enable SSH tunneling (uses default or provided key) | |
# --ssh-key <path> path to private key (default: ~/.ssh/id_ed25519_github) | |
multiarch-docker-build () { | |
local builder="multiarch-builder" | |
local plats="linux/amd64,linux/arm64" | |
local df="Dockerfile" | |
local ctx="." | |
local push=1 | |
local ssh=0 | |
local ssh_key="$HOME/.ssh/id_ed25519_github" | |
local pat="" | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
--builder) builder="$2"; shift 2 ;; | |
--platforms) plats="$2"; shift 2 ;; | |
--dockerfile) df="$2"; shift 2 ;; | |
--context) ctx="$2"; shift 2 ;; | |
--push) push=1; shift ;; | |
--load) push=0; shift ;; | |
--github-pat) pat="$2"; shift 2 ;; | |
--ssh) ssh=1; shift ;; | |
--ssh-key) ssh_key="$2"; shift 2 ;; | |
-h|--help) | |
echo "Usage: multiarch-docker-build [options] <registry/image> [extra-tag]" | |
return 0 ;; | |
*) break ;; | |
esac | |
done | |
local img="${1:-}"; shift || true | |
local extra="${1:-}" | |
[[ -z "$img" ]] && { echo "Error: image name required"; return 1; } | |
local stamp="$(date +%Y%m%d-%H%M)" | |
if ! docker buildx ls | grep -q "^${builder}\b"; then | |
docker buildx create --name "$builder" --use | |
else | |
docker buildx use "$builder" | |
fi | |
docker buildx inspect --bootstrap >/dev/null 2>&1 | |
local args=(docker buildx build --platform "$plats" -f "$df" | |
-t "$img:latest" -t "$img:$stamp") | |
[[ -n "$extra" ]] && args+=(-t "$img:$extra") | |
[[ -n "$pat" ]] && args+=(--build-arg "GITHUB_PAT=$pat") | |
if [[ "$ssh" -eq 1 ]]; then | |
if [[ -z "${SSH_AUTH_SOCK:-}" ]]; then eval "$(ssh-agent -s)" >/dev/null; fi | |
[[ -r "$ssh_key" ]] && ssh-add "$ssh_key" >/dev/null 2>&1 || true | |
args+=(--ssh default) | |
fi | |
args+=("$ctx") | |
[[ "$push" -eq 1 ]] && args+=(--push) || args+=(--load) | |
DOCKER_BUILDKIT=1 "${args[@]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment