Jenkins手動更新AWS 上面的ECS服務

以上是整個部署更新的流程圖:前端

1.開發人員對上線的代碼打一個tag,而後把帶tag的代碼推到AWS codecommit上面。git

git   add  -A   *docker

git   commit   -a -m "${tag}"json

git   tag   "${tag}"api

git  push   origin   分支   ${tag}bash

5.jenkins進行代碼構建做業:

(首先須要安裝插件:Amazon ECR pluginDocker pluginapp

如下是jenkins項目配置的示例:ui

如下是構建image和把image推送到ECR上插件

如下是更新ECS服務的設置(適用於更新接口、對外接口、定時任務、前端、app的H5前端):code

例子:(api的配置)

#!/bin/bash

REGION="ap-northeast-1"    #區域
REPOSITORY_NAME="exchange-api"   #ECR的存儲庫
CLUSTER="online-exchange"    #集羣名稱
SERVICE_NAME="online-exchange-api"    #服務名稱
FAMILY="online_exchange-api"     #任務定義名稱
NAME="online-exchange-api"       #容器名稱
TASKDEFNAME="online_exchange-api"    ##任務定義名稱

#列出存儲庫的URL

REPOSITORY_URI=`aws ecr describe-repositories --repository-names ${REPOSITORY_NAME} --region ${REGION} | jq .repositories[].repositoryUri | tr -d '"'`

#根據構建的tag標籤去建立一個生成任務定義的json文件
sudo sed  -e "s;%TAG%;${tag};g"  -e "s;%REPOSITORY_URI%;${REPOSITORY_URI};g" /opt/update-exchange/update-api/online-ecs-exchange-api.json > /opt/update-exchange/update-api/${NAME}-${tag}.json

#根據ECR裏面的URL,建立一個新的任務定義
aws ecs register-task-definition --family ${FAMILY} --cli-input-json file:///opt/update-exchange/update-api/${NAME}-${tag}.json  --region ${REGION}
SERVICES=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .failures[]`

#獲取最新的版本
REVISION=`aws ecs describe-task-definition --task-definition ${TASKDEFNAME} --region ${REGION} | jq .taskDefinition.revision`

#建立或者更新服務
if [ "$SERVICES" == "" ]; then
  echo "entered existing service"
  DESIRED_COUNT=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .services[].desiredCount`
  if [ ${DESIRED_COUNT} = "0" ]; then
    DESIRED_COUNT="1"
  fi
  aws ecs update-service --cluster ${CLUSTER} --region ${REGION} --service ${SERVICE_NAME} --task-definition ${FAMILY}:${REVISION} 
else
  echo "entered new service"
  aws ecs create-service --service-name ${SERVICE_NAME} --desired-count 2 --task-definition ${FAMILY} --cluster ${CLUSTER} --region ${REGION}
fi

#更新服務成功以後,執行清理鏡像,清理文件。
docker  rmi   786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-api:latest
docker  rmi   786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-api:${tag}
\rm  /opt/update-exchange/update-api/${NAME}-${tag}.json

如下是ECS更新的配置(適用於更新撮合、結算):

例子(結算):

#!/bin/bash

REGION="ap-northeast-1"
REPOSITORY_NAME="exchange-deal"
CLUSTER="online-exchange"
SERVICE_NAME="online-exchange-deal"
FAMILY="online_exchange-deal"
NAME="online-exchange-deal"
TASKDEFNAME="online_exchange-deal"

REPOSITORY_URI=`aws ecr describe-repositories --repository-names ${REPOSITORY_NAME} --region ${REGION} | jq .repositories[].repositoryUri | tr -d '"'`

#Replace the build number and respository URI placeholders with the constants above
sudo sed  -e "s;%TAG%;${tag};g" -e "s;%REPOSITORY_URI%;${REPOSITORY_URI};g" /opt/update-exchange/update-deal/online-ecs-exchange-deal.json > /opt/update-exchange/update-deal/${NAME}-${tag}.json

#Register the task definition in the repository
aws ecs register-task-definition --family ${FAMILY} --cli-input-json file:///opt/update-exchange/update-deal/${NAME}-${tag}.json  --region ${REGION}
SERVICES=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .failures[]`

#Get latest revision
REVISION=`aws ecs describe-task-definition --task-definition ${TASKDEFNAME} --region ${REGION} | jq .taskDefinition.revision`
#首先把預期數改成0,再刪除服務,再建立新的服務。
if [ "$SERVICES" == "" ]; then
    echo "entered existing service"
    aws ecs update-service --cluster  ${CLUSTER}   --service  ${SERVICE_NAME}  --desired-count 0
    sleep 10
    aws ecs delete-service --cluster  ${CLUSTER}   --service ${SERVICE_NAME}
    sleep 100
    aws ecs create-service --cluster ${CLUSTER} --service-name ${SERVICE_NAME} --task-definition ${FAMILY}:${REVISION} --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-04a486cb94c9c9032,subnet-0d93a8c5452781c8f],securityGroups=[sg-0c66800c0bdd235fc],assignPublicIp=ENABLED}"
else
    echo "service does not exist"
    echo "create a new service"
    aws ecs create-service --cluster ${CLUSTER} --service-name ${SERVICE_NAME} --task-definition ${FAMILY}:${REVISION} --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-04a486cb94c9c9032,subnet-0d93a8c5452781c8f],securityGroups=[sg-0c66800c0bdd235fc],assignPublicIp=ENABLED}"
fi

#更新服務成功以後,執行清理鏡像,清理文件。
docker  rmi   786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-deal:latest
docker  rmi   786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-deal:${tag}
\rm  /opt/update-exchange/update-deal/${NAME}-${tag}.json
 

先刪除服務的目的是爲了絕對的保證服務只會存在一個,最後有一個清理鏡像,清理文件,是爲了減小磁盤的壓力。

如下是ECS更新的配置(適用於更新推送):

例子(推送):

#!/bin/bash

REGION="ap-northeast-1"
REPOSITORY_NAME="exchange-ws"
CLUSTER="online-exchange"
SERVICE_NAME="online-exchange-ws"
FAMILY="online_exchange-ws"
NAME="online-exchange-ws"
TASKDEFNAME="online_exchange-ws"

REPOSITORY_URI=`aws ecr describe-repositories --repository-names ${REPOSITORY_NAME} --region ${REGION} | jq .repositories[].repositoryUri | tr -d '"'`

#Replace the build number and respository URI placeholders with the constants above
sudo sed -e "s;%TAG%;${tag};g" -e "s;%REPOSITORY_URI%;${REPOSITORY_URI};g" /opt/update-exchange/update-ws/online-ecs-exchange-ws.json > /opt/update-exchange/update-ws/${NAME}-${tag}.json

#Register the task definition in the repository
aws ecs register-task-definition --family ${FAMILY} --cli-input-json file:///opt/update-exchange/update-ws/${NAME}-${tag}.json  --region ${REGION}
SERVICES=`aws ecs describe-services --services ${SERVICE_NAME} --cluster ${CLUSTER} --region ${REGION} | jq .failures[]`

#Get latest revision
REVISION=`aws ecs describe-task-definition --task-definition ${TASKDEFNAME} --region ${REGION} | jq .taskDefinition.revision`

#首先把預期數改成0,等待服務中沒有運行得任務,再去更新任務。
aws ecs update-service --cluster online-exchange   --service  online-exchange-ws  --desired-count 0

while :
do
    count=`aws ecs describe-services --cluster ${CLUSTER} --service ${SERVICE_NAME} | grep runningCount | tail -n1 | egrep -oE '[0-9]'`
    sleep 30
    if [ $count -eq 0 ];then
       aws ecs update-service --cluster ${CLUSTER} --region ${REGION} --service ${SERVICE_NAME} --task-definition ${FAMILY}:${REVISION} --desired-count 1
       exit
    fi
done
#更新服務成功以後,執行清理鏡像,清理文件。
docker  rmi   786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-ws:latest
docker  rmi   786381498352.dkr.ecr.ap-northeast-1.amazonaws.com/exchange-ws:${tag}
\rm  /opt/update-exchange/update-ws/${NAME}-${tag}.json

相關文章
相關標籤/搜索