CI
CI는 Continuous Integration으로 빌드와 테스트를 자동화하는 과정이다.
CD
CD는 Continuous deployment로 CI 작업을 끝낸 다음에, 배포 준비가 된 코드를 자동으로 서버에 배포하는 작업이다.
깃허브 액션
깃허브에서 제공하는 서비스로, 리포지토리에 특정 이벤트가 발생하면 특정 작업을 반복할 수 있게 해주는 서비스이다.
시작
- 깃허브 액션 CI/CD 스크립트 작성하기
- 프로젝트에 /.github/workflows 디렉토리를 만든다. 그 안에 [name].yml 파일을 생성해 스크립트를 작성한다.
# 워크플로의 이름 지정
name: Build and Deploy
# 워크플로가 시작될 조건 지정
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest # 실행 환경 지정
# 실행 스텝 지정
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
submodules: recursive # 모든 레벨의 서브모듈을 초기화
token: ${{ secrets.GIT_TOKEN }}
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'zulu'
- name: Make gradlew executable
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build -x test
- name: Check if JAR file exists
run: |
if [ -f ./build/libs/BitO-1.0.1.jar ]; then
echo "JAR file exists."
else
echo "JAR file does not exist."
exit 1
fi
# 원격 서버로 복사
- name: Copy jar file to remote
uses: appleboy/scp-action@master
with:
username: ec2-user
host: ${{ secrets.SERVER_IP }}
key: ${{ secrets.SERVER_KEY }}
source: "./build/libs/BitO-1.0.1.jar"
target: "/home/ec2-user/"
strip_components: 3
# 우분투 배포
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy to prod
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_KEY }}
port: 22
script: |
/home/ec2-user/run_app.sh
여기서 host는 서버 공개 IP 주소,
key는 ec2 생성시 만들었던 키페어의 암호키 내용을
깃허브 레포지토리에 접속 후 [Settings -> Secrets and variables -> Actions]에 넣어주면 된다.
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.SERVER_KEY }}
완료후 main 브런치에 push가 되면, 자동으로 배포되는 것 까지 확인 할 수 있을것이다.
728x90
'SideProject > 개발일지' 카테고리의 다른 글
3-layer-architecture에서의 Command 객체에 대해서 (1) | 2024.12.09 |
---|---|
6. logback 설정 관련 정리 (0) | 2024.11.20 |
5. PlantUML로 ERD와 Sequence Diagram을 그린 학습내용 정리하기 (1) | 2024.10.29 |
4. React + Spring Boot 이용해서 카카오 로그인 구현기 (공식문서 참조) (0) | 2024.08.15 |
3. 내가 @Setter 어노테이션을 싫어하는 이유 (0) | 2024.07.15 |