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
0 commit comments