Skip to content

Commit 790e0dd

Browse files
committed
feat: change deploy to aws ecs
1 parent cb4d6c1 commit 790e0dd

2 files changed

Lines changed: 78 additions & 36 deletions

File tree

.github/workflows/main.yml

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,61 @@
1-
name: Deploy to Vercel
1+
name: Deploy to ECS (Fargate)
22

33
on:
44
push:
5-
branches:
6-
[main] # Executa o workflow ao fazer push na branch 'main'
7-
workflow_dispatch: # Permite execução manual do workflow
5+
branches: [ "main" ] # ajuste se necessário
6+
workflow_dispatch:
7+
8+
env:
9+
AWS_REGION: ${{ secrets.AWS_REGION }}
10+
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
11+
ECS_CLUSTER: ${{ secrets.ECS_CLUSTER }}
12+
ECS_SERVICE: ${{ secrets.ECS_SERVICE }}
13+
CONTAINER_NAME: ${{ secrets.CONTAINER_NAME }}
814

915
jobs:
10-
test:
16+
deploy:
1117
runs-on: ubuntu-latest
12-
18+
permissions:
19+
id-token: write # necessário pro OIDC
20+
contents: read
1321
steps:
14-
# Checkout do código
15-
- uses: actions/checkout@v3
22+
- name: Checkout
23+
uses: actions/checkout@v4
1624

17-
# Configurar o Python
18-
- uses: actions/setup-python@v3
25+
- name: Configure AWS credentials (OIDC)
26+
uses: aws-actions/configure-aws-credentials@v4
1927
with:
20-
python-version: '3.9'
21-
22-
# Instalar dependências
23-
- name: Install dependencies
24-
run: |
25-
pip install -r requirements.txt
26-
pip install flake8 pytest mongomock
27-
28-
# Rodar os testes
29-
- name: Run tests
30-
run: make test
31-
32-
deploy:
33-
needs: test # O deploy só será executado se o job 'test' passar
34-
runs-on: ubuntu-latest
35-
36-
steps:
37-
# Checkout do código
38-
- uses: actions/checkout@v3
28+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
29+
aws-region: ${{ env.AWS_REGION }}
3930

40-
# Instalar o Vercel CLI
41-
- name: Install Vercel CLI
42-
run: npm install -g vercel
31+
- name: Login to Amazon ECR
32+
id: ecr-login
33+
uses: aws-actions/amazon-ecr-login@v2
4334

44-
# Fazer o deploy na Vercel
45-
- name: Deploy to Vercel
35+
- name: Build, tag, and push image
4636
env:
47-
VERCEL_TOKEN: ${{ secrets.VERCEL_CLI_KEY }} # Token de API armazenado nos Secrets
37+
REGISTRY: ${{ steps.ecr-login.outputs.registry }}
38+
IMAGE_TAG: ${{ github.sha }}
4839
run: |
49-
vercel --prod --token $VERCEL_TOKEN --yes
40+
docker build -t $REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG .
41+
docker tag $REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG $REGISTRY/${{ env.ECR_REPOSITORY }}:latest
42+
docker push $REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG
43+
docker push $REGISTRY/${{ env.ECR_REPOSITORY }}:latest
44+
echo "IMAGE_URI=$REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG" >> $GITHUB_ENV
45+
46+
# Se você versiona um taskdef.json, renderizamos o container image lá:
47+
- name: Render ECS task definition
48+
id: render
49+
uses: aws-actions/amazon-ecs-render-task-definition@v1
50+
with:
51+
task-definition: ecs-taskdef.json
52+
container-name: ${{ env.CONTAINER_NAME }}
53+
image: ${{ env.IMAGE_URI }}
54+
55+
- name: Deploy ECS service
56+
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
57+
with:
58+
task-definition: ${{ steps.render.outputs.task-definition }}
59+
service: ${{ env.ECS_SERVICE }}
60+
cluster: ${{ env.ECS_CLUSTER }}
61+
wait-for-service-stability: true

ecs-taskdef.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"family": "my-app-task",
3+
"networkMode": "awsvpc",
4+
"requiresCompatibilities": ["FARGATE"],
5+
"cpu": "256",
6+
"memory": "512",
7+
"executionRoleArn": "arn:aws:iam::ACCOUNT_ID:role/ecsTaskExecutionRole",
8+
"taskRoleArn": "arn:aws:iam::ACCOUNT_ID:role/myAppTaskRole",
9+
"containerDefinitions": [
10+
{
11+
"name": "web",
12+
"image": "REPLACED_BY_ACTION",
13+
"essential": true,
14+
"portMappings": [
15+
{ "containerPort": 3000, "protocol": "tcp" }
16+
],
17+
"logConfiguration": {
18+
"logDriver": "awslogs",
19+
"options": {
20+
"awslogs-group": "/ecs/my-app",
21+
"awslogs-region": "us-east-1",
22+
"awslogs-stream-prefix": "ecs"
23+
}
24+
},
25+
"environment": [
26+
{ "name": "NODE_ENV", "value": "production" }
27+
]
28+
}
29+
]
30+
}

0 commit comments

Comments
 (0)