-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevops_deploy_app.sh
executable file
·83 lines (69 loc) · 2.64 KB
/
devops_deploy_app.sh
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
#!/bin/bash
# setting required parameters
PROJECT_NAME="$1"
TAG_VERSION="$2"
SERVICE_TYPE="$3"
# manage service
if [ -n "$SERVICE_TYPE" ] && [ "$SERVICE_TYPE" != "" ]
then
if [ "$SERVICE_TYPE" != "LoadBalancer" ] && [ "$SERVICE_TYPE" != "NodePort" ]
then
SERVICE_TYPE="NodePort"
fi
fi
# setting environment color
PROD_ENV_COLOR=$(kubectl get services --field-selector metadata.name="$PROJECT_NAME-service-prod" -o=jsonpath={.items..spec.selector.slot})
STAGE_ENV_COLOR=$(kubectl get services --field-selector metadata.name="$PROJECT_NAME-service-stage" -o=jsonpath={.items..spec.selector.slot})
if [ -z $WORKSPACE ]
then
WORKSPACE="$PWD"
fi
# initialize service
if [ "$PROD_ENV_COLOR" = "" ]
then
kubectl apply -f "$WORKSPACE/kubernetes/services/service-prod.yaml" --dry-run=true -o yaml | sed "s/NodePort/$SERVICE_TYPE/g" | kubectl apply -f -
fi
if [ "$STAGE_ENV_COLOR" = "" ]
then
kubectl apply -f "$WORKSPACE/kubernetes/services/service-stage.yaml" --dry-run=true -o yaml | sed "s/NodePort/$SERVICE_TYPE/g" | kubectl apply -f -
fi
DEPLOYMENT_BLUE=$(kubectl get deployment "$PROJECT_NAME-blue" -o jsonpath='{.metadata.uid}' --ignore-not-found)
DEPLOYMENT_GREEN=$(kubectl get deployment "$PROJECT_NAME-green" -o jsonpath='{.metadata.uid}' --ignore-not-found)
ENV_COLOR=""
# first deployment execution
if [ "$DEPLOYMENT_BLUE" = "" ] && [ "$DEPLOYMENT_GREEN" = "" ]
then
ENV_COLOR="blue"
else
# manage deployment
PROD_DEPLOYMENT_COLOR=$(kubectl get deployment "$PROJECT_NAME-$PROD_ENV_COLOR" -o jsonpath='{.metadata.uid}' --ignore-not-found)
if [ "$PROD_DEPLOYMENT_COLOR" = "" ]
then
# assurance that there is a deployment to production
ENV_COLOR="$PROD_ENV_COLOR"
else
# invert color to stage environment
if [ "$PROD_ENV_COLOR" = "blue" ]
then
ENV_COLOR="green"
else
ENV_COLOR="blue"
fi
fi
fi
echo ""
echo "deploying application"
echo ""
kubectl apply -f "$WORKSPACE/kubernetes/config-maps/$PROJECT_NAME-config-map-$ENV_COLOR.yaml"
kubectl apply -f "$WORKSPACE/kubernetes/deployments/deployment-$ENV_COLOR.yaml" --dry-run=true -o yaml | sed "s/:latest/:$TAG_VERSION/g" | kubectl apply -f -
echo ""
echo "waiting deployment $ENV_COLOR"
READY=$(kubectl get deployment $PROJECT_NAME-$ENV_COLOR -o json | jq -r '.status.conditions[] | select(.reason == "MinimumReplicasAvailable") | .status')
while [[ "$READY" != "True" ]]; do
READY=$(kubectl get deployment $PROJECT_NAME-$ENV_COLOR -o json | jq -r '.status.conditions[] | select(.reason == "MinimumReplicasAvailable") | .status')
sleep 5
done
echo ""
echo "list pods"
echo ""
kubectl get pods