1
+ name : Build And Push Docker Release Master
2
+
3
+ on :
4
+ workflow_dispatch :
5
+ branches : [ "master" ]
6
+ inputs :
7
+ tag :
8
+ description : tag/version to release
9
+ required : true
10
+ jobs :
11
+ build_push_docker_release_master :
12
+
13
+ runs-on : ubuntu-latest
14
+
15
+ steps :
16
+ - uses : actions/checkout@v3
17
+ name : git checkout master
18
+ with :
19
+ ref : master
20
+ - name : Set up Java
21
+ uses : actions/setup-java@v3
22
+ with :
23
+ java-version : 17
24
+ distribution : temurin
25
+ cache : maven
26
+ - name : Set up QEMU
27
+ uses : docker/setup-qemu-action@v3
28
+ - name : Set up Docker Buildx
29
+ uses : docker/setup-buildx-action@v3
30
+ - name : preliminary checks
31
+ run : |
32
+ docker login --username=${{ secrets.DOCKERHUB_SB_USERNAME }} --password=${{ secrets.DOCKERHUB_SB_PASSWORD }}
33
+ set -e
34
+ # fail if templates/generators contain carriage return '\r'
35
+ /bin/bash ./bin/utils/detect_carriage_return.sh
36
+ # fail if generators contain merge conflicts
37
+ /bin/bash ./bin/utils/detect_merge_conflict.sh
38
+ # fail if generators contain tab '\t'
39
+ /bin/bash ./bin/utils/detect_tab_in_java_class.sh
40
+
41
+ name : setup maven settings.xml
42
+ with :
43
+ servers : |
44
+ [{
45
+ "id": "sonatype-nexus-staging",
46
+ "username": "${{ secrets.OSSRH_USERNAME }}",
47
+ "password": "${{ secrets.OSSRH_TOKEN }}"
48
+ },
49
+ {
50
+ "id": "sonatype-nexus-snapshots",
51
+ "username": "${{ secrets.OSSRH_USERNAME }}",
52
+ "password": "${{ secrets.OSSRH_TOKEN }}"
53
+ }]
54
+ - name : Build with Maven
55
+ run : |
56
+ mvn clean install -U
57
+ - name : Build generator image and push
58
+ uses : docker/build-push-action@v5
59
+ with :
60
+ context : ./modules/swagger-generator
61
+ push : true
62
+ platforms : linux/amd64,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
63
+ provenance : false
64
+ tags : swaggerapi/swagger-generator:${{ env.TAG }},swaggerapi/swagger-generator:latest
65
+ - name : Build CLI image and push
66
+ uses : docker/build-push-action@v5
67
+ with :
68
+ context : ./modules/swagger-codegen-cli
69
+ push : true
70
+ platforms : linux/amd64,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x
71
+ provenance : false
72
+ tags : swaggerapi/swagger-codegen-cli:${{ env.TAG }},swaggerapi/swagger-codegen-cli:latest
73
+ - name : deploy
74
+ run : |
75
+ echo "${{ env.TAG }}"
76
+
77
+ TOKEN="${{ secrets.RANCHER2_BEARER_TOKEN }}"
78
+ RANCHER_HOST="rancher.tools.swagger.io"
79
+ CLUSTER_ID="c-n8zp2"
80
+ NAMESPACE_NAME="swagger-oss"
81
+ K8S_OBJECT_TYPE="daemonsets"
82
+ K8S_OBJECT_NAME="swagger-generator"
83
+ DEPLOY_IMAGE="swaggerapi/swagger-generator:${{ env.TAG }}"
84
+
85
+ workloadStatus=""
86
+ getStatus() {
87
+ echo "Getting update status..."
88
+ if ! workloadStatus="$(curl -s -X GET \
89
+ -H "Authorization: Bearer ${TOKEN}" \
90
+ -H 'Content-Type: application/json' \
91
+ "https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}/status")"
92
+ then
93
+ echo 'ERROR - get status k8s API call failed!'
94
+ echo "Exiting build"...
95
+ exit 1
96
+ fi
97
+ }
98
+
99
+ # $1 = image to deploy
100
+ updateObject() {
101
+ local image="${1}"
102
+ echo "Updating image value..."
103
+
104
+ if ! curl -s -X PATCH \
105
+ -H "Authorization: Bearer ${TOKEN}" \
106
+ -H 'Content-Type: application/json-patch+json' \
107
+ "https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}" \
108
+ -d "[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/image\", \"value\": \"${image}\"}]"
109
+ then
110
+ echo 'ERROR - image update k8s API call failed!'
111
+ echo "Exiting build..."
112
+ exit 1
113
+ fi
114
+ }
115
+
116
+
117
+ # Check that the TAG is valid
118
+ if [[ ${{ env.TAG }} =~ ^[vV]?[0-9]*\.[0-9]*\.[0-9]*$ ]]; then
119
+ echo ""
120
+ echo "This is a Valid TAG..."
121
+
122
+ # Get current image/tag in case we need to rollback
123
+ getStatus
124
+ ROLLBACK_IMAGE="$(echo "${workloadStatus}" | jq -r '.spec.template.spec.containers[0].image')"
125
+ echo ""
126
+ echo "Current image: ${ROLLBACK_IMAGE}"
127
+
128
+ # Update image and validate response
129
+ echo ""
130
+ updateObject "${DEPLOY_IMAGE}"
131
+ echo ""
132
+
133
+ echo ""
134
+ echo "Waiting for pods to start..."
135
+ echo ""
136
+ sleep 60s
137
+
138
+ # Get state of the k8s object. If numberReady == desiredNumberScheduled, consider the upgrade successful. Else raise error
139
+ getStatus
140
+ status="$(echo "${workloadStatus}" | jq '.status')"
141
+ echo ""
142
+ echo "${status}"
143
+ echo ""
144
+
145
+ numberDesired="$(echo "${status}" | jq -r '.desiredNumberScheduled')"
146
+ numberReady="$(echo "${status}" | jq -r '.numberReady')"
147
+
148
+ if (( numberReady == numberDesired )); then
149
+ echo "${K8S_OBJECT_NAME} has been upgraded to ${DEPLOY_IMAGE}"
150
+
151
+ # If pods are not starting, rollback the upgrade and exit the build with error
152
+ else
153
+ echo "state = error...rolling back upgrade"
154
+ updateObject "${ROLLBACK_IMAGE}"
155
+ echo ""
156
+
157
+ echo ""
158
+ echo "Waiting for rollback pods to start..."
159
+ echo ""
160
+ sleep 60s
161
+
162
+ getStatus
163
+ status="$(echo "${workloadStatus}" | jq '.status')"
164
+ echo ""
165
+ echo "${status}"
166
+ echo ""
167
+
168
+ numberDesired="$(echo "${status}" | jq -r '.desiredNumberScheduled')"
169
+ numberReady="$(echo "${status}" | jq -r '.numberReady')"
170
+
171
+ if (( numberReady == numberDesired )); then
172
+ echo "Rollback to ${ROLLBACK_IMAGE} completed."
173
+ else
174
+ echo "FATAL - rollback failed"
175
+ fi
176
+ echo "Exiting Build..."
177
+ exit 1
178
+ fi
179
+
180
+ else
181
+ echo "This TAG is not in a valid format..."
182
+ echo "Exiting Build..."
183
+ exit 0
184
+ fi
185
+ echo "Exiting Build..."
186
+ exit 0
187
+ env :
188
+ TAG : ${{ github.event.inputs.tag }}
0 commit comments