HOTFIX: actions의 버전 3으로 수정 #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: X-BE CICD | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main # main 브랜치로의 PR 시에도 실행되도록 설정 | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the repository | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Cache Gradle packages | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle | |
- name: Build with Gradle (Skip Tests) | |
run: ./gradlew build -x test # '-x test' 옵션으로 테스트를 제외하고 빌드 | |
- name: Docker Hub Login | |
run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USER_NAME }}" --password-stdin | |
- name: Build Docker image | |
run: docker build -t ${{ secrets.DOCKER_USER_NAME }}/x-be:latest . | |
- name: Push Docker image | |
run: docker push ${{ secrets.DOCKER_USER_NAME }}/x-be | |
deploy: | |
runs-on: ubuntu-latest | |
needs: build # CI 파이프라인의 build 작업이 끝나야 deploy가 실행됨 | |
steps: | |
# EC2 서버로 배포 | |
- name: Deploy to EC2 | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.SSH_PRIVATE_KEY }} | |
port: 22 | |
# 들여쓰기 추가: script 아래 명령어 한 칸 들여쓰기 | |
- name: Execute deployment commands | |
run: | | |
# 배포 경로로 이동 | |
cd /home/ubuntu/xclone | |
# Docker 이미지를 pull | |
docker pull ${{ secrets.DOCKER_USER_NAME }}/x-be:latest | |
# 기존 컨테이너 중지 및 삭제 | |
docker stop app || true | |
docker rm app || true | |
# 새로운 컨테이너 실행 | |
docker run -d --name app -p 8080:8080 ${{ secrets.DOCKER_USER_NAME }}/x-be:latest |