-
Notifications
You must be signed in to change notification settings - Fork 0
74 lines (61 loc) · 2.74 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
name: Deploy to AWS
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
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: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push socket Docker image
run: |
docker buildx build --platform linux/amd64 \
--tag ${{ secrets.ECR_SOCKET_REPO_URI }}:latest \
--file ./socket/Dockerfile \
./socket --push
- name: Build and push server Docker image
run: |
docker buildx build --platform linux/amd64 \
--tag ${{ secrets.ECR_SERVER_REPO_URI }}:latest \
--file ./server/Dockerfile \
./server --push
- name: Deploy to EC2 instance
env:
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
run: |
echo "$EC2_SSH_KEY" | base64 --decode > /tmp/id_rsa
chmod 600 /tmp/id_rsa
ssh -o StrictHostKeyChecking=no -i /tmp/id_rsa ec2-user@${{ secrets.EC2_INSTANCE_IP }} << 'EOF'
export AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}
export AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}
export AWS_REGION=${{ secrets.AWS_REGION }}
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin ${{ secrets.ECR_SOCKET_REPO_URI }}
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin ${{ secrets.ECR_SERVER_REPO_URI }}
docker pull ${{ secrets.ECR_SOCKET_REPO_URI }}:latest
docker pull ${{ secrets.ECR_SERVER_REPO_URI }}:latest
docker stop socket-app || true
docker rm socket-app || true
docker run -d --name socket-app -p 3000:3000 ${{ secrets.ECR_SOCKET_REPO_URI }}:latest
docker stop server-app || true
docker rm server-app || true
docker run -d --name server-app -p 5000:5000 \
-e PORT=5000 \
-e MONGO_URI=${{ secrets.MONGO_URI }} \
-e JWT_KEY=${{ secrets.JWT_KEY }} \
${{ secrets.ECR_SERVER_REPO_URI }}:latest
EOF