-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-push.sh.example
More file actions
70 lines (58 loc) · 2.29 KB
/
Copy pathbuild-push.sh.example
File metadata and controls
70 lines (58 loc) · 2.29 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
#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# Build, push, and deploy a service to Kubernetes
#
# Usage:
# 1. Copy this file into the service directory you want to deploy:
# cp build-push.sh.example rest/build-push.sh
#
# 2. Update the configuration variables below for your environment.
#
# 3. Run from the service directory:
# cd rest && ./build-push.sh
# =============================================================================
# Configuration — update these for your environment
REGISTRY="<your-docker-registry>" # e.g. europe-north1-docker.pkg.dev/my-project/my-repo
IMAGE_NAME="<service-name>" # e.g. rest-backend, provision-log-queue-consumer
KUBE_CONTEXT="<your-kubectl-context>" # e.g. my-cluster-prod
DEPLOYMENT_NAME="<k8s-deployment-name>" # e.g. rest-backend-deployment
DEPLOYMENT_YAML="<deployment-yaml-file>" # e.g. rest-backend-deployment.yaml
# Generate version tag (git commit SHA + timestamp)
GIT_SHA=$(git rev-parse --short HEAD)
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
VERSION_TAG="${GIT_SHA}-${TIMESTAMP}"
# Image tags
IMAGE_VERSIONED="${REGISTRY}/${IMAGE_NAME}:${VERSION_TAG}"
IMAGE_LATEST="${REGISTRY}/${IMAGE_NAME}:latest"
echo "Building version: ${VERSION_TAG}"
# Build with both tags
docker build \
-t "${IMAGE_VERSIONED}" \
-t "${IMAGE_LATEST}" \
.
# Push both tags
echo "Pushing images..."
docker push "${IMAGE_VERSIONED}"
docker push "${IMAGE_LATEST}"
# Switch kubectl context
kubectl config use-context "${KUBE_CONTEXT}"
# Apply the deployment configuration
cd ../gcp/gke || exit 1
echo "Deploying version ${VERSION_TAG}..."
kubectl apply -f "${DEPLOYMENT_YAML}"
# Update the image to the specific version (triggers rolling update)
kubectl set image deployment/${DEPLOYMENT_NAME} \
${DEPLOYMENT_NAME}="${IMAGE_VERSIONED}"
# Wait for rollout to complete
echo "Waiting for rollout to complete..."
kubectl rollout status deployment/${DEPLOYMENT_NAME} --timeout=5m
if [ $? -eq 0 ]; then
echo "Deployment successful! Version: ${VERSION_TAG}"
kubectl get deployment ${DEPLOYMENT_NAME}
echo "To rollback: kubectl rollout undo deployment/${DEPLOYMENT_NAME}"
else
echo "Deployment failed. Rolling back..."
kubectl rollout undo deployment/${DEPLOYMENT_NAME}
exit 1
fi