-
-
Save tstrohmeier/3da60392a5ea2eecbe32895e6624a2b4 to your computer and use it in GitHub Desktop.
| # 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} |
| #!/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 | |
| [ | |
| { | |
| "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" | |
| } | |
| } | |
| ] |
export VERSION_NUMBER=1.0.0
Instead of 1.0.0 can we make this one dynamic ? Thanks for you script
export VERSION_NUMBER=1.0.0
Instead of 1.0.0 can we make this one dynamic ? Thanks for you script
Using CodeBuild to build my image, I used CODEBUILD_BUILD_NUMBER as the incremental build number to tag to images. Its a far easier solution and goes a long way to automate.
export VERSION_NUMBER=1.0.0
Instead of 1.0.0 can we make this one dynamic ? Thanks for you scriptUsing CodeBuild to build my image, I used
CODEBUILD_BUILD_NUMBERas the incremental build number to tag to images. Its a far easier solution and goes a long way to automate.
Or, if you'd like to derive it from package.json in the repo, use
export VERSION_NUMBER=$(cat package.json | jq '.version' | sed -e 's/^"//' -e 's/"$//')If you are like me looking for that solution but for github workflow i made this:
https://gist.github.com/mesaque/e6bc0cceac94b6f201d0a7581c269380
https://medium.com/byteschneiderei/how-to-setup-a-continuous-deployment-to-amazon-ecs-with-bitbucket-pipeline-f1931a078f67