Skip to content

Commit

Permalink
CI/CD: Seperate CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
oxdjww committed Nov 9, 2024
1 parent ab57a78 commit b79defe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,75 +1,58 @@
name: Build and Deploy
name: Deploy Docker Image

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

permissions:
contents: read

jobs:
build-docker-image:
runs-on: ubuntu-latest
steps:
# 리포지토리 체크아웃
- name: Checkout repository
uses: actions/checkout@v3

# JDK 17 설정
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

# 줄 끝 형식을 Unix 형식으로 변환
- name: Convert line endings to Unix
run: |
sudo apt-get install dos2unix
dos2unix ./gradlew
# gradlew에 실행 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew

# Gradle을 사용하여 Spring Boot 애플리케이션 빌드
- name: Build with Gradle
run: ./gradlew clean bootJar

# Docker 이미지를 빌드
- name: Docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/cogoserver .

# Docker Hub에 로그인
- name: Docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

# Docker 이미지를 Docker Hub에 푸시
- name: Docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/cogoserver

run-docker-image-on-ec2:
needs: build-docker-image
runs-on: self-hosted
steps:
# Docker 이미지를 EC2에서 pull
- name: Docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/cogoserver

# 기존 cogo 컨테이너 중지
- name: Docker stop container
run: sudo docker stop cogo 2>/dev/null || true

# 기존 cogo 컨테이너 삭제
- name: Docker remove container
run: sudo docker rm cogo 2>/dev/null || true

# 새 컨테이너 실행
- name: Docker run new container
run: |
sudo docker run --name cogo --rm -d -p 8080:8080 \
Expand Down Expand Up @@ -99,13 +82,11 @@ jobs:
-e SMS_FROM=${{ secrets.SMS_FROM }} \
-e SQS_NAME=${{ secrets.SQS_NAME }} \
${{ secrets.DOCKERHUB_USERNAME }}/cogoserver
sleep 5 # 컨테이너 실행 시간을 확보
sudo docker ps -a # 모든 컨테이너 목록 출력
sleep 5
sudo docker ps -a
# 실행된 컨테이너의 로그 확인
- name: Check Docker logs
run: sudo docker logs cogo || true # 컨테이너 로그 출력
run: sudo docker logs cogo || true

# 미사용 Docker 이미지 삭제
- name: Delete old docker image
run: sudo docker system prune -f
32 changes: 32 additions & 0 deletions .github/workflows/cogo_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build Docker Image

on:
pull_request:
branches: ["main"]

jobs:
build-docker-image:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Convert line endings to Unix
run: |
sudo apt-get install dos2unix
dos2unix ./gradlew
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew

- name: Build with Gradle
run: ./gradlew clean bootJar

- name: Docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/cogoserver .

0 comments on commit b79defe

Please sign in to comment.