Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cc 12 #132

Merged
merged 23 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: Deploy to Amazon ECS

on:
pull_request:
branches:
- main
permissions:
id-token: write
contents: read

env:
AWS_REGION: ap-southeast-2
ECR_REPOSITORY: cc-backend-dockerimage-repo
ECS_SERVICE_UAT: CCS-F-UAT-service
ECS_CLUSTER_UAT: CCS-F-UAT-cluster
ECS_SERVICE_PROD: CCS-F-PROD-service
ECS_CLUSTER_PROD: CCS-F-PROD-cluster
CONTAINER_NAME_UAT: CC-UAT-container
CONTAINER_NAME_PROD: CC-PROD-container

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@v3

- name: "Create env file"
run: |
echo "${{ secrets.ENV_FILE }}" > .env

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: arn:aws:iam::245866473499:role/OIDC-ECS-deployment
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- 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
sed -i "s|{{imageID}}|${IMAGE_TAG}|" taskdef-uat.json
sed -i "s|{{imageID}}|${IMAGE_TAG}|" taskdef-prod.json

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 16

- name: Install dependencies
run: npm install

- name: Migrate DB to DEV
env:
DATA_MIGRATION_URL: ${{ secrets.DATA_MIGRATION_URL_DEV }}
DATABASE_NAME: DEV
run: npm run migrate-up

- name: Render Amazon ECS task definition for uat container
id: task-def-uat
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: taskdef-uat.json
container-name: ${{ env.CONTAINER_NAME_UAT }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition for uat
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: taskdef-uat.json
service: ${{ env.ECS_SERVICE_UAT }}
cluster: ${{ env.ECS_CLUSTER_UAT }}
wait-for-service-stability: true

- name: Migrate DB to PROD
env:
DATA_MIGRATION_URL: ${{ secrets.DATA_MIGRATION_URL_PROD }}
DATABASE_NAME: PROD
run: npm run migrate-up

- name: Render Amazon ECS task definition for prod container
id: task-def-prod
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: taskdef-prod.json
container-name: ${{ env.CONTAINER_NAME_PROD}}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition for prod
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: taskdef-prod.json
service: ${{ env.ECS_SERVICE_PROD }}
cluster: ${{ env.ECS_CLUSTER_PROD }}
wait-for-service-stability: true
43 changes: 6 additions & 37 deletions taskdef-prod.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,20 @@
{
"family": "CCS-F-PROD-task",
"family": "CC-Server-PROD",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"essential": true,
"image": "grafana/fluent-bit-plugin-loki:main",
"name": "log_router",
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"enable-ecs-log-metadata": "true"
}
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "firelens-container-prod",
"awslogs-region": "ap-southeast-2",
"awslogs-create-group": "true",
"awslogs-stream-prefix": "firelens"
}
},
"memoryReservation": 50
},
{
"name": "CCS-F-PROD-container",
"image": "{{image}}",
"name": "CC-PROD-container",
"image": "245866473499.dkr.ecr.ap-southeast-2.amazonaws.com/cc-backend-dockerimage-repo:{{imageID}}",
"portMappings": [
{
"containerPort": 8080,
"hostPort": 8080,
"protocol": "tcp"
}
],
"logConfiguration": {
"logDriver": "awsfirelens",
"options": {
"Name": "grafana-loki",
"Url": "https://254699:eyJrIjoiNDA4OGJjYmJiNGE2MzliODlkNTdjMDVhYWQ5MzZhOWVmNjRjZTZhMCIsIm4iOiJ0ZXN0IiwiaWQiOjY4MzIzNH0=@logs-prod-004.grafana.net/loki/api/v1/push",
"Labels": "{job=\"prod-logs\"}",
"RemoveKeys": "container_id,ecs_task_arn",
"LabelKeys": "container_name,ecs_task_definition,source,ecs_cluster",
"LineFormat": "key_value"
}
},
"environmentFiles": [
{
"value": "arn:aws:s3:::cc-server-environment-variables/ccs/ccsprod.env",
"value": "arn:aws:s3:::cc-environment-variables/ccs/ccsprod.env",
"type": "s3"
}
],
Expand All @@ -58,5 +26,6 @@
],
"cpu": "1vCPU",
"memory": "2GB",
"executionRoleArn": "arn:aws:iam::497551902879:role/ecsTaskExecutionRole"
"taskRoleArn": "arn:aws:iam::245866473499:role/myEcsTaskExecutionRole",
"executionRoleArn": "arn:aws:iam::245866473499:role/myEcsTaskExecutionRole"
}