Skip to content

Commit

Permalink
#173 [ENV] : CI/CD 파이프라인 개선 - Docker 통합 및 Docker Compose 도입, Gradle 캐싱 (
Browse files Browse the repository at this point in the history
#174)

* #173 env: 도커 파일 수정
- 자바 버전 17로 업그레이드
- 포트 9010으로 설정

* #173 env: CI/CD 파이프라인 개선: Gradle 캐싱 추가 및 Docker 이미지 빌드/배포로 전환
- Gradle 캐싱을 통해 빌드 시간 최적화
- Docker Hub에 이미지 빌드 및 푸시 스크립트 작성
- EC2 환경에서 Docker Compose로 자동 배포 적용

* #173 chore: ci/cd 필요없는 트리거 삭제

* #173 chore: 도커 컴포즈 파일 추가
  • Loading branch information
hye-on authored Sep 29, 2024
1 parent 73a08b6 commit 4811b28
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 69 deletions.
60 changes: 28 additions & 32 deletions .github/workflows/cicd_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: echo "CurrentTime=${{steps.current-time.outputs.formattedTime}}"
shell: bash

- name: make application.yml
- name: Make application.yml
run: |
## create application.yml
mkdir -p ./src/main/resources
Expand All @@ -42,51 +42,47 @@ jobs:
cat ./application.yml
shell: bash

- name: Gradle caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew

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

- name: Deliver File
uses: appleboy/scp-action@master

- name: Login to Docker Hub
uses: docker/login-action@v1
with:
host: ${{ secrets.EC2_HOST_DEV }}
username: ${{ secrets.EC2_USERNAME_DEV }}
key: ${{ secrets.EC2_SSH_KEY_DEV }}
port: 22
source: "build/libs/TogetUp-0.0.1-SNAPSHOT.jar"
target: "/home/ubuntu/app"
strip_components: 2
overwrite: true
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

- name: Docker build & push
run: |
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/togetupdev .
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/togetupdev
- name: Deploy
- name: Deploy to dev
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST_DEV }}
username: ${{ secrets.EC2_USERNAME_DEV }}
key: ${{ secrets.EC2_SSH_KEY_DEV }}
port: 22
wait_for_deployment: false
port: ${{ secrets.EC2_SSH_PORT }}
script: |
SOURCE_DIR=/home/ubuntu/app/
FILE_NAME=TogetUp-0.0.1-SNAPSHOT.jar
PID=$(ps aux | grep $FILE_NAME | grep -v grep | awk '{print $2}')
echo "PID : $PID"
if [ -z "$PID" ]; then
echo "#### THERE IS NO PROCESS ####"
else
echo "#### KILL $PID ####"
sudo kill $PID
fi
echo "#### RUNNING $SOURCE_DIR/$FILE_NAME ####"
cd $SOURCE_DIR
sudo nohup java -jar $FILE_NAME > nohup.out 2> nohup.err < /dev/null &
sleep 5
echo "New PID after restart: $(ps aux | grep $FILE_NAME | grep -v grep | awk '{print $2}')"
sudo docker ps -qa | grep -q . && sudo docker rm -f $(docker ps -qa)
sudo docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/togetupdev
sudo docker-compose up -d
sudo docker image prune -f


62 changes: 27 additions & 35 deletions .github/workflows/cicd_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ env:
FILE_PATH: /home/ubuntu/app/
FILE_NAME: TogetUp-0.0.1-SNAPSHOT.jar
jobs:
build:
deploy_prod:
runs-on: ubuntu-latest
steps:

Expand All @@ -34,7 +34,7 @@ jobs:
run: echo "CurrentTime=${{steps.current-time.outputs.formattedTime}}"
shell: bash

- name: make application.yml
- name: Make application.yml
run: |
## create application.yml
mkdir -p ./src/main/resources
Expand All @@ -44,51 +44,43 @@ jobs:
cat ./application.yml
shell: bash

- name: Gradle caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew

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

- name: Deliver File
uses: appleboy/scp-action@master
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: 22
source: "build/libs/TogetUp-0.0.1-SNAPSHOT.jar"
target: "/home/ubuntu/app"
strip_components: 2
overwrite: true
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

- name: Deploy
- name: Docker build & push
run: |
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/togetupprod .
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/togetupprod
- name: Deploy to prod
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
port: 22
wait_for_deployment: false
port: ${{ secrets.EC2_SSH_PORT }}
script: |
SOURCE_DIR=/home/ubuntu/app/
FILE_NAME=TogetUp-0.0.1-SNAPSHOT.jar
PID=$(ps aux | grep $FILE_NAME | grep -v grep | awk '{print $2}')
echo "PID : $PID"
if [ -z "$PID" ]; then
echo "#### THERE IS NO PROCESS ####"
else
echo "#### KILL $PID ####"
sudo kill $PID
fi
echo "#### RUNNING $SOURCE_DIR/$FILE_NAME ####"
cd $SOURCE_DIR
sudo nohup java -jar $FILE_NAME > nohup.out 2> nohup.err < /dev/null &
sleep 2
echo "New PID after restart: $(ps aux | grep $FILE_NAME | grep -v grep | awk '{print $2}')"
sudo docker ps -qa | grep -q . && sudo docker rm -f $(docker ps -qa)
sudo docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/togetupprod
sudo docker-compose up -d
sudo docker image prune -f
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM openjdk:11
FROM openjdk:17
EXPOSE 9010
ARG JAR_FILE=./build/libs/TogetUp-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} TogetUp.jar
ENTRYPOINT ["java","-jar","TogetUp.jar"]
ENTRYPOINT ["java","-jar","TogetUp.jar"]
19 changes: 19 additions & 0 deletions docker-compose/docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '3.8'

services:
togetup-server:
image: togetupdev/togetupdev
ports:
- "9010:9010"
expose:
- "9010"
restart: always
networks:
- server-connection
volumes:
- /home/ubuntu/app/src/main/resources:/app/src/main/resources
networks:
server-connection:
driver: bridge


36 changes: 36 additions & 0 deletions docker-compose/docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: '3.8'

services:
togetup-server:
image: togetupdev/togetupprod
ports:
- "9010:9010"
expose:
- "9010"
restart: always
networks:
- server-connection
volumes:
- /home/ubuntu/app/src/main/resources:/app/src/main/resources

nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- /etc/nginx/nginx.conf:/etc/nginx/nginx.conf
- /etc/nginx/sites-available:/etc/nginx/sites-available
- /etc/nginx/sites-enabled:/etc/nginx/sites-enabled
- /etc/letsencrypt:/etc/letsencrypt
- /var/www/certbot:/var/www/certbot
networks:
- server-connection
depends_on:
- togetup-server
restart: always

networks:
server-connection:
driver: bridge

0 comments on commit 4811b28

Please sign in to comment.