Skip to content

Merge pull request #59 from 9oormthon-univ/hyundong #116

Merge pull request #59 from 9oormthon-univ/hyundong

Merge pull request #59 from 9oormthon-univ/hyundong #116

Workflow file for this run

name: Java CI with Gradle
on:
push:
branches: [ "main" ]
pull_request:
types: [ closed ]
workflow_dispatch: # Github UI 수동 실행 가능
jobs:
build-docker-image:
runs-on: ubuntu-latest
# 닫힌 PR이 실제로 merge 되었을 때 && PR이 main 브랜치로 merge 될 때
if: github.event_name == 'push' || (github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main')
steps: # 깃허브 레포지토리의 코드 체크아웃
- name: Checkout
uses: actions/checkout@v4
# JDK 17 설치
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Make application yml files
run: |
cd ./src/main/resources
echo "${{ secrets.APPLICATION_PROD }}" > ./application-prod.yml
echo "${{ secrets.APPLICATION_COMMON }}" > ./application-common.yml
# Gradle 설정
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5
# gradlew 파일에 실행 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Spring Boot 애플리케이션을 Gradle로 빌드
- name: Spring Boot Build
run: ./gradlew clean build
# Docker 이미지 빌드 & 이미지 푸시
- name: Docker image build
run: |
docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{secrets.DOCKERHUB_PASSWORD }}
docker build -t youthmap-image .
docker tag youthmap-image ${{ secrets.DOCKERHUB_USERNAME }}/youthmap-image:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/youthmap-image:latest
# DockerHub에 push한 이미지를 EC2에서 받아서 실행
run-docker-image-on-ec2:
needs: build-docker-image
runs-on: self-hosted
steps:
# DockerHub에서 이미지 pull
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/youthmap-image:latest
# 기존에 실행 중인 컨테이너 중지
- name: docker stop container
run: |
if [ $(sudo docker ps -a -q -f name=youthmap) ]; then
sudo docker stop youthmap
fi
# 오래된 Docker 이미지 삭제
- name: delete old docker image
run: sudo docker system prune -f
# 새 컨테이너 실행
- name: docker run new container
run: sudo docker run --rm -it -d -p 8080:8080 --name youthmap ${{ secrets.DOCKERHUB_USERNAME }}/youthmap-image:latest