-
Notifications
You must be signed in to change notification settings - Fork 0
105 lines (91 loc) · 3.31 KB
/
dev-deploy.yml
File metadata and controls
105 lines (91 loc) · 3.31 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
name: Dev - Build and Deploy
on:
push:
branches:
- dev
jobs:
Build-and-Push-docker-image:
runs-on: ubuntu-latest
steps:
- name: Git Checkout
uses: actions/checkout@v3
- name: Generate env file
run: |
echo "NEXT_PUBLIC_API_URL=${{ secrets.API_URL }}" > .env
echo "NEXT_PUBLIC_S3_URL=${{ secrets.S3_URL }}" >> .env
- name: Cache nextjs build
uses: actions/cache@v3
id: yarn-cahce
with:
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
${{ github.workspace }}/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/yarn.lock') }}-
- name: Print nextjs cache directory
continue-on-error: true
run : |
ls -la ${{ github.workspace }}/.next/cache
- 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.V2_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.V2_AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push Docker image to Amazon ECR
uses: docker/build-push-action@v4
env:
DOCKER_BUILDKIT: 1
BUILDKIT_PROGRESS: plain
with:
context: .
file: Dockerfile
push: true
tags: ${{ secrets.V2_ECR_URL }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Exporting nextjs build cache
uses: docker/build-push-action@v4
env:
DOCKER_BUILDKIT: 1
BUILDKIT_PROGRESS: plain
with:
context: .
file: Dockerfile
target: nextjs-cache
outputs: type=local,dest=.
push: false
tags: ${{ secrets.V2_ECR_URL }}
cache-from: type=gha
- name: Print nextjs cache directory after build
continue-on-error: true
run : |
ls -la ${{ github.workspace }}/.next/cache
Deploy-to-EC2:
needs: Build-and-Push-docker-image
runs-on: ubuntu-latest
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.V2_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.V2_AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.V2_EC2_HOST }}
username: ${{ secrets.V2_EC2_USERNAME }}
key: ${{ secrets.V2_EC2_KEY }}
script: |
docker ps -q --filter "publish=3000" | xargs --no-run-if-empty docker stop
docker ps -a -q --filter "publish=3000" | xargs --no-run-if-empty docker rm
aws ecr get-login-password --region ap-northeast-2 | sudo docker login --username AWS --password-stdin ${{ secrets.V2_ECR_URL }}
sudo docker pull ${{ secrets.V2_ECR_URL }}:latest
docker run -d -p 3000:3000 ${{ secrets.V2_ECR_URL }}:latest