forked from codesquad-members-2024/issue-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
70 lines (61 loc) · 2.44 KB
/
server-cd.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
name: Deploy Spring Application to Ec2
# 임시로 배포 브랜치 be/solve로 설정
on:
push:
branches: [ "be/solve" ]
pull_request:
branches: [ "be/solve" ]
# 워크플로우가 저장소의 콘텐츠를 읽을 수 있는 권한을 갖도록 설정
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v4
- name: set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'corretto'
java-version: '17'
- name: Make application.properties
run: |
cd ./be/issue-tracker/src/main
mkdir resources
cd resources
touch application.properties
echo "${{ secrets.PROPERTIES }}" > ./application.properties
shell: bash
- name: Grant execute permission for gradlew
run: |
cd ./be/issue-tracker
chmod +x gradlew
- name: Build with Gradle
run: |
cd ./be/issue-tracker
./gradlew clean build -x test
# dockerfile을 통해 이미지를 빌드하고, 이를 docker repo로 push 합니다.
- name: Docker build & push to docker repo
run: |
cd ./be/issue-tracker
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:latest
# 서버에 접속 후 Docker 이미지 가져와서 배포
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:latest
# 기존에 실행 중인 컨테이너 중지 및 삭제
if [ $(sudo docker ps -q -f name=spring-server) ]; then
sudo docker stop spring-server
sudo docker rm spring-server
fi
# 새로운 컨테이너 실행
sudo docker run -d -p 8080:8080 --name spring-server ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:latest