Last active
June 4, 2025 07:00
-
-
Save hrishix6/44b09e88f5609a2ae4b03a93541a8640 to your computer and use it in GitHub Desktop.
Node v22.x.x App Bitbucket Pipeline For Fargate Deployment, Includes running E2E tests with TestContainers, Upload Image to ECR, Register New Task Definition, Update Fargate Service
This file contains hidden or 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
| definitions: | |
| services: | |
| docker: | |
| memory: 5120 | |
| # Assumes Following Repository variables are present - | |
| # AWS_ECR_REPO_PROD - your ECR repository URL | |
| # AWS_DEFAULT_REGION - aws account region | |
| # AWS_SERVICE_NAME_PROD - ECS service name | |
| # AWS_CLUSTER_NAME_PROD - ECS cluster name | |
| # AWS_ACCESS_KEY_ID - access key id of user having proper permissions for ECS,ECR,IAM | |
| # AWS_SECRET_ACCESS_KEY - secret access key | |
| # Assumes task definition template is present under .aws/prod.task.definition.json with "containerDefinitions.image: "$IMAGE_NAME" | |
| pipelines: | |
| branches: | |
| <your-branch-name>: | |
| - step: | |
| name: Run Unit and E2E Tests | |
| image: node:22.15.1-alpine3.21 | |
| script: | |
| - export TESTCONTAINERS_RYUK_DISABLED=true | |
| - export TESTCONTAINERS_CHECKS_DISABLE=true | |
| - npm install -g pnpm@latest-10 | |
| - pnpm install --frozen-lockfile | |
| - pnpm run test | |
| - apk update && apk upgrade | |
| - apk add jq | |
| - touch ./image_version.txt | |
| - export IMAGE_VERSION=$(jq -r .version package.json) | |
| - echo $IMAGE_VERSION >> image_version.txt # extracts version from package.json to be used in next step | |
| artifacts: | |
| - image_version.txt | |
| - step: | |
| name: Build & Upload Image | |
| services: | |
| - docker | |
| size: 2x #allocates 2x memory, maybe required for large apps | |
| image: atlassian/pipelines-awscli | |
| script: | |
| - export IMAGE_VERSION=$(cat ./image_version.txt) | |
| - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ECR_REPO_PROD | |
| - docker build -t app:$IMAGE_VERSION . | |
| - docker tag app:$IMAGE_VERSION $AWS_ECR_REPO_PROD:$IMAGE_VERSION | |
| - docker push $AWS_ECR_REPO_PROD:$IMAGE_VERSION | |
| - export IMAGE_NAME=$AWS_ECR_REPO_PROD:$IMAGE_VERSION | |
| - echo $IMAGE_NAME >> image_name.txt #save image name to use in next step for task registration | |
| artifacts: | |
| - image_name.txt | |
| - step: | |
| name: Register Task definition | |
| script: | |
| - apt-get update >> /dev/null && apt-get install -y unzip curl gettext >> /dev/null | |
| - curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
| - unzip awscliv2.zip >> /dev/null | |
| - ./aws/install | |
| - aws --version | |
| - export IMAGE_NAME=$(cat ./image_name.txt) | |
| - envsubst < .aws/prod.task.definition.json > updated.prod.json #replace "$IMAGE_NAME" in prod.task.definition.json | |
| - export UPDATED_TASK_DEFINITION=$(aws ecs register-task-definition --region $AWS_DEFAULT_REGION --cli-input-json file://updated.prod.json --output text --query 'taskDefinition.taskDefinitionArn') | |
| - echo $UPDATED_TASK_DEFINITION >> task_arn.txt # save newly registered task definition ARN for next step. | |
| artifacts: | |
| - task_arn.txt | |
| - step: | |
| name: Update ECS service | |
| image: atlassian/pipelines-awscli | |
| trigger: manual | |
| script: | |
| - export UPDATED_TASK_DEFINITION=$(cat ./task_arn.txt) | |
| - echo $UPDATED_TASK_DEFINITION | |
| - aws ecs update-service --service $AWS_SERVICE_NAME_PROD --cluster $AWS_CLUSTER_NAME_PROD --task-definition $UPDATED_TASK_DEFINITION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment