From 29156f554bbf4241e6c179646ebc4c208e664487 Mon Sep 17 00:00:00 2001 From: Stan Runge Date: Sat, 11 Jan 2025 05:08:32 +0100 Subject: [PATCH] feat: add ci/cd --- .github/workflows/ci-cd.yml | 55 ++++++++++++++++++++ .github/workflows/taskdef.json | 95 ++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 .github/workflows/ci-cd.yml create mode 100644 .github/workflows/taskdef.json diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml new file mode 100644 index 0000000..a5c4f8d --- /dev/null +++ b/.github/workflows/ci-cd.yml @@ -0,0 +1,55 @@ +name: Deploy to Production + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Log into Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - 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: | + 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@c804dfbdd57f713b6c079302a4c01db7017a36fc + with: + task-definition: .github/workflows/taskdef.json + container-name: web + image: ${{ steps.build-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: main-service + cluster: PersonalWebsiteCluster + wait-for-service-stability: true + diff --git a/.github/workflows/taskdef.json b/.github/workflows/taskdef.json new file mode 100644 index 0000000..1dc667a --- /dev/null +++ b/.github/workflows/taskdef.json @@ -0,0 +1,95 @@ +{ + "taskDefinitionArn": "arn:aws:ecs:eu-west-1:949678216675:task-definition/personal-website:14", + "containerDefinitions": [ + { + "name": "web", + "image": "949678216675.dkr.ecr.eu-west-1.amazonaws.com/stan/personal-website", + "cpu": 0, + "portMappings": [ + { + "containerPort": 3000, + "hostPort": 3000, + "protocol": "tcp" + } + ], + "essential": true, + "environment": [ + { + "name": "ORIGIN", + "value": "https://stanrunge.dev" + }, + { + "name": "DATABASE_URL", + "value": "postgres://$DATABASE_USERNAME:$DATABASE_PASSWORD@vash-db.c0n0e8svjact.eu-west-1.rds.amazonaws.com:5432/personal-website-db" + } + ], + "mountPoints": [], + "volumesFrom": [], + "secrets": [ + { + "name": "DATABASE_USERNAME", + "valueFrom": "arn:aws:secretsmanager:eu-west-1:949678216675:secret:rds!db-16c19530-97ce-43c1-87fc-bb44ac8f77a3-9d5wca:username::" + }, + { + "name": "DATABASE_PASSWORD", + "valueFrom": "arn:aws:secretsmanager:eu-west-1:949678216675:secret:rds!db-16c19530-97ce-43c1-87fc-bb44ac8f77a3-9d5wca:password::" + } + ], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/personal-website", + "awslogs-region": "eu-west-1", + "awslogs-stream-prefix": "ecs" + } + }, + "systemControls": [] + } + ], + "family": "personal-website", + "executionRoleArn": "arn:aws:iam::949678216675:role/ecsTaskExecutionRole", + "networkMode": "awsvpc", + "revision": 14, + "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.secrets.asm.environment-variables" + }, + { + "name": "ecs.capability.execution-role-ecr-pull" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" + }, + { + "name": "ecs.capability.task-eni" + } + ], + "placementConstraints": [], + "compatibilities": [ + "EC2", + "FARGATE" + ], + "requiresCompatibilities": [ + "FARGATE" + ], + "cpu": "256", + "memory": "512", + "registeredAt": "2025-01-10T22:52:09.951Z", + "registeredBy": "arn:aws:iam::949678216675:user/stan", + "enableFaultInjection": false, + "tags": [] +}