Last active
June 21, 2024 16:23
-
-
Save tstrohmeier/3da60392a5ea2eecbe32895e6624a2b4 to your computer and use it in GitHub Desktop.
AWS ECS: Script for creating a new revision of a task definition and update a service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# enable Docker for your repository | |
options: | |
docker: true | |
pipelines: | |
branches: | |
development: | |
- step: | |
# python image with aws-cli installed | |
image: tstrohmeier/awscli:3.8.3 | |
# the name for this step which will be shown in the pipeline | |
name: Deploy to test | |
# with this new feature you can track your deployments in bitbucket (tab: Deployments) | |
deployment: test | |
# when manuel is set you have to trigger the deployment manually (click 'run' in the pipeline) | |
trigger: manual | |
script: | |
# aws login | |
- eval $(aws ecr get-login --region ${AWS_DEFAULT_REGION} --no-include-email) | |
# set version and build id | |
- export VERSION_NUMBER=1.0.0 | |
- export DEPLOYMENT_ENV=test | |
- export BUILD_ID=$BITBUCKET_BRANCH_$BITBUCKET_COMMIT_$BITBUCKET_BUILD_NUMBER | |
# build docker images | |
- docker build -t ${AWS_REGISTRY_URL}:$BUILD_ID . | |
- docker push ${AWS_REGISTRY_URL}:$BUILD_ID | |
- docker tag ${AWS_REGISTRY_URL}:$BUILD_ID ${AWS_REGISTRY_URL}:${DEPLOYMENT_ENV}_${VERSION_NUMBER} | |
- docker push ${AWS_REGISTRY_URL}:${DEPLOYMENT_ENV}_${VERSION_NUMBER} | |
# Update task definition & service | |
- bash ./deploy-ecs.sh -b mytestapp -s webgui -e ${DEPLOYMENT_ENV} -i ${DEPLOYMENT_ENV}_${VERSION_NUMBER} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# possible -b (base / app name) -i (image version), -e (deploy env) and -s (service id) | |
while getopts b:i:e:s: option | |
do | |
case "${option}" | |
in | |
b) BASE_NAME=${OPTARG};; | |
i) IMG_VERSION=${OPTARG};; | |
e) DEPLOY_ENV=${OPTARG};; | |
s) SERVICE_ID=${OPTARG};; | |
esac | |
done | |
echo "BASE_NAME: " $BASE_NAME | |
echo "IMG_VERSION: " $IMG_VERSION | |
echo "DEPLOY_ENV: " $DEPLOY_ENV | |
echo "SERVICE_ID: " $SERVICE_ID | |
if [ -z "$BASE_NAME" ]; then | |
echo "exit: No BASE_NAME specified" | |
exit; | |
fi | |
if [ -z "$SERVICE_ID" ]; then | |
echo "exit: No SERVICE_ID specified" | |
exit; | |
fi | |
if [ -z "$DEPLOY_ENV" ]; then | |
echo "exit: No DEPLOY_ENV specified" | |
exit; | |
fi | |
if [ -z "$IMG_VERSION" ]; then | |
echo "exit: No IMG_VERSION specified" | |
exit; | |
fi | |
# Define variables | |
TASK_FAMILY=${BASE_NAME}-${DEPLOY_ENV}-${SERVICE_ID} | |
SERVICE_NAME=${BASE_NAME}-${DEPLOY_ENV}-${SERVICE_ID}-service | |
CLUSTER_NAME=${BASE_NAME}-${DEPLOY_ENV}-cluster | |
IMGAGE_PACEHOLDER="<IMGAGE_VERSION>" | |
CONTAINER_DEFINITION_FILE=$(cat ${BASE_NAME}-$SERVICE_ID.container-definition.json) | |
CONTAINER_DEFINITION="${CONTAINER_DEFINITION_FILE//$IMGAGE_PACEHOLDER/$IMG_VERSION}" | |
export TASK_VERSION=$(aws ecs register-task-definition --family ${TASK_FAMILY} --container-definitions "$CONTAINER_DEFINITION" | jq --raw-output '.taskDefinition.revision') | |
echo "Registered ECS Task Definition: " $TASK_VERSION | |
if [ -n "$TASK_VERSION" ]; then | |
echo "Update ECS Cluster: " $CLUSTER_NAME | |
echo "Service: " $SERVICE_NAME | |
echo "Task Definition: " $TASK_FAMILY:$TASK_VERSION | |
DEPLOYED_SERVICE=$(aws ecs update-service --cluster $CLUSTER_NAME --service $SERVICE_NAME --task-definition $TASK_FAMILY:$TASK_VERSION | jq --raw-output '.service.serviceName') | |
echo "Deployment of $DEPLOYED_SERVICE complete" | |
else | |
echo "exit: No task definition" | |
exit; | |
fi | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "mytestapp-webgui", | |
"image": "xxx.dkr.ecr.eu-central-1.amazonaws.com/mytestapp-webgui:<IMGAGE_VERSION>", | |
"cpu": 0, | |
"memory": 500, | |
"memoryReservation": 75, | |
"portMappings": [], | |
"essential": true, | |
"entryPoint": [], | |
"command": [], | |
"environment": [ | |
{ | |
"name": "ENV_DEBUG", | |
"value": "false" | |
} | |
], | |
"mountPoints": [], | |
"volumesFrom": [], | |
"workingDirectory": "/usr/share/nginx/html/", | |
"disableNetworking": false, | |
"privileged": true, | |
"readonlyRootFilesystem": false, | |
"dnsServers": [], | |
"dnsSearchDomains": [], | |
"dockerSecurityOptions": [], | |
"logConfiguration": { | |
"logDriver": "json-file" | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are like me looking for that solution but for github workflow i made this:
https://gist.github.com/mesaque/e6bc0cceac94b6f201d0a7581c269380