-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpush-image.sh
More file actions
executable file
·80 lines (68 loc) · 2.61 KB
/
Copy pathpush-image.sh
File metadata and controls
executable file
·80 lines (68 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
#
# Build a Docker image and push it to a registry, tagged with the last commit's
# short hash (and :latest). Nothing is hardcoded — everything is passed as a flag
# (or an env var). Run it from the repo whose image you want to build.
#
set -euo pipefail
# All configurable values; env vars act as fallbacks, flags override them.
REGISTRY="${REGISTRY:-}"
IMAGE="${IMAGE:-castit}"
DOCKERFILE="${DOCKERFILE:-CastIt.Server/Dockerfile}"
PLATFORM="${PLATFORM:-linux/arm64}"
CONTEXT="${CONTEXT:-.}"
PUSH_LATEST=1
usage() {
cat >&2 <<'EOF'
Usage: ./push-image.sh --registry <host> --image <name> [options]
--registry <host> Registry host (env: REGISTRY) [required]
--image <name> Image repository name (env: IMAGE, default: castit)
--dockerfile <path> Dockerfile path (env: DOCKERFILE, default: CastIt.Server/Dockerfile)
--platform <p> Target platform (env: PLATFORM, default: linux/arm64)
--context <dir> Build context (env: CONTEXT, default: .)
--sha-only Push only the :<sha> tag (skip :latest)
-h, --help Show this help
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--registry) REGISTRY="$2"; shift 2 ;;
--image) IMAGE="$2"; shift 2 ;;
--dockerfile) DOCKERFILE="$2"; shift 2 ;;
--platform) PLATFORM="$2"; shift 2 ;;
--context) CONTEXT="$2"; shift 2 ;;
--sha-only) PUSH_LATEST=0; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown argument: $1" >&2; usage; exit 1 ;;
esac
done
[[ -n "$REGISTRY" ]] || { echo "ERROR: --registry (or REGISTRY env) is required" >&2; exit 1; }
# Always run from the repo root (where this script lives)
cd "$(dirname "$0")"
# Tag = last commit's short hash
SHA="$(git rev-parse --short HEAD)"
REF="${REGISTRY}/${IMAGE}"
# Warn if there are uncommitted changes — the tag won't reflect them
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "WARNING: working tree has uncommitted changes; tagging as ${SHA} anyway." >&2
fi
# Make sure we're authenticated to the registry
if ! grep -q "${REGISTRY}" "${HOME}/.docker/config.json" 2>/dev/null; then
echo "Not logged in to ${REGISTRY} — running docker login..."
docker login "${REGISTRY}"
fi
# Build tag list
TAGS=(-t "${REF}:${SHA}")
[[ "$PUSH_LATEST" -eq 1 ]] && TAGS+=(-t "${REF}:latest")
echo "Building and pushing ${REF}:${SHA} (${PLATFORM})..."
docker buildx build \
--platform "${PLATFORM}" \
--build-arg "TARGETARCH=${PLATFORM##*/}" \
"${TAGS[@]}" \
-f "${DOCKERFILE}" \
--push \
"${CONTEXT}"
echo
echo "Pushed:"
echo " ${REF}:${SHA}"
[[ "$PUSH_LATEST" -eq 1 ]] && echo " ${REF}:latest"