forked from Gene-Weaver/VoucherVisionGO
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudbuild.yaml
More file actions
93 lines (84 loc) · 3.92 KB
/
Copy pathcloudbuild.yaml
File metadata and controls
93 lines (84 loc) · 3.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
steps:
# Step 1: Check if we need to rebuild dependencies and store the result
- name: 'gcr.io/cloud-builders/git'
id: check-changes
entrypoint: 'bash'
args:
- '-c'
- |
# Default to false for first build where we can't diff
_DEPS_CHANGED="false"
# Check if we can access previous commit
if git rev-parse --verify ${COMMIT_SHA}~1 >/dev/null 2>&1; then
# Check if dependency files changed between this commit and the previous one
if git diff --name-only ${COMMIT_SHA}~1 ${COMMIT_SHA} | grep -q -E 'requirements.txt|Dockerfile|entrypoint.sh'; then
_DEPS_CHANGED="true"
fi
else
# First commit or shallow clone - rebuild required
_DEPS_CHANGED="true"
fi
# Store the result as a build artifact
echo $_DEPS_CHANGED > /workspace/_deps_changed.txt
echo "Dependencies changed: $_DEPS_CHANGED"
# Step 2: Pull the latest image to use as cache
- name: 'gcr.io/cloud-builders/docker'
id: pull-cache
entrypoint: 'bash'
args:
- '-c'
- |
# Try to pull the latest image to use as cache, ignore errors if it doesn't exist
docker pull us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:latest || true
# Step 3: Build with appropriate caching strategy based on whether deps changed
- name: 'gcr.io/cloud-builders/docker'
id: build
entrypoint: 'bash'
args:
- '-c'
- |
_DEPS_CHANGED=$(cat /workspace/_deps_changed.txt)
if [ "$_DEPS_CHANGED" = "true" ]; then
echo "Rebuilding container from scratch..."
docker build --no-cache -t us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:${COMMIT_SHA} .
else
echo "Performing incremental build..."
docker build --cache-from us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:latest -t us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:${COMMIT_SHA} .
fi
# Step 4: Push the container image to Artifact Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:${COMMIT_SHA}']
# Step 5: Tag the image as latest
- name: 'gcr.io/cloud-builders/docker'
args: ['tag', 'us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:${COMMIT_SHA}', 'us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:latest']
# Step 6: Push the latest tag
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:latest']
# Step 7: Deploy container image to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
- 'run'
- 'deploy'
- 'vouchervision-go'
- '--image'
- 'us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:${COMMIT_SHA}'
- '--region'
- 'us-central1'
- '--platform'
- 'managed'
- '--allow-unauthenticated'
# Store images in Artifact Registry
images:
- 'us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:${COMMIT_SHA}'
- 'us-central1-docker.pkg.dev/${PROJECT_ID}/cloud-run-source-deploy/vouchervisiongo/vouchervision-go:latest'
# Service account and logging configuration
serviceAccount: 'projects/${PROJECT_ID}/serviceAccounts/${_SERVICE_ACCOUNT_EMAIL}'
logsBucket: 'gs://${_BUCKET}'
options:
logging: GCS_ONLY
# Define substitution variables
substitutions:
_API_KEY: '' # Will be provided during deployment
_SERVICE_ACCOUNT_EMAIL: '' # Service account email
_BUCKET: '' # Logs bucket name