@@ -138,6 +138,22 @@ ECS_push_ecr_image() {
138138 docker tag $APP_IMAGE_NAME :$ECS_TAG $AWS_ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$AWS_REPOSITORY :$CIRCLE_BUILD_NUM
139139 ECS_TAG=$CIRCLE_BUILD_NUM
140140 fi
141+
142+ CHECK_ECR_EXIST=" "
143+ CHECK_ECR_EXIST=$( aws ecr describe-repositories --repository-names ${AWS_REPOSITORY} 2>&1 )
144+ if [ $? -ne 0 ]; then
145+ if echo ${CHECK_ECR_EXIST} | grep -q RepositoryNotFoundException; then
146+ echo " repo does not exist and creating repo"
147+ aws ecr create-repository --repository-name $AWS_REPOSITORY
148+ track_error $? " ECS ECR repo creation"
149+ log " Repo created successfully."
150+ else
151+ echo ${CHECK_ECR_EXIST}
152+ fi
153+ else
154+ echo " $AWS_REPOSITORY repository already exist"
155+ fi
156+
141157 log " Pushing Docker Image..."
142158 eval $( aws ecr get-login --region $AWS_REGION --no-include-email)
143159 docker push $AWS_ACCOUNT_ID .dkr.ecr.$AWS_REGION .amazonaws.com/$AWS_REPOSITORY :$ECS_TAG
460476ECS_deploy_cluster () {
461477
462478 AWS_ECS_SERVICE=$1
463- update_result=$( aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $REVISION )
464- result=$( echo $update_result | $JQ ' .service.taskDefinition' )
465- log $result
466- if [[ $result != $REVISION ]]; then
467- # echo "Error updating service."
468- track_error 1 " ECS updating service."
469- return 1
479+ # checking cluster exist
480+ CHECK_CLUSTER_EXIST=" "
481+ CHECK_CLUSTER_EXIST=$( aws ecs describe-clusters --cluster $AWS_ECS_CLUSTER | jq --raw-output ' select(.clusters[].clusterName != null ) | .clusters[].clusterName' )
482+ if [ -z $CHECK_CLUSTER_EXIST ];
483+ then
484+ echo " $AWS_ECS_CLUSTER cluster does not exist. Kindly check with admin team"
485+ exit 1
486+ else
487+ echo " $AWS_ECS_CLUSTER Cluster exist"
488+ fi
489+ # checking service exist
490+ CHECK_SERVICE_EXIST=" "
491+ CHECK_SERVICE_EXIST=$( aws ecs describe-services --service $AWS_ECS_SERVICE --cluster $AWS_ECS_CLUSTER | jq --raw-output ' select(.services[].status != null ) | .services[].status' )
492+ if [ -z $CHECK_SERVICE_EXIST ];
493+ then
494+ if [ " $ECS_TEMPLATE_TYPE " == " FARGATE" ];
495+ then
496+ echo " Fargate Service does not exist. Kindly check with admin team"
497+ exit 1
498+ else
499+ echo " service does not exist. Creating service"
500+ aws ecs create-service --cluster $AWS_ECS_CLUSTER --service-name $AWS_ECS_SERVICE --task-definition $REVISION --desired-count 1
501+ echo " Kindly work with admin team for routing"
502+ fi
503+ else
504+ echo " service exist.Application updates the service"
505+ update_result=$( aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $REVISION )
506+ result=$( echo $update_result | $JQ ' .service.taskDefinition' )
507+ log $result
508+ if [[ $result != $REVISION ]]; then
509+ # echo "Error updating service."
510+ track_error 1 " ECS updating service."
511+ return 1
512+ fi
513+
514+ echo " Update service intialised successfully for deployment"
470515 fi
471516
472- echo " Update service intialised successfully for deployment"
473517 return 0
474518}
475519
@@ -1042,7 +1086,7 @@ then
10421086 echo " ${# AWS_ECS_SERVICES[@]} service are going to be updated"
10431087 for AWS_ECS_SERVICE_NAME in " ${AWS_ECS_SERVICES[@]} "
10441088 do
1045- echo " updating ECS Cluster Service - $AWS_ECS_SERVICE_NAME "
1089+ echo " creating/ updating ECS Cluster Service - $AWS_ECS_SERVICE_NAME "
10461090 ECS_deploy_cluster " $AWS_ECS_SERVICE_NAME "
10471091 check_service_status " $AWS_ECS_SERVICE_NAME "
10481092 # echo $REVISION
0 commit comments