@@ -2,133 +2,45 @@ name: Deploy Docker
22
33on :
44 workflow_dispatch :
5- branches : ["master"]
65 inputs :
76 tag :
87 description : tag/version to deploy
98 required : true
9+
1010jobs :
1111 deploy :
12-
1312 runs-on : ubuntu-latest
1413
14+ permissions :
15+ id-token : write
16+ contents : read
17+
1518 steps :
16- - name : deploy docker
17- run : |
18- SC_RELEASE_TAG="v${{ env.TAG }}"
19- echo "$SC_RELEASE_TAG"
20-
21- TOKEN="${{ secrets.RANCHER2_BEARER_TOKEN }}"
22- RANCHER_HOST="rancher.tools.swagger.io"
23- CLUSTER_ID="c-n8zp2"
24- NAMESPACE_NAME="swagger-oss"
25- K8S_OBJECT_TYPE="daemonsets"
26- K8S_OBJECT_NAME="swagger-validator-v2"
27- DEPLOY_IMAGE="swaggerapi/swagger-validator-v2:$SC_RELEASE_TAG"
28-
29- workloadStatus=""
30- getStatus() {
31- echo "Getting update status..."
32- if ! workloadStatus="$(curl -s -X GET \
33- -H "Authorization: Bearer ${TOKEN}" \
34- -H 'Content-Type: application/json' \
35- "https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}/status")"
36- then
37- echo 'ERROR - get status k8s API call failed!'
38- echo "Exiting build"...
39- exit 1
40- fi
41- }
42-
43- # $1 = image to deploy
44- updateObject() {
45- local image="${1}"
46- echo "Updating image value..."
47-
48- if ! curl -s -X PATCH \
49- -H "Authorization: Bearer ${TOKEN}" \
50- -H 'Content-Type: application/json-patch+json' \
51- "https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}" \
52- -d "[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/image\", \"value\": \"${image}\"}]"
53- then
54- echo 'ERROR - image update k8s API call failed!'
55- echo "Exiting build..."
56- exit 1
57- fi
58- }
59-
60-
61- # Check that the TAG is valid
62- if [[ $SC_RELEASE_TAG =~ ^[vV]?[0-9]*\.[0-9]*\.[0-9]*$ ]]; then
63- echo ""
64- echo "This is a Valid TAG..."
65-
66- # Get current image/tag in case we need to rollback
67- getStatus
68- ROLLBACK_IMAGE="$(echo "${workloadStatus}" | jq -r '.spec.template.spec.containers[0].image')"
69- echo ""
70- echo "Current image: ${ROLLBACK_IMAGE}"
71-
72- # Update image and validate response
73- echo ""
74- updateObject "${DEPLOY_IMAGE}"
75- echo ""
76-
77- echo ""
78- echo "Waiting for pods to start..."
79- echo ""
80- sleep 60s
81-
82- # Get state of the k8s object. If numberReady == desiredNumberScheduled, consider the upgrade successful. Else raise error
83- getStatus
84- status="$(echo "${workloadStatus}" | jq '.status')"
85- echo ""
86- echo "${status}"
87- echo ""
88-
89- numberDesired="$(echo "${status}" | jq -r '.desiredNumberScheduled')"
90- numberReady="$(echo "${status}" | jq -r '.numberReady')"
91-
92- if (( numberReady == numberDesired )); then
93- echo "${K8S_OBJECT_NAME} has been upgraded to ${DEPLOY_IMAGE}"
94-
95- # If pods are not starting, rollback the upgrade and exit the build with error
96- else
97- echo "state = error...rolling back upgrade"
98- updateObject "${ROLLBACK_IMAGE}"
99- echo ""
100-
101- echo ""
102- echo "Waiting for rollback pods to start..."
103- echo ""
104- sleep 60s
105-
106- getStatus
107- status="$(echo "${workloadStatus}" | jq '.status')"
108- echo ""
109- echo "${status}"
110- echo ""
111-
112- numberDesired="$(echo "${status}" | jq -r '.desiredNumberScheduled')"
113- numberReady="$(echo "${status}" | jq -r '.numberReady')"
114-
115- if (( numberReady == numberDesired )); then
116- echo "Rollback to ${ROLLBACK_IMAGE} completed."
117- else
118- echo "FATAL - rollback failed"
119- fi
120- echo "Exiting Build..."
121- exit 1
122- fi
123-
124- else
125- echo "This TAG is not in a valid format..."
126- echo "Exiting Build..."
127- exit 0
128- fi
129- echo "Exiting Build..."
130- exit 0
131- env :
132- ACTIONS_ALLOW_UNSECURE_COMMANDS : true
133- GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
134- TAG : ${{ github.event.inputs.tag }}
19+ - name : Checkout
20+ uses : actions/checkout@v4
21+
22+ - name : Configure AWS Credentials
23+ uses : aws-actions/configure-aws-credentials@v4
24+ with :
25+ audience : sts.amazonaws.com
26+ aws-region : us-east-1
27+ role-to-assume : arn:aws:iam::886148526908:role/CloudformationBuild
28+
29+ - name : Update kubeconfig
30+ run : |
31+ aws eks update-kubeconfig \
32+ --name eks-prod-swagger-oss-cluster-tf \
33+ --region us-east-1 \
34+ --role-arn arn:aws:iam::886148526908:role/CloudformationBuild
35+
36+ - name : Deploy to Kubernetes
37+ run : |
38+ IMAGE="swaggerapi/swagger-validator-v2:v${{ github.event.inputs.tag }}"
39+
40+ echo "Deploying image: $IMAGE"
41+
42+ kubectl set image daemonset/swagger-validator-v2 \
43+ swagger-validator-v2=$IMAGE \
44+ -n swagger-oss
45+
46+ kubectl rollout status daemonset/swagger-validator-v2 -n swagger-oss
0 commit comments