-
Notifications
You must be signed in to change notification settings - Fork 3
71 lines (54 loc) · 2.39 KB
/
baekjoon_bot_cicd.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
name: Baekjoon-Bot CI/CD
on:
push:
branches: [ "main" ]
jobs:
CI:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js environment
uses: actions/setup-node@v3.8.1
with:
node-version: "18.x"
- name: Install modules
run: npm ci
- name: Unit test
run: npm test
- name: Docker login
uses: docker/login-action@v3.0.0
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
logout: true
- name: Build and push to DockerHub
run: |
docker build -t ${{ secrets.PROJECT_NAME }} .
docker tag ${{ secrets.PROJECT_NAME }}:latest ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest
CD:
needs: CI
runs-on: ubuntu-latest
steps:
- name: Deploy to EC2 / Check bot status
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_KEY }}
script: |
echo "${{ secrets.DOCKERHUB_TOKEN }}" | sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
sudo docker rm $(sudo docker stop $(sudo docker ps -a -q --filter ancestor=${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest --format="{{.ID}}"))
sudo docker rmi ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest
echo "DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}" > env.list
echo "RDS_ENDPOINT=${{ secrets.RDS_ENDPOINT }}" >> env.list
echo "RDS_USER=${{ secrets.RDS_USER }}" >> env.list
echo "RDS_PASSWORD=${{ secrets.RDS_PASSWORD }}" >> env.list
echo "RDS_DB=${{ secrets.RDS_DB }}" >> env.list
echo "BASE_URL=${{ secrets.BASE_URL }}" >> env.list
echo "TZ=Asia/Seoul" >> env.list
sudo docker run -d --env-file env.list --name boj-bot ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest
sleep 10s
sudo docker ps | grep ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}:latest