Skip to content

Commit

Permalink
Feature/cc 12 (#132)
Browse files Browse the repository at this point in the history
* feat: add cc-server pipeline

* feat: add cc-server pipeline

* feat: add permission

* feat: add env file

* feat: update env file

* feat: update env file

* feat: update env file

* feat: update env file

* feat: change container name

* feat: change task definition

* feat: output imageID

* feat: change .env file

* feat: change yml file

* feat: remove grafana in taskdef-prod.json

* feat: remove grafana in taskdef-prod.json

* feat: change container name

* feat: remove loki

* feat: change taskdef-prod.json

* feat: change change container name

* feat: change change container name

* feat: echo tag

* feat: add test and lint

* feat: change npm i to ci
  • Loading branch information
kornzhoudev authored May 31, 2023
1 parent 1dbf312 commit cbb320e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 37 deletions.
113 changes: 113 additions & 0 deletions .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 16

- name: Install dependencies
run: npm ci

- name: Lint code
run: npm run lint

- name: Test code
run: npm run test

- 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: 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"
}

0 comments on commit cbb320e

Please sign in to comment.