Skip to content

Commit 659ee70

Browse files
committed
docker release GH action - deploy
1 parent 4d2c3b9 commit 659ee70

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

.github/workflows/docker-release-3.0.yml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,119 @@ jobs:
7777
docker tag $DOCKER_GENERATOR_ROOT_FULL_IMAGE_NAME:${{ env.TAG }} $DOCKER_GENERATOR_FULL_IMAGE_NAME:${{ env.TAG }}-root
7878
echo "pushing $DOCKER_GENERATOR_FULL_IMAGE_NAME:${{ env.TAG }}-root"
7979
docker -D -l debug push $DOCKER_GENERATOR_FULL_IMAGE_NAME:${{ env.TAG }}-root
80+
- name: deploy
81+
run: |
82+
echo "${{ env.TAG }}"
83+
84+
TOKEN="${{ secrets.RANCHER2_BEARER_TOKEN }}"
85+
RANCHER_HOST="rancher.tools.swagger.io"
86+
CLUSTER_ID="c-n8zp2"
87+
NAMESPACE_NAME="swagger-oss"
88+
K8S_OBJECT_TYPE="daemonsets"
89+
K8S_OBJECT_NAME="swagger-generator-v3"
90+
DEPLOY_IMAGE="swaggerapi/swagger-generator-v3:${{ env.TAG }}"
91+
92+
workloadStatus=""
93+
getStatus() {
94+
echo "Getting update status..."
95+
if ! workloadStatus="$(curl -s -X GET \
96+
-H "Authorization: Bearer ${TOKEN}" \
97+
-H 'Content-Type: application/json' \
98+
"https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}/status")"
99+
then
100+
echo 'ERROR - get status k8s API call failed!'
101+
echo "Exiting build"...
102+
exit 1
103+
fi
104+
}
105+
106+
# $1 = image to deploy
107+
updateObject() {
108+
local image="${1}"
109+
echo "Updating image value..."
110+
111+
if ! curl -s -X PATCH \
112+
-H "Authorization: Bearer ${TOKEN}" \
113+
-H 'Content-Type: application/json-patch+json' \
114+
"https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}" \
115+
-d "[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/image\", \"value\": \"${image}\"}]"
116+
then
117+
echo 'ERROR - image update k8s API call failed!'
118+
echo "Exiting build..."
119+
exit 1
120+
fi
121+
}
122+
123+
124+
# Check that the TAG is valid
125+
if [[ ${{ env.TAG }} =~ ^[vV]?[0-9]*\.[0-9]*\.[0-9]*$ ]]; then
126+
echo ""
127+
echo "This is a Valid TAG..."
128+
129+
# Get current image/tag in case we need to rollback
130+
getStatus
131+
ROLLBACK_IMAGE="$(echo "${workloadStatus}" | jq -r '.spec.template.spec.containers[0].image')"
132+
echo ""
133+
echo "Current image: ${ROLLBACK_IMAGE}"
134+
135+
# Update image and validate response
136+
echo ""
137+
updateObject "${DEPLOY_IMAGE}"
138+
echo ""
139+
140+
echo ""
141+
echo "Waiting for pods to start..."
142+
echo ""
143+
sleep 60s
144+
145+
# Get state of the k8s object. If numberReady == desiredNumberScheduled, consider the upgrade successful. Else raise error
146+
getStatus
147+
status="$(echo "${workloadStatus}" | jq '.status')"
148+
echo ""
149+
echo "${status}"
150+
echo ""
151+
152+
numberDesired="$(echo "${status}" | jq -r '.desiredNumberScheduled')"
153+
numberReady="$(echo "${status}" | jq -r '.numberReady')"
154+
155+
if (( numberReady == numberDesired )); then
156+
echo "${K8S_OBJECT_NAME} has been upgraded to ${DEPLOY_IMAGE}"
157+
158+
# If pods are not starting, rollback the upgrade and exit the build with error
159+
else
160+
echo "state = error...rolling back upgrade"
161+
updateObject "${ROLLBACK_IMAGE}"
162+
echo ""
163+
164+
echo ""
165+
echo "Waiting for rollback pods to start..."
166+
echo ""
167+
sleep 60s
168+
169+
getStatus
170+
status="$(echo "${workloadStatus}" | jq '.status')"
171+
echo ""
172+
echo "${status}"
173+
echo ""
174+
175+
numberDesired="$(echo "${status}" | jq -r '.desiredNumberScheduled')"
176+
numberReady="$(echo "${status}" | jq -r '.numberReady')"
177+
178+
if (( numberReady == numberDesired )); then
179+
echo "Rollback to ${ROLLBACK_IMAGE} completed."
180+
else
181+
echo "FATAL - rollback failed"
182+
fi
183+
echo "Exiting Build..."
184+
exit 1
185+
fi
186+
187+
else
188+
echo "This TAG is not in a valid format..."
189+
echo "Exiting Build..."
190+
exit 0
191+
fi
192+
echo "Exiting Build..."
193+
exit 0
80194
env:
81195
TAG: ${{ github.event.inputs.tag }}

0 commit comments

Comments
 (0)