Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f23da71

Browse files
authored and
committed
Update
1 parent 458b143 commit f23da71

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

deploy.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
#Constants
3+
4+
REGION="cn-north-1"
5+
TG_ARN="arn:aws-cn:elasticloadbalancing:cn-north-1:761602622223:targetgroup/ecslabphp-tg/cda96cf0d98bdcc1"
6+
REPOSITORY_NAME="ecs-lab-php"
7+
CLUSTER="ecs-guolianc"
8+
FAMILY=`sed -n 's/.*"family": "\(.*\)",/\1/p' taskdef.json`
9+
FAMILY=${FAMILY:0:10}
10+
NAME=`sed -n 's/.*"name": "\(.*\)",/\1/p' taskdef.json`
11+
NAME=${NAME:0:10}
12+
SERVICE_NAME=${NAME}-service
13+
14+
#Store the repositoryUri as a variable
15+
REPOSITORY_URI=`aws ecr describe-repositories --repository-names ${REPOSITORY_NAME} --region ${REGION} | jq .repositories[].repositoryUri | tr -d '"'`
16+
17+
18+
#Replace the build number and respository URI placeholders with the constants above
19+
sed -e "s;%BUILD_NUMBER%;${BUILD_NUMBER};g" -e "s;%REPOSITORY_URI%;${REPOSITORY_URI};g" taskdef.json > ${NAME}-v_${BUILD_NUMBER}.json
20+
21+
#Register the task definition in the repository
22+
aws ecs register-task-definition --family ${FAMILY} --cli-input-json file://${WORKSPACE}/${NAME}-v_${BUILD_NUMBER}.json --region ${REGION}
23+
24+
SERVICES=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .failures[]`
25+
#Get latest revision
26+
REVISION=`aws ecs describe-task-definition --task-definition ${NAME} --region ${REGION} | jq .taskDefinition.revision`
27+
28+
echo $SERVICE
29+
echo $REVISION
30+
31+
32+
#Create or update service
33+
if [ "$SERVICES" == "" ]; then
34+
echo "entered existing service"
35+
DESIRED_COUNT=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .services[].desiredCount`
36+
if [ ${DESIRED_COUNT} = "0" ]; then
37+
DESIRED_COUNT="1"
38+
fi
39+
sed -e "s;%CLUSTER%;${CLUSTER};g" -e "s;%SERVICE_NAME%;${SERVICE_NAME};g" -e "s;%FAMILY%;${FAMILY};g" -e "s;%REVISION%;${REVISION};g" updateservicedef.json > update-${SERVICE_NAME}-v_${BUILD_NUMBER}.json
40+
aws ecs update-service --cli-input-json file://${WORKSPACE}/update-${SERVICE_NAME}-v_${BUILD_NUMBER}.json --region ${REGION}
41+
else
42+
echo "entered new service"
43+
sed -e "s;%CLUSTER%;${CLUSTER};g" -e "s;%SERVICE_NAME%;${SERVICE_NAME};g" -e "s;%TG_ARN%;${TG_ARN};g" -e "s;%FAMILY%;${FAMILY};g" servicedef.json > ${SERVICE_NAME}-v_${BUILD_NUMBER}.json
44+
aws ecs create-service --cli-input-json file://${WORKSPACE}/${SERVICE_NAME}-v_${BUILD_NUMBER}.json --region ${REGION}
45+
fi

servicedef.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
{
2-
"serviceName": "ecslabphpsvc",
3-
"taskDefinition": "ecslabphp",
4-
"loadBalancers": [
5-
{
6-
"loadBalancerName": "EC2Contai-EcsElast-S06278JGSJCM",
7-
"containerName": "ecslabphp",
8-
"containerPort": 80
9-
}
10-
],
2+
"cluster": "%CLUSTER%",
3+
"serviceName": "%SERVICE_NAME%",
4+
"taskDefinition": "%FAMILY%",
5+
"loadBalancers": [{
6+
"targetGroupArn": "%TG_ARN%",
7+
"containerName": "ecslabphp",
8+
"containerPort": 80
9+
}],
10+
"deploymentConfiguration": {
11+
"maximumPercent": 100,
12+
"minimumHealthyPercent": 200
13+
},
14+
"healthCheckGracePeriodSeconds": 0,
1115
"desiredCount": 10,
1216
"role": "ecsServiceRole"
1317
}

updateservicedef.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"cluster": "%CLUSTER%",
3+
"service": "%SERVICE_NAME%",
4+
"taskDefinition": "%FAMILY%:%REVISION%",
5+
"deploymentConfiguration": {
6+
"maximumPercent": 200,
7+
"minimumHealthyPercent": 100
8+
},
9+
"healthCheckGracePeriodSeconds": 0,
10+
"desiredCount": 3,
11+
"forceNewDeployment": true
12+
}

0 commit comments

Comments
 (0)