728x90
- 아래 글을 이어서 작업합니다.
github에서 git push를 하면 github 액션이 이벤트로 작업 후 배포
이전 방식과 차이는 빌드의 위치다.
이전은 빌드를 원격 접속해서 EC2 내부에서 진행했지만
github actions 안에서 빌드랑 테스트 과정을 다 할 수 있다.
github actions도 로직을 실행시킬 수 있는 컴퓨터기 때문에 가능하다.
무중단 배포나 여러 EC2에 배포해야한다면 스크립트 작성이 복잡하다는 단점이 있다.
실습
시작 전 서버 종료 및 프로젝트 폴더 삭제
# 확인
lsof -i:8080
# 끄기
sudo fuser -k -n tcp 8080
# rm -rf {프로젝트파일}
이전파일
name: Deploy To EC2
on:
push:
branches:
- dev
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: SSH로 EC2에 접속하기
uses: appleboy/ssh-action@v1.0.3
env:
APPLICATION_PROPERTIES: ${{ secrets.APPLICATION_PROPERTIES }}
with:
host: ${{ secrets.DEV_EC2_HOST }}
username: ${{ secrets.DEV_EC2_USERNAME }}
key: ${{ secrets.DEV_EC2_PRIVATE_KEY }}
envs: APPLICATION_PROPERTIES
script_stop: true
script: |
cd /home/ubuntu/goodsending-be/goodsending
rm -rf src/main/resources/application.yaml
git pull origin dev
echo "$APPLICATION_PROPERTIES" > src/main/resources/application.yaml
./gradlew clean build
sudo fuser -k -n tcp 8080 || true
nohup java -jar build/libs/*SNAPSHOT.jar > ./output.log 2>&1 &
수정 파일
name: Deploy To EC2
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Github Repository에 올린 파일들을 불러오기 # github actions에서 github에 푸쉬한 코드로 작업
uses: actions/checkout@v4 # 라이브러리) 레파지토리에 저장되어있는 코드 불러오는 라이브러리
- name: JDK 17버전 설치 # GitHub Actions에 빌드하고 테스트하기위한 JDK 17버전
uses: actions/setup-java@v4
with:
distribution: temurin # 브랜드
java-version: 17 # 버전
- name: application.yml 파일 만들기 # github actions에서 yml 파일 만들어버림
run: echo "${{ secrets.APPLICATION_PROPERTIES }}" > ./src/main/resources/application.yml
- name: 테스트 및 빌드하기
run: ./gradlew clean build
- name: 빌드된 파일 이름 변경하기
run: mv ./build/libs/*SNAPSHOT.jar ./project.jar
- name: SCP로 EC2에 빌드된 파일 전송하기 # scp: EC2로 파일을 전송할 때 쓰는 방식
uses: appleboy/scp-action@v0.1.7
with: # scp로 전송할 때는 ssh를 기반으로 파일전송하는 방식이라 EC2 정보필요
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: project.jar # 어떤파일을 저장할건지에 대한 코드
target: /home/ubuntu/instagram-server/tobe # 바뀔 파일을 넣을 폴더
- name: SSH로 EC2에 접속하기
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script_stop: true
script: |
rm -rf /home/ubuntu/instagram-server/current # 실행중인 코드 넣을 폴더
mkdir /home/ubuntu/instagram-server/current
mv /home/ubuntu/instagram-server/tobe/project.jar /home/ubuntu/instagram-server/current/project.jar # 이동
cd /home/ubuntu/instagram-server/current
sudo fuser -k -n tcp 8080 || true # 실행되고 있는 서버가 있으면 꺼줌
nohup java -jar project.jar > ./output.log 2>&1 & # 실행
rm -rf /home/ubuntu/instagram-server/tobe # 옮겼으니까 삭제
실행 후 log파일 보기
# current 파일로 이동 후 output.log 파일 열기
cat output.log
디버깅
- name: Test
run: |
ls
pwd
를 넣어서 제대로 실행되는지 확인할 수 있다.
/home/runner/work/...
처럼 경로 확인도 가능하다.
✅ 정리
1. github push
2. github actions 실행
3. github actions에 레파지토리 불러오기
4. yml 파일 설치
5. 빌드테스트
6. 통과하면 jar파일 scp 활용해서 전달
7. ssh로 접속
8. jar파일 기존꺼는 삭제 후 교체
9. 기존 서버 종료후 jar 파일 실행
728x90
'devops > AWS' 카테고리의 다른 글
[AWS][S3] 파일 저장 서비스 S3 (0) | 2024.05.13 |
---|---|
[AWS][EC2] 백엔드 API 서버를 배포하기 - sprint boot 서버 EC2에 배포 (0) | 2024.05.13 |
[AWS][EC2] 백엔드 API 서버를 배포하기 - 탄력적 IP 연결 (0) | 2024.05.13 |
[AWS][EC2] 백엔드 API 서버를 배포하기 - EC2 인스턴스 생성 (0) | 2024.05.12 |