Pages

Thursday, July 23, 2020

AWS: Upload Docker Image to ECR Repo

To upload the Docker Image to ECR Policy:
ENV:
projectName=sismanager
region=us-east-1
serviceName=application-management
awsAccount=arn:aws:ecs:us-east-1:396773889961
clusterName=sismanager-cluster-preeval
environment=preeval
awsServiceName=${projectName}-${serviceName}-service-preeval
awsTaskName=${projectName}-${serviceName}-task-preeval
latestTag=preevallatest

Shell-1:
#!/bin/bash
MANIFEST=$(aws ecr batch-get-image --repository-name ${environment}/${projectName}/${serviceName} --image-ids imageTag=${release_version} --query images[].imageManifest --output text --region ${region})
aws ecr put-image --repository-name  ${environment}/${projectName}/${serviceName} --image-tag ${latestTag} --image-manifest "$MANIFEST" --region ${region} || error=true
if [ $error ]
then
    echo "[OUTPUT] - ${latestTag} is already applied to the image"
else
    echo "[OUTPUT] - ${latestTag} is applied to the image"
fi
   
aws ecr describe-images --repository-name ${environment}/${projectName}/${serviceName} --region ${region}

Shell-2: - update
#!/bin/bash
#check whether task exists
#if task not exists, deploy new code and ensure new code is running
#if task exits, get the current task name, deploy new code and ensure new code is running and kill the old task

startTask ()
{
    echo "Starting Tasks"
    taskRevision=`aws ecs describe-task-definition --task-definition ${awsTaskName} --region ${region} | jq .taskDefinition.revision `
    if [ $? -eq '0' ]
    then
        echo "[OUTPUT] - Revision is ${taskRevision} "
echo ${taskRevision}
aws ecs update-service --cluster ${clusterName} --region ${region} --service ${awsServiceName} --task-definition ${awsTaskName}:${taskRevision} --desired-count 1 --force-new-deployment
    else
    echo "[OUTPUT] - Task revision not found. So unable to start the task"
        exit 1
fi
}

stopTask ()
{
    aws ecs stop-task --cluster ${clusterName} --task ${trimGetTask}
    if [ $? -eq '0' ]
    then
        echo "[OUTPUT] - Stop task command executed successfully"
        checkTask=`aws ecs describe-tasks --cluster ${clusterName} --tasks ${trimGetTask} |jq '.tasks[].desiredStatus'`
        echo "[OUTPUT] - Task status is ${checkTask}"
        trimCheckTask=`echo $checkTask | tr -d \"`
        echo "[OUTPUT] - After slashing task name is ${trimCheckTask}"
        if [ "$trimCheckTask" == "STOPPED" ]
        then
            echo "[OUTPUT] - Task has been successfully stopped"
            exit 0
        else
            echo "[ERROR] - Issue while stopping task"
            exit 1
        fi
    else
        echo "[ERROR] - Stop task command didnt executed successfully."
        exit 1
    fi
}


echo "[OUTPUT] - Checking if any tasks exists already"
taskCount=`aws ecs list-tasks --cluster ${clusterName} --service-name ${awsAccount}:service/${awsServiceName} |jq length`
echo "[OUTPUT] - Task count is $taskCount"
if [ "$taskCount" -eq '1' ]
then
    echo "[OUTPUT] - Task is running"
    getTask=`aws ecs list-tasks --cluster ${clusterName} --service-name ${awsAccount}:service/${awsServiceName} |jq '.taskArns[0]'`
    echo "[OUTPUT] - Task name is ${getTask}"
    trimGetTask=`echo $getTask | tr -d \"`
    echo "[OUTPUT] - After slashing task name is ${trimGetTask}"
    startTask
    #stopTask
else
    echo "[OUTPUT] - No task is running. So starting task"
    startTask
fi



No comments:

Post a Comment