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

[Weekly/10/Chore/Deploy] CI/CD 스크립트 그룹화 #78

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ permissions:
contents: read

jobs:
CI-CD:
## 1단계: 프로젝트 빌드
Build:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v4

Expand All @@ -32,7 +32,6 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-

# 테스트까지 포함해 빌드하려면 -x test를 제거하기
- name: Build With Gradle (exclude test)
run: |
chmod +x gradlew
Expand All @@ -44,25 +43,53 @@ jobs:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Docker Build & Push
run: |
docker build -f Dockerfile --build-arg DEPENDENCY=build/dependency --platform linux/amd64 -t ${{ secrets.DOCKER_REPO_FULLNAME }} .
docker push ${{ secrets.DOCKER_REPO_FULLNAME }}
- name: Docker Build
run: docker build -f Dockerfile --build-arg DEPENDENCY=build/dependency -t ${{ secrets.DOCKER_REPO_FULLNAME }} .

- name: Docker Push
run: docker push ${{ secrets.DOCKER_REPO_FULLNAME }}

## 2단계: 서버에 배포
Deploy:
runs-on: ubuntu-22.04
needs: Build
steps:
- name: Pull New Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: sudo docker pull ${{ secrets.DOCKER_REPO_FULLNAME }}

- name: Stop Old Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: sudo docker stop would-you-in

- name: Run New Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: sudo docker run --rm -d -p 80:8080 --env-file ~/.env --name would-you-in ${{ secrets.DOCKER_REPO_FULLNAME }}

- name: Deploy to Server
- name: Clean-Up Docker Image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }} # EC2 퍼블릭 IPv4 DNS
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: GITHUB_SHA
script: |
echo "[script] docker pull"
sudo docker pull ${{ secrets.DOCKER_REPO_FULLNAME }}
echo "[script] docker stop"
sudo docker stop would-you-in
echo "[script] docker run"
sudo docker run --rm -d -p 80:8080 --env-file ~/.env --name would-you-in ${{ secrets.DOCKER_REPO_FULLNAME }}
echo "[script] docker image clean-up"
sudo docker image prune -f
script: sudo docker image prune -f
Loading