Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] Dev환경 CI/CD에서 Docker 설정 제거 #263

Merged
merged 9 commits into from
Sep 22, 2024
79 changes: 19 additions & 60 deletions .github/workflows/be-cd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ on:

jobs:
build:
timeout-minutes: 3
runs-on: ubuntu-latest
env:
DOCKER_REPOSITORY_NAME: ddangkong/ddangkong-api-dev
timeout-minutes: 2
runs-on: [ self-hosted, linux, ARM64, dev ] # Self hosted runner 사용

defaults:
run:
Expand All @@ -27,77 +25,38 @@ jobs:
run: |
echo "${{ secrets.APPLICATION_DEV_YML }}" > ./src/main/resources/application-dev.yml

- name: Setting SSL/TLS Key
run: |
mkdir -p ./src/main/resources/ssl
echo "${{ secrets.SSL_KEY_BASE64 }}" | base64 -d > ./src/main/resources/ssl/keystore.p12

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

- name: Grant execute permission for gradlew
run: chmod +x gradlew

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

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_GMAIL }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build Docker image
run: |
docker compose -f deploy/docker-compose-dev.yml build
docker tag deploy-ddangkong-api-dev:latest $DOCKER_REPOSITORY_NAME:latest
- name: Change artifact file name
run: mv build/libs/*.jar build/libs/app.jar

- name: Push Docker image
run: docker push $DOCKER_REPOSITORY_NAME:latest
- name: Upload artifact file
uses: actions/upload-artifact@v4
with:
name: app-artifact
path: ./backend/build/libs/app.jar
Comment on lines +40 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

질문) artifact actions을 사용한 이유가 무엇인가요?? 그냥 jar 파일을 빌드해서 사용하면 되면 아닌가? 라고 생각이 들어서요! 🧐

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EC2의 메모리를 아끼기 위해서 artifact 파일 빌드를 Github Actions 에서 수행하고
EC2는 해당 파일을 내려받아서 실행만하도록 구현하였습니다~


deploy:
needs: build
timeout-minutes: 2
runs-on: [ self-hosted, linux, ARM64, dev ] # Self hosted runner 사용
env:
DOCKER_REPOSITORY_NAME: ddangkong/ddangkong-api-dev
CONTAINER_NAME: ddangkong-api-dev
runs-on: [ self-hosted, linux, ARM64, dev ]

steps:
- name: Log in to Docker Hub
uses: docker/login-action@v3
- name: Download artifact file
uses: actions/download-artifact@v4
with:
username: ${{ secrets.DOCKER_GMAIL }}
password: ${{ secrets.DOCKER_TOKEN }}
name: app-artifact
path: ~/app

- name: Stop and Remove previous Docker container
run: |
CONTAINER_ID=$(sudo docker ps -aqf name=$CONTAINER_NAME)
if [ -n "${CONTAINER_ID}" ]; then
sudo docker rm -f ${CONTAINER_ID}
echo "Container ${CONTAINER_ID} is stopped and removed."
else
echo "No previous container found with name. Skipping removal."
fi
- name: Replace application to latest
run: sudo sh ~/scripts/replace-new-version.sh
Comment on lines +58 to +59
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

질문) 굳이 스크립트를 분리해서 사용한 이유가 궁금합니다. 만약 CD 과정에서 해당 스크립트에서 Fail 이 되면 해당 스크립트를 찾아서 확인해야하는데 관리 포인트를 두 군데 두는 것이 아닌가? 라는 생각이 듭니다. CD안에서 jobs로 분리하는 방식이 탭별로 어떤 부분에서 문제가 발생했는지 확인할 수 있으니까 유지보수에 더 좋지 않을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 그렇게 처리하고 싶었는데 경로가 안잡히더라구여..
일단 시간 없어서 클라이언트가 빨리 사용할 수 있도록 저렇게 처리해두고 말았는데 저도 단점이라고 느끼고 있었어서 말 나온 김에 지금 다시 시도해보겠습니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


- name: Remove previous Docker image
run: |
IMAGE_ID=$(sudo docker images --filter=reference=ddangkong/ddangkong-api-dev --format "{{.ID}}")
if [ -n "${IMAGE_ID}" ]; then
sudo docker rmi ${IMAGE_ID}
echo "Image ${IMAGE_ID} is removed."
else
echo "No previous image found with repository name. Skipping removal."
fi

- name: Pull docker image
run: sudo docker pull $DOCKER_REPOSITORY_NAME:latest

- name: Run new Docker container
run: |
docker run -d -p 443:8080 --name $CONTAINER_NAME \
-v /home/ubuntu/app-logs:/logs \
$DOCKER_REPOSITORY_NAME:latest
- name: Health check latest application
run: sudo sh ~/scripts/health-check.sh
7 changes: 0 additions & 7 deletions backend/Dockerfile

This file was deleted.

14 changes: 0 additions & 14 deletions backend/deploy/docker-compose-dev.yml

This file was deleted.

Loading