Skip to content

Commit

Permalink
Merge pull request #31 from wndlthsk/Master
Browse files Browse the repository at this point in the history
[Feat] #8 CI/CD 관련 수정했어요
  • Loading branch information
sanghee0820 authored Oct 4, 2024
2 parents 5f76bdf + 4198919 commit c71e1aa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
53 changes: 35 additions & 18 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
name: Java CI with Gradle

# Master, develop 브랜치에 push 혹은 pull request가 발생할 경우 동작한다.
# Master, develop, weekly/* 브랜치에 push가 발생할 경우 동작한다.
on:
push:
branches: [ "Master", "develop" ]
pull_request:
branches: [ "Master", "develop" ]
branches: [ "Master", "develop", "weekly/*" ]

permissions:
contents: read

jobs:
# Spring Boot 애플리케이션을 빌드하여 도커허브에 푸시하는 과정
build-docker-image:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
# 1. Java 17 세팅
# Java 17 세팅
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

# Gradle Wrapper 파일 실행 권한 주기
- name: Run chmod to make gradlew executable
run: chmod +x ./gradlew

# 2. Spring Boot 애플리케이션 빌드
# Spring Boot 애플리케이션 빌드
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: clean bootJar

# 3. Docker 이미지 빌드
# 테스트 실행
# - name: Run tests
# run: ./gradlew test

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

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

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

Expand All @@ -55,23 +58,37 @@ jobs:
needs: build-docker-image
runs-on: self-hosted

# weekly/* 브랜치에서는 실행되지 않도록 조건 추가
if: startsWith(github.ref, 'refs/heads/Master') || startsWith(github.ref, 'refs/heads/develop')

steps:
# 1. 최신 이미지를 pull
# 최신 이미지를 pull
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/inplace

# 2. 기존의 컨테이너를 중지
# 기존의 컨테이너를 중지
- name: docker stop container
run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true

# 3. 브랜치에 따라 다른 환경 변수를 설정하여 컨테이너 실행
# .env 파일 생성
- name: Create .env file
run: |
echo "KAKAO_CLIENT_ID=${{ secrets.KAKAO_CLIENT_ID }}" >> .env
echo "KAKAO_CLIENT_SECRET=${{ secrets.KAKAO_CLIENT_SECRET }}" >> .env
echo "KAKAO_REDIRECT_URI=${{ secrets.KAKAO_REDIRECT_URI }}" >> .env
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> .env
echo "JWT_ACCESS_TOKEN_EXPIRED_TIME=${{ secrets.JWT_ACCESS_TOKEN_EXPIRED_TIME }}" >> .env
echo "JWT_REFRESH_TOKEN_EXPIRED_TIME=${{ secrets.JWT_REFRESH_TOKEN_EXPIRED_TIME }}" >> .env
# 브랜치에 따라 다른 환경 변수를 설정하여 컨테이너 실행
- name: docker run new container
run: |
if [ "${{ github.ref }}" == "refs/heads/master" ]; then
sudo docker run --name inplace --rm -d -p 8080:8080 ${{ secrets.DOCKERHUB_USERNAME }}/inplace
if [ "${{ github.ref }}" == "refs/heads/Master" ]; then
sudo docker run --name inplace --rm -d -p 8080:8080 --env-file .env ${{ secrets.DOCKERHUB_USERNAME }}/inplace
elif [ "${{ github.ref }}" == "refs/heads/develop" ]; then
sudo docker run --name inplace-dev --rm -d -p 8081:8080 ${{ secrets.DOCKERHUB_USERNAME }}/inplace
# 4. 미사용 이미지를 정리
sudo docker run --name inplace-dev --rm -d -p 8081:8080 --env-file .env ${{ secrets.DOCKERHUB_USERNAME }}/inplace
fi
# 미사용 이미지를 정리
- name: delete old docker image
run: sudo docker system prune -f
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Dockerfile

# jdk17 Image Start
#FROM --platform=linux/amd64 ubuntu:latest
FROM openjdk:17

# 인자 설정 - Jar_File
ARG JAR_FILE=build/libs/*.jar

# jar 파일 복제
COPY ${JAR_FILE} app.jar

# 인자 설정 부분과 jar 파일 복제 부분 합쳐서 진행해도 무방
#COPY build/libs/*.jar app.jar

# 실행 명령어
ENTRYPOINT ["java", "-jar", "app.jar"]
13 changes: 13 additions & 0 deletions src/main/java/team7/inplace/cicd/TestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package team7.inplace.cicd;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
@GetMapping("/cicd")
public ResponseEntity test() {
return ResponseEntity.ok("ci/cd 테스트2");
}
}

0 comments on commit c71e1aa

Please sign in to comment.