forked from shivaaws69/Wisdom-School-Spring-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudbuild-tag.yaml
More file actions
168 lines (145 loc) · 4.69 KB
/
cloudbuild-tag.yaml
File metadata and controls
168 lines (145 loc) · 4.69 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# substitutions:
# _REPO: "asia-docker.pkg.dev/$_PROJECT_ID/school/sch"
# _DEFAULT_VERSION: "v1.0"
# steps:
# # Step 1: Check out the repository (Cloud Build clones the source automatically)
# - name: gcr.io/cloud-builders/git
# entrypoint: "bash"
# args:
# - "-c"
# - |
# echo "Source code is already cloned by Cloud Build."
# # Step 2: Set up JDK (for Maven)
# - name: gcr.io/cloud-builders/mvn
# args:
# - clean
# - package
# # Step 3: Authenticate to Google Cloud
# - name: gcr.io/cloud-builders/gcloud
# args:
# - auth
# - configure-docker
# - asia-docker.pkg.dev
# # Step 4: Get the latest version and calculate new version
# - name: gcr.io/cloud-builders/gcloud
# id: "get-latest-version"
# entrypoint: "bash"
# args:
# - "-c"
# - |
# _LATEST_VERSION=$(gcloud artifacts docker images list $_REPO \
# --format="value(TAGS)" --sort-by="~CREATE_TIME" | grep -Eo 'v[0-9]+\.[0-9]+' | head -1)
# if [[ -z "$_LATEST_VERSION" ]]; then
# _NEW_VERSION="$_DEFAULT_VERSION"
# else
# VERSION_NUMBER=$(echo $_LATEST_VERSION | cut -d'.' -f2)
# _NEW_VERSION="v1.$((VERSION_NUMBER + 1))"
# fi
# echo "$_NEW_VERSION" > /workspace/new_version
# echo "New version will be: $_NEW_VERSION"
# # Step 5: Tag the previous "latest" image with the new version
# - name: gcr.io/cloud-builders/docker
# id: "tag-previous-latest"
# entrypoint: "bash"
# args:
# - "-c"
# - |
# _NEW_VERSION=$(cat /workspace/new_version)
# docker pull $_REPO:latest || exit 0
# docker tag $_REPO:latest $_REPO:$_NEW_VERSION
# docker push $_REPO:$_NEW_VERSION
# # Step 6: Build the new Docker image
# - name: gcr.io/cloud-builders/docker
# args:
# - build
# - "-t"
# - "$_REPO:latest"
# - "."
# # Step 7: Push the new "latest" Docker image
# - name: gcr.io/cloud-builders/docker
# args:
# - push
# - "$_REPO:latest"
# logsBucket: gs://school-build-logs
# options:
# logging: GCS_ONLY
substitutions:
_PROJECT_ID: "aqueous-cortex-445305-m6" # This can be uppercase or lowercase
_DEFAULT_VERSION: "v1.0"
_REPO: "asia-docker.pkg.dev/$_PROJECT_ID/school/sch"
steps:
# Step 1: Convert $_PROJECT_ID to lowercase and set $_REPO
- name: gcr.io/cloud-builders/git
id: "set-repo"
entrypoint: "bash"
args:
- "-c"
- |
_PROJECT_ID_LOWER=$(echo $_PROJECT_ID | tr '[:upper:]' '[:lower:]')
_REPO="asia-docker.pkg.dev/$_PROJECT_ID_LOWER/school/sch"
echo "$_REPO" > /workspace/repo
echo "Repository set to: $_REPO"
# Step 2: Check out the repository
- name: gcr.io/cloud-builders/git
entrypoint: "bash"
args:
- "-c"
- |
echo "Source code is already cloned by Cloud Build."
# Step 3: Set up JDK (for Maven)
- name: gcr.io/cloud-builders/mvn
args:
- clean
- package
# Step 4: Authenticate to Google Cloud
- name: gcr.io/cloud-builders/gcloud
args:
- auth
- configure-docker
- asia-docker.pkg.dev
# Step 5: Get the latest version and calculate new version
- name: gcr.io/cloud-builders/gcloud
id: "get-latest-version"
entrypoint: "bash"
args:
- "-c"
- |
_REPO=$(cat /workspace/repo)
_LATEST_VERSION=$(gcloud artifacts docker images list $_REPO \
--format="value(TAGS)" --sort-by="~CREATE_TIME" | grep -Eo 'v[0-9]+\.[0-9]+' | head -1)
if [[ -z "$_LATEST_VERSION" ]]; then
echo "No latest version found. Using default version."
_NEW_VERSION="$_DEFAULT_VERSION"
else
VERSION_NUMBER=$(echo $_LATEST_VERSION | cut -d'.' -f2)
_NEW_VERSION="v1.$((VERSION_NUMBER + 1))"
fi
echo "$_NEW_VERSION" > /workspace/new_version
echo "New version will be: $_NEW_VERSION"
# Step 6: Tag the previous "latest" image with the new version
- name: gcr.io/cloud-builders/docker
id: "tag-previous-latest"
entrypoint: "bash"
args:
- "-c"
- |
_REPO=$(cat /workspace/repo)
_NEW_VERSION=$(cat /workspace/new_version)
docker pull $_REPO:latest || exit 0
docker tag $_REPO:latest $_REPO:$_NEW_VERSION
docker push $_REPO:$_NEW_VERSION
# Step 7: Build the new Docker image
- name: gcr.io/cloud-builders/docker
args:
- build
- "-t"
- "$(cat /workspace/repo):latest"
- "."
# Step 8: Push the new "latest" Docker image
- name: gcr.io/cloud-builders/docker
args:
- push
- "$(cat /workspace/repo):latest"
logsBucket: gs://school-build-logs
options:
logging: GCS_ONLY