From 713079fcf6f122e9bcfd5d34451f6b3712970a23 Mon Sep 17 00:00:00 2001 From: amirrr <6696894+amirrr@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:45:28 +0200 Subject: [PATCH] deploy --- .aws/task-definition-multi.json | 80 +++++++++++++++++++++++ .github/workflows/aws-deploy-multi.yml | 87 ++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 .aws/task-definition-multi.json create mode 100644 .github/workflows/aws-deploy-multi.yml diff --git a/.aws/task-definition-multi.json b/.aws/task-definition-multi.json new file mode 100644 index 0000000..351442a --- /dev/null +++ b/.aws/task-definition-multi.json @@ -0,0 +1,80 @@ +{ + "family": "atlas-orchestration", + "containerDefinitions": [ + { + "name": "backend", + "image": "533266983284.dkr.ecr.us-east-1.amazonaws.com/atlas-backend", + "cpu": 0, + "portMappings": [ + { + "name": "backend-8000-tcp", + "containerPort": 8000, + "hostPort": 8000, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [], + "environmentFiles": [ + { + "value": "arn:aws:s3:::atlasenvironment/server.env", + "type": "s3" + } + ], + "mountPoints": [], + "volumesFrom": [], + "ulimits": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/atlas-orchestration", + "awslogs-create-group": "true", + "awslogs-region": "us-east-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + }, + "systemControls": [] + }, + { + "name": "frontend", + "image": "533266983284.dkr.ecr.us-east-1.amazonaws.com/atlas-frontend", + "cpu": 0, + "portMappings": [ + { + "name": "frontend-80-tcp", + "containerPort": 80, + "hostPort": 80, + "protocol": "tcp" + } + ], + "essential": true, + "environment": [], + "environmentFiles": [], + "mountPoints": [], + "volumesFrom": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/atlas-orchestration", + "awslogs-create-group": "true", + "awslogs-region": "us-east-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + }, + "systemControls": [] + } + ], + "taskRoleArn": "arn:aws:iam::533266983284:role/ecsTaskExecutionRole", + "executionRoleArn": "arn:aws:iam::533266983284:role/ecsTaskExecutionRole", + "networkMode": "awsvpc", + "requiresCompatibilities": ["FARGATE"], + "cpu": "1024", + "memory": "2048", + "runtimePlatform": { + "cpuArchitecture": "X86_64", + "operatingSystemFamily": "LINUX" + } +} diff --git a/.github/workflows/aws-deploy-multi.yml b/.github/workflows/aws-deploy-multi.yml new file mode 100644 index 0000000..d10e6c0 --- /dev/null +++ b/.github/workflows/aws-deploy-multi.yml @@ -0,0 +1,87 @@ +name: Deploy to Amazon ECS + +on: + workflow_run: + workflows: ["Run Tests (Frontend)"] + branches: [login-window] + types: + - completed + +env: + AWS_REGION: us-east-1 # AWS region + ECR_REPOSITORY_BACKEND: atlas-backend + ECR_REPOSITORY_FRONTEND: atlas-frontend + ECS_SERVICE: atlas-multi-container # Amazon ECS service name + ECS_CLUSTER: atlas-dev # Amazon ECS cluster name + ECS_TASK_DEFINITION: .aws/task-definition-multi.json # Amazon ECS task definition + CONTAINER_NAME_BACKEND: atlas-backend + CONTAINER_NAME_FRONTEND: atlas-frontend + +permissions: + id-token: write + contents: read + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + environment: production + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_PROD_ID }} + aws-secret-access-key: ${{ secrets.AWS_PROD_KEY }} + aws-region: us-east-1 + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build, tag, and push image to Amazon ECR + id: build-backend-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_BACKEND:$IMAGE_TAG -f Dockerfile.backend . + docker push $ECR_REGISTRY/$ECR_REPOSITORY_BACKEND:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY_BACKEND:$IMAGE_TAG" >> $GITHUB_OUTPUT + + - name: Build, tag, and push image to Amazon ECR + id: build-front-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY_FRONTEND:$IMAGE_TAG -f Dockerfile.client . + docker push $ECR_REGISTRY/$ECR_REPOSITORY_FRONTEND:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY_FRONTEND:$IMAGE_TAG" >> $GITHUB_OUTPUT + + - name: Fill in the new image ID in the Amazon ECS task definition + id: render-backend-container + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: ${{ env.ECS_TASK_DEFINITION }} + container-name: ${{ env.CONTAINER_NAME_BACKEND }} + image: ${{ steps.build-backend-image.outputs.image }} + + - name: Fill in the new image ID in the Amazon ECS task definition + id: render-frontend-container + uses: aws-actions/amazon-ecs-render-task-definition@v1 + with: + task-definition: ${{ steps.render-backend-container.outputs.task-definition }} + container-name: ${{ env.CONTAINER_NAME_FRONTEND }} + image: ${{ steps.build-front-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@v1 + with: + task-definition: ${{ steps.render-frontend-container.outputs.task-definition }} + service: ${{ env.ECS_SERVICE }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: true