Skip to content

Instantly share code, notes, and snippets.

@sabriayes
Last active June 16, 2023 11:53
Show Gist options
  • Save sabriayes/5f1d4b9dd73f789d251fafe6ce2a3c26 to your computer and use it in GitHub Desktop.
Save sabriayes/5f1d4b9dd73f789d251fafe6ce2a3c26 to your computer and use it in GitHub Desktop.
AWS ECR/ECS CI/CD pipeline for Gitlab and Bitbucket
node_modules
npm-debug.log
package-lock.json
yarn.lock
.env
image: docker:latest
services:
- docker:dind
stages:
- build
- deploy
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
npm_config_cache: "$CI_PROJECT_DIR/.npm"
before_script:
- apk add --no-cache py-pip
- pip install awscli
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_DEFAULT_REGION
.build-scripts: &build-scripts
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $ECR_REPOSITORY_URL
- docker build -t $CI_PROJECT_NAME-$CI_ENVIRONMENT_SLUG .
- echo "🐳 Dockerize"
- echo $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:$CI_COMMIT_SHORT_SHA
- docker tag $CI_PROJECT_NAME-$CI_ENVIRONMENT_SLUG:latest $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:$CI_COMMIT_SHORT_SHA
- docker tag $CI_PROJECT_NAME-$CI_ENVIRONMENT_SLUG:latest $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:latest
- docker push $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:$CI_COMMIT_SHORT_SHA
- echo "🚀 Push to ECR"
- docker push $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:latest
.deploy-scripts: &deploy-scripts
- pip install ecs-deploy
- echo "😽 Deploy to ECS"
- ecs deploy $ECS_CLUSTER_NAME $ECS_SERVICE_NAME --tag $CI_COMMIT_SHORT_SHA --timeout -1
development-build:
stage: build
environment:
name: development
script:
- *build-scripts
only:
- development
development-deploy:
stage: deploy
environment:
name: development
script:
- *deploy-scripts
only:
- development
production-build:
stage: build
environment:
name: production
script:
- *build-scripts
only:
- tags
# tags:
# - YOUR RUNNER TAG -> Optional: It will only work on the corresponding runner!
production-deploy:
stage: deploy
environment:
name: production
script:
- *deploy-scripts
only:
- tag
# tags:
# - YOUR RUNNER TAG
image: python:3.7.4-alpine3.10
definitions:
services:
docker:
memory: 2048
steps:
- step: &dockerize
name: Dockerize & Push
script:
- IMAGE_TAG="${BITBUCKET_COMMIT::7}"
- pip install awscli
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_DEFAULT_REGION
- echo "🐳 Dockerize"
- echo $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:$IMAGE_TAG
- aws --version
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $ECR_REPOSITORY_URL
- docker build -t $BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH .
- docker tag $BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH:latest $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:$IMAGE_TAG
- docker tag $BITBUCKET_REPO_SLUG-$BITBUCKET_BRANCH:latest $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:latest
- echo "🚀 Push to ECR"
- docker push $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:$IMAGE_TAG
- docker push $ECR_REPOSITORY_URL/$ECR_REPOSITORY_NAME:latest
services:
- docker
caches:
- pip
- step: &deploy
name: Deploy
script:
- IMAGE_TAG="${BITBUCKET_COMMIT::7}"
- pip install awscli
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_DEFAULT_REGION
- pip install ecs-deploy
- echo "😽 Deploy to ECS"
- ecs deploy $ECS_CLUSTER_NAME $ECS_SERVICE_NAME --tag $IMAGE_TAG --timeout -1
pipelines:
branches:
development:
- stage:
name: Dockerize - Push - Deploy (Development)
deployment: development
steps:
- step: *dockerize
- step: *deploy
tags:
release-*:
- stage:
name: Dockerize - Push - Deploy (Production)
deployment: production
steps:
- step: *dockerize
- step: *deploy
FROM node:16
WORKDIR /usr/src/app
EXPOSE 80 3000
COPY package*.json ./
RUN yarn install
COPY . .
RUN yarn build
ENTRYPOINT ["node", "dist/main"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment