Skip to content

Commit a41f458

Browse files
committed
update docker build/push/deploy action
1 parent 4304762 commit a41f458

File tree

2 files changed

+236
-2
lines changed

2 files changed

+236
-2
lines changed

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

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,129 @@ jobs:
6767
docker push $DOCKER_CODEGEN_CLI_IMAGE_NAME:latest
6868
docker push $DOCKER_GENERATOR_FULL_IMAGE_NAME:${{ env.TAG }}
6969
docker push $DOCKER_GENERATOR_FULL_IMAGE_NAME:latest
70+
echo "docker images:"
71+
docker images | grep -i generator
72+
echo "pushing $DOCKER_GENERATOR_ROOT_FULL_IMAGE_NAME:${{ env.TAG }}"
7073
docker push $DOCKER_GENERATOR_ROOT_FULL_IMAGE_NAME:${{ env.TAG }}
74+
echo "pushing $DOCKER_GENERATOR_ROOT_FULL_IMAGE_NAME:latest"
7175
docker push $DOCKER_GENERATOR_ROOT_FULL_IMAGE_NAME:latest
76+
echo "tagging $DOCKER_GENERATOR_FULL_IMAGE_NAME:${{ env.TAG }}-root"
7277
docker tag $DOCKER_GENERATOR_ROOT_FULL_IMAGE_NAME:${{ env.TAG }} $DOCKER_GENERATOR_FULL_IMAGE_NAME:${{ env.TAG }}-root
73-
docker push $DOCKER_GENERATOR_FULL_IMAGE_NAME:$${{ env.TAG }}-root
78+
echo "pushing $DOCKER_GENERATOR_FULL_IMAGE_NAME:${{ env.TAG }}-root"
79+
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
74194
env:
75-
TAG: ${{ github.event.inputs.tag }}
195+
TAG: ${{ github.event.inputs.tag }}

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

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

0 commit comments

Comments
 (0)