Skip to content

Commit

Permalink
Merge pull request #128 from infinitybase/FEAT/get-ci
Browse files Browse the repository at this point in the history
Feat/get ci
  • Loading branch information
guimroque authored Jun 25, 2024
2 parents 821979f + 415e425 commit 7d579a7
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 38 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/aws-deploy-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: '[API] Deploy to Amazon ECS'

on:
push:
branches: [ main ]

jobs:
deploy:
name: '[API] Deploy to Amazon ECS'
runs-on: ubuntu-latest

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

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.BAKO_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.BAKO_AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- 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 }}
ECR_REPOSITORY: bako-safe-api
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and push it to ECR
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./packages/api
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- 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: ./packages/api/prod_api_task_definition.json
container-name: bako-safe-api
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: bako-safe-api-service
cluster: Bako-Safe-ECS
wait-for-service-stability: true
59 changes: 59 additions & 0 deletions .github/workflows/aws-deploy-socket.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: '[Socket Server] Deploy to Amazon ECS'

on:
push:
branches: [ main ]

jobs:
deploy:
name: '[Socket Server] Deploy to Amazon ECS'
runs-on: ubuntu-latest

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

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.BAKO_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.BAKO_AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- 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 }}
ECR_REPOSITORY: bako-safe-socket-server-api
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and push it to ECR
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG ./packages/socket-server
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- 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: ./packages/socket-server/prod_socket_task_definition.json
container-name: bako-safe-socket-server-api
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: bako-safe-socket-server-api-service
cluster: Bako-Safe-ECS
wait-for-service-stability: true
24 changes: 0 additions & 24 deletions .github/workflows/test-and-lint.yml

This file was deleted.

7 changes: 4 additions & 3 deletions packages/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM node:18.18.2
FROM arm64v8/node:18.18.2-alpine

# Create app directory
WORKDIR api
WORKDIR /api
ADD . /api

# Install app dependencies
Expand All @@ -13,7 +13,8 @@ RUN yarn global add pm2
# Build
RUN yarn build

# Run!
# Expose the application port
EXPOSE 3333

# Start the application
ENTRYPOINT ["yarn", "start"]
2 changes: 1 addition & 1 deletion packages/api/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
context: .
working_dir: /api
environment:
- NODE_ENV=${API_ENVIRONMENT}
- API_ENVIRONMENT=${API_ENVIRONMENT}
# DATABASE
- DATABASE_HOST=${DATABASE_HOST}
- DATABASE_PORT=${DATABASE_PORT}
Expand Down
106 changes: 106 additions & 0 deletions packages/api/prod_api_task_definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"taskDefinitionArn": "arn:aws:ecs:us-east-1:392170836981:task-definition/bako-safe-api:4",
"containerDefinitions": [
{
"name": "bako-safe-api",
"image": "392170836981.dkr.ecr.us-east-1.amazonaws.com/bako-safe-api:v1",
"cpu": 0,
"portMappings": [
{
"name": "port-3333",
"containerPort": 3333,
"hostPort": 3333,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"environment": [],
"environmentFiles": [
{
"value": "arn:aws:s3:::bako-safe-env/bako-safe-api-prod.env",
"type": "s3"
}
],
"mountPoints": [],
"volumesFrom": [],
"ulimits": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/bako-safe-api",
"awslogs-create-group": "true",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
},
"secretOptions": []
},
"systemControls": []
}
],
"family": "bako-safe-api",
"taskRoleArn": "arn:aws:iam::392170836981:role/ecs-api-bsafe-s3-bako-safe-env",
"executionRoleArn": "arn:aws:iam::392170836981:role/ecsTaskExecutionRole",
"networkMode": "awsvpc",
"revision": 4,
"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.env-files.s3"
},
{
"name": "com.amazonaws.ecs.capability.task-iam-role"
},
{
"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",
"FARGATE"
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024",
"runtimePlatform": {
"cpuArchitecture": "ARM64",
"operatingSystemFamily": "LINUX"
},
"registeredAt": "2024-06-21T19:04:43.968Z",
"registeredBy": "arn:aws:iam::392170836981:user/jota",
"tags": [
{
"key": "environment",
"value": "prod"
},
{
"key": "nome",
"value": "bako-safe-api-task"
}
]
}
14 changes: 12 additions & 2 deletions packages/api/src/config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ const {
DATABASE_USERNAME,
DATABASE_NAME,
DATABASE_PORT,
NODE_ENV,
API_ENVIRONMENT,
} = process.env;

const environment: string = NODE_ENV;
const environment: string = API_ENVIRONMENT;
const entitiesDir = path.resolve(__dirname, '..', 'models', '**', '*{.ts,.js}');

console.log('[DATABASE_CONNECTION]: ', {
DATABASE_PASSWORD,
DATABASE_HOST,
DATABASE_USERNAME,
DATABASE_NAME,
DATABASE_PORT,
API_ENVIRONMENT,
})

export const migrationsDir = path.resolve(
__dirname,
'..',
Expand Down
8 changes: 7 additions & 1 deletion packages/api/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ router.use('/workspace', workspace);

// ping route
router.get('/ping', ({ res }) =>
res.send(`${new Date().toISOString()} ${process.env.APP_NAME}`),
res.send(`${new Date().toISOString()} ${process.env.API_NAME}`),
);

// healthcheck route
router.get('/healthcheck', ({ res }) =>
res.status(200).send({ status: 'ok' })
);

export { router };
2 changes: 1 addition & 1 deletion packages/api/src/socket/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ export class SocketClient {
disconnect() {
this.socket.disconnect();
}
}
}
2 changes: 1 addition & 1 deletion packages/socket-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18.18.2
FROM arm64v8/node:18.18.2-alpine

# Create app directory
WORKDIR socket
Expand Down
Loading

0 comments on commit 7d579a7

Please sign in to comment.