diff --git a/.aws/task-definition.json b/.aws/task-definition.json new file mode 100644 index 0000000..4fbe5c6 --- /dev/null +++ b/.aws/task-definition.json @@ -0,0 +1,82 @@ +{ + + "IMPORTANT_NOTE": "THIS IS A DUMMY FILE, THIS WILL BE UPDATED", + "taskDefinitionArn": "arn:aws:ecs:ap-south-1*", + "containerDefinitions": [ + { + "name": "website-*", + "image": "", + "cpu": 0, + "portMappings": [ + { + "name": "website-backend", + "containerPort": 3000, + "hostPort": 3000, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [], + "environmentFiles": [], + "mountPoints": [], + "volumesFrom": [], + "ulimits": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-create-group": "true", + "awslogs-group": "/ecs/group", + "awslogs-region": "ap-south-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + } + } + ], + "family": "rds-backend", + "executionRoleArn": "arn:aws:iam::role/ecsTaskExecutionRole", + "networkMode": "default", + "revision": 2, + "volumes": [], + "status": "ACTIVE", + "requiresAttributes": [ + { + "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" + }, + { + "name": "ecs.capability.execution-role-awslogs" + }, + { + "name": "com.amazonaws.ecs.capability.ecr-auth" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" + }, + { + "name": "ecs.capability.execution-role-ecr-pull" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" + }, + { + "name": "ecs.capability.task-eni" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" + } + ], + "placementConstraints": [], + "compatibilities": ["EC2"], + "requiresCompatibilities": ["EC2"], + "cpu": "512", + "memory": "512", + "runtimePlatform": { + "cpuArchitecture": "X86_64", + "operatingSystemFamily": "LINUX" + }, + "registeredAt": "2023-12-13T07:47:44.044Z", + "registeredBy": "arn:aws:iam", + "tags": [] + } + \ No newline at end of file diff --git a/.github/workflows/aws-deployment.yml b/.github/workflows/aws-deployment.yml new file mode 100644 index 0000000..7f695dd --- /dev/null +++ b/.github/workflows/aws-deployment.yml @@ -0,0 +1,71 @@ +name: Deploy to Amazon ECS + +on: + push: + branches: + - "main" + - "develop" + +env: + AWS_REGION: ${{ vars.AWS_REGION }} # set this to your preferred AWS region, e.g. us-west-1 + ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} # set this to your Amazon ECR repository name + ECS_SERVICE: ${{ vars.ECS_SERVICE }} # set this to your Amazon ECS service name + ECS_CLUSTER: ${{ vars.ECS_CLUSTER }} # set this to your Amazon ECS cluster name + ECS_TASK_DEFINITION: + .aws/task-definition.json # set this to the path to your Amazon ECS task definition + # file, e.g. .aws/task-definition.json + CONTAINER_NAME: + ${{ vars.CONTAINER_NAME }} # set this to the name of the container in the + # containerDefinitions section of your task definition + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + environment: production + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ env.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + with: + mask-password: "false" + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + # Build a docker container and + # push it to ECR so that it can + # be deployed to ECS. + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + + - name: Fill in the new image ID in the Amazon ECS task definition + id: task-def + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: ${{ env.ECS_TASK_DEFINITION }} + container-name: ${{ env.CONTAINER_NAME }} + image: ${{ steps.build-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: ${{ env.ECS_SERVICE }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: true