Merge pull request #4 from Central-MakeUs/feature/infrastructure-setup #7
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: Java CI with Gradle | |
on: | |
push: | |
branches: [ "main" ] | |
permissions: | |
contents: read | |
jobs: | |
build-docker-image: | |
runs-on: ubuntu-latest | |
steps: | |
# 1. 코드 체크아웃 | |
- uses: actions/checkout@v3 | |
# 2. Java 17 세팅 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# 3. Spring Boot 애플리케이션 빌드 | |
- name: Build with Gradle | |
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 | |
with: | |
arguments: clean bootJar | |
# 4. DockerHub 로그인 | |
- name: docker login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# 5. Docker 이미지 빌드 및 푸시 (latest만 적용) | |
- name: Build and Push Docker image | |
run: | | |
IMAGE_NAME=${{ secrets.DOCKERHUB_USERNAME }}/spark | |
docker build -t $IMAGE_NAME:latest . | |
docker push $IMAGE_NAME:latest | |
deploy-to-ec2: | |
needs: build-docker-image | |
runs-on: ubuntu-latest | |
steps: | |
# 1. SSH 설정 | |
- name: SSH into EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_KEY }} | |
script: | | |
docker stop spark-container || true | |
docker rm spark-container || true | |
docker image prune -f | |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/spark:latest | |
docker run -d -p 8080:8080 --name spark-container ${{ secrets.DOCKERHUB_USERNAME }}/spark:latest |