-
Notifications
You must be signed in to change notification settings - Fork 0
73 lines (61 loc) · 2.24 KB
/
docker_build_and_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
name: Docker Image CI and Deploy to Amazon EC2
on:
push:
branches: ['main', 'dev']
pull_request:
branches: ['main', 'dev']
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Docker Setup Buildx
uses: docker/[email protected]
- name: Login to DockerHub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Set image name with date
run: echo "IMAGE_NAME=bada308/mooluck-react:$(date +%s)" >> $GITHUB_ENV
- name: Build and push the Docker image
run: |
docker build . --file Dockerfile -t ${{ env.IMAGE_NAME }}
docker push ${{ env.IMAGE_NAME }}
- name: Save image name to file
run: echo ${{ env.IMAGE_NAME }} > image-name.txt
- name: Upload image name artifact
uses: actions/[email protected]
with:
name: image-name
path: image-name.txt
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download image name artifact
uses: actions/[email protected]
with:
name: image-name
- name: Read image name from file
id: read-image-name
run: echo "IMAGE_NAME=$(cat image-name.txt)" >> $GITHUB_ENV
- name: SSH into EC2 and deploy
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }}
sudo docker pull ${{ env.IMAGE_NAME }}
sudo docker stop mooluck-react || true
sudo docker rm mooluck-react || true
sudo docker run -d --name mooluck-react -p 5173:5173 \
-e VITE_API_URL=${{ secrets.VITE_API_URL }} \
-e VITE_WEBSOCKET_URL=${{ secrets.VITE_WEBSOCKET_URL }} \
-e VITE_NODE_API_URL=${{ secrets.VITE_NODE_API_URL }} \
${{ env.IMAGE_NAME }}