Skip to content

Feat/assignment dev #91

Feat/assignment dev

Feat/assignment dev #91

Workflow file for this run

name: Docker CI/CD
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
# Spring Boot 애플리케이션을 빌드하여 도커허브에 푸시하는 과정
build-docker-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# 1. Java 21 세팅
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
# 2. gradlew에 실행 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
# 3. Spring Boot 애플리케이션 빌드
- name: Build with Gradle
run: ./gradlew build
# 5. DockerHub 로그인
- name: Log in to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PWD }}
# 4. Docker 이미지 빌드
- name: Build Docker image
run: docker build -t ${{ secrets.DOCKER_USER }}/bada-spring .
# 6. Docker Hub 이미지 푸시
- name: Push Docker image to DockerHub
run: docker push ${{ secrets.DOCKER_USER }}/bada-spring
run-docker-image-on-ec2:
# build-docker-image 과정이 완료되어야 실행됩니다.
needs: build-docker-image
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: self-hosted
steps:
# 1. 최신 이미지를 풀받습니다
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKER_USER }}/bada-spring
# 2. 기존의 컨테이너를 중지시킵니다
- name: Stop container if running
run: |
if [ "$(sudo docker ps -q -f name=bada-spring-server)" ]; then
echo "Stopping container bada-spring-server..."
sudo docker stop bada-spring-server
else
echo "Container bada-spring-server is not running. Skipping stop command."
fi
- name: Create .env file
run: echo "${{ secrets.BADA_ENV }}" > ~/bada-spring/.env
# 3. 최신 이미지를 컨테이너화하여 실행시킵니다
- name: docker run new container
run: sudo docker run --name bada-spring-server --rm -d -p 5943:5943 --env-file ~/bada-spring/.env ${{ secrets.DOCKER_USER }}/bada-spring
# 4. 미사용 이미지를 정리합니다
- name: delete old docker image
run: sudo docker system prune -f