-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-build-and-push.sh
More file actions
64 lines (51 loc) · 1.92 KB
/
Copy path03-build-and-push.sh
File metadata and controls
64 lines (51 loc) · 1.92 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
#!/usr/bin/env bash
# Builds the ocrapi container image and pushes it to Artifact Registry.
# Run from the REPO ROOT on your dev machine (Docker required).
#
# bash deploy/gcp/03-build-and-push.sh
#
# Outputs the fully qualified image tag; feed it to 04-run.sh on the VM.
set -euo pipefail
PROJECT_ID="${PROJECT_ID:-$(gcloud config get-value project 2>/dev/null)}"
REGION="${REGION:-us-central1}"
AR_REPO="${AR_REPO:-ocrapi}"
IMAGE_NAME="${IMAGE_NAME:-ocrapi}"
if [[ -z "${PROJECT_ID}" ]]; then
echo "ERROR: PROJECT_ID is empty. Run 'gcloud config set project <id>' first." >&2
exit 1
fi
# Run from repo root regardless of cwd.
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "${REPO_ROOT}"
# Tag with date + short git SHA when available; falls back to date+random.
DATE_TAG="$(date -u +%Y-%m-%d)"
if git rev-parse --short HEAD >/dev/null 2>&1; then
SHA_TAG="$(git rev-parse --short HEAD)"
else
SHA_TAG="$(date -u +%H%M%S)"
fi
TAG="${DATE_TAG}-${SHA_TAG}"
REGISTRY="${REGION}-docker.pkg.dev/${PROJECT_ID}/${AR_REPO}"
FULL_TAG="${REGISTRY}/${IMAGE_NAME}:${TAG}"
LATEST_TAG="${REGISTRY}/${IMAGE_NAME}:latest"
echo "==> Authenticating Docker with Artifact Registry..."
gcloud auth configure-docker "${REGION}-docker.pkg.dev" --quiet
echo "==> Building ${FULL_TAG} (and :latest) for linux/amd64..."
# c4 VMs are amd64; explicit --platform avoids surprises when building on arm64 (Apple Silicon).
docker buildx build \
--platform=linux/amd64 \
-t "${FULL_TAG}" \
-t "${LATEST_TAG}" \
--push \
.
cat <<EOF
============================================================
Build & push complete.
Image: ${FULL_TAG}
Also tagged as: ${LATEST_TAG}
On the VM, write the image tag and (re)start the service:
sudo bash -c 'echo "IMAGE=${FULL_TAG}" > /etc/ocrapi.image'
sudo systemctl restart ocrapi.service
sudo systemctl status ocrapi.service --no-pager
============================================================
EOF