Skip to content
Merged

Dev #83

Show file tree
Hide file tree
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
65 changes: 32 additions & 33 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Deploy to EC2 with Docker hub
on:
push:
branches:
- main
- dev

jobs:
deploy:
Expand All @@ -30,21 +30,30 @@ jobs:
uses: docker/setup-buildx-action@v3


# Dockerfile을 μ‚¬μš©ν•˜μ—¬ 이미지λ₯Ό λΉŒλ“œν•˜κ³  Docker Hub에 ν‘Έμ‹œν•©λ‹ˆλ‹€.
# Dockerfile을 μ‚¬μš© 이미지 λΉŒλ“œ & ν‘Έμ‹œ
- name: Build and push Docker image
uses: docker/[email protected]
with:
context: .
file: ./Dockerfile
push: true
# Docker λ ˆμ΄μ–΄ μΊμ‹œ ν™œμ„±ν™”
cache-from: type=gha
cache-to: type=gha,mode=max
tags: |
${{ secrets.DOCKER_USERNAME }}/gitdeun:latest
${{ secrets.DOCKER_USERNAME }}/gitdeun:${{ github.sha }}

# 3. EC2μ—μ„œ ν™˜κ²½ λ³€μˆ˜λ₯Ό μ„€μ •ν•˜κ³  μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰
# docker-compose.yml μ—…λ‘œλ“œ
- name: Upload docker-compose.yml
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: "docker-compose.prod.yml"
target: "${{ secrets.EC2_TARGET_PATH }}/"

# EC2μ—μ„œ ν™˜κ²½ λ³€μˆ˜λ₯Ό μ„€μ •ν•˜κ³  μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰
- name: Deploy via SSH
uses: appleboy/[email protected]
with:
Expand All @@ -54,36 +63,26 @@ jobs:
port: 22
script: |
set -euo pipefail
# λŒ€μƒ 디렉터리 보μž₯
mkdir -p ${{ secrets.EC2_TARGET_PATH }}

# 멀티라인 .env μ•ˆμ „ μ €μž₯ - μ˜΅μ…˜ A(Heredoc) μ˜ˆμ‹œ
cat > ${{ secrets.EC2_TARGET_PATH }}/.env <<'ENV_EOF'${{ secrets.ENV_FILE }}ENV_EOF
chmod 600 ${{ secrets.EC2_TARGET_PATH }}/.env

# 배포 경둜/둜그 폴더
mkdir -p ${{ secrets.EC2_TARGET_PATH }}/logs
cd ${{ secrets.EC2_TARGET_PATH }}

# 멀티라인 .env μ•ˆμ „ μ €μž₯
cat > ./.env <<'ENV_EOF'
${{ secrets.ENV_FILE }}
ENV_EOF
chmod 600 ./.env

# composeκ°€ μ°Έμ‘°ν•  ν™˜κ²½λ³€μˆ˜(이미지 νƒœκ·Έ/계정)
export TAG=${{ github.sha }}
export DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}

# Docker Hub 둜그인
echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

# λ„€νŠΈμ›Œν¬ μ€€λΉ„
docker network create gitdeun-network || true

# μ΅œμ‹  컀밋 SHA둜 배포(λΆˆλ³€ νƒœκ·Έ)
IMAGE="${{ secrets.DOCKER_USERNAME }}/gitdeun:${{ github.sha }}"
docker pull "$IMAGE"

# κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆ 쀑지/μ‚­μ œ
docker stop gitdeun || true
docker rm gitdeun || true

# μ‹€ν–‰
docker run -d \
--name gitdeun \
--restart unless-stopped \
--env SPRING_PROFILES_ACTIVE=prod,s3Bucket \
--env-file ${{ secrets.EC2_TARGET_PATH }}/.env \
--network gitdeun-network \
-p 8080:8080 \
-v ${{ secrets.EC2_TARGET_PATH }}/logs:/app/logs \
"$IMAGE"
# 였래된 이미지 정리
docker image prune -f --filter "until=168h"
# 이미지 κ°±μ‹  ν›„ ꡬ동
docker compose -f docker-compose.prod.yml pull
docker compose -f docker-compose.prod.yml up -d --remove-orphans

docker image prune -f --filter "until=168h"
47 changes: 47 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
services:
app:
image: ${DOCKER_USERNAME}/gitdeun:${TAG:-latest}
container_name: gitdeun
restart: unless-stopped
env_file: [ ./.env ]
environment:
SPRING_PROFILES_ACTIVE: prod,s3Bucket
TZ: Asia/Seoul
depends_on:
redis:
condition: service_healthy
networks: [gitdeun-network]
ports:
- "8080:8080"
volumes:
- ./logs:/app/logs
healthcheck:
test: ["CMD-SHELL", "wget -qO- http://localhost:8080/actuator/health | grep -q 'UP' || exit 1"]
interval: 15s
timeout: 5s
retries: 10
start_period: 30s

redis:
image: redis:7-alpine
container_name: gitdeun-redis
restart: unless-stopped
networks: [gitdeun-network]
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD}
command: >
sh -c '
if [ -n "$REDIS_PASSWORD" ]; then
exec redis-server --appendonly yes --requirepass "$REDIS_PASSWORD";
else
exec redis-server --appendonly yes;
fi'
healthcheck:
test: ["CMD-SHELL", "if [ -n \"$REDIS_PASSWORD\" ]; then redis-cli -a \"$REDIS_PASSWORD\" ping | grep -q PONG; else redis-cli ping | grep -q PONG; fi"]
interval: 10s
timeout: 5s
retries: 5

networks:
gitdeun-network:
name: gitdeun-network
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
Expand Down