Skip to content

Merge pull request #57 from CSID-DGU/back #39

Merge pull request #57 from CSID-DGU/back

Merge pull request #57 from CSID-DGU/back #39

Workflow file for this run

# Workflow 이름
name: StockSignal Backend CI/CD
# 어떤 이벤트가 발생하면 workflow 실행할 지 명시
on:
# prod 브랜치에 push나 pull request 발생 시
push:
branches: [ "prod" ]
pull_request:
branches: [ "prod" ]
# 위 이벤트 발생 시 실행될 작업들
jobs:
build:
# VM의실행 환경 지정 => 우분투 최신 버전
runs-on: ubuntu-latest
# 실행될 jobs를 순서대로 명시
steps:
- name: Checkout
uses: actions/checkout@v3
# JDK 21 설치
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
# Gradle Build를 위한 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x back/gradlew
# Gradle Build (test 제외)
- name: Build with Gradle
working-directory: back
env:
SPRING_PROFILES_ACTIVE: prod # 원하는 프로파일 설정
run: ./gradlew clean build --exclude-task test
# DockerHub 로그인
- name: DockerHub Login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
# Docker 이미지 빌드
- name: Docker Image Build
run: docker build -f back/Dockerfile -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }} .
# DockerHub Push
- name: DockerHub Push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}
# EC2 인스턴스 접속 및 애플리케이션 실행
- name: Application Run
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_KEY }}
script: |
sudo docker kill ${{ secrets.PROJECT_NAME }} || true
sudo docker rm -f ${{ secrets.PROJECT_NAME }} || true
sudo docker rmi ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }} || true
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}
sudo docker run -p ${{ secrets.PORT }}:${{ secrets.PORT }} \
--name ${{ secrets.PROJECT_NAME }} \
-e SPRING_PROFILES_ACTIVE=prod \
-e DB_URL=${{ secrets.MYSQL_URL }} \
-e DB_USER=${{ secrets.MYSQL_USERNAME }} \
-e DB_PASSWD=${{ secrets.MYSQL_PASSWORD }} \
-e JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }} \
-e REST_API_KEY=${{ secrets.REST_API_KEY }} \
-e REDIRECT_URI=${{ secrets.REDIRECT_URI }} \
-e NAVER_CLIENT_ID=${{ secrets.NAVER_CLIENT_ID }} \
-e NAVER_CLIENT_SECRET=${{ secrets.NAVER_CLIENT_SECRET }} \
-d ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.PROJECT_NAME }}