Skip to content

Commit

Permalink
feat: cicd docker 버전3
Browse files Browse the repository at this point in the history
  • Loading branch information
yooookm committed Nov 14, 2024
1 parent 7a64040 commit b5e4d71
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
name: Java CI and Deploy to AWS EC2 with Docker
name: Java CI and Deploy to AWS EC2 (Mock Docker Deployment)

on:
push:
branches: [ "readme" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

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

- name: Build Docker Image
run: |
docker build -t care-app:latest .
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Save SSH key
Expand All @@ -31,19 +15,55 @@ jobs:
echo "$EC2_SSH_KEY" > ec2-key.pem
chmod 600 ec2-key.pem
- name: Upload Docker Image to EC2 and Run
- name: Upload source code to EC2
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USER: ${{ secrets.EC2_USER }}
run: |
# Docker 이미지 μ €μž₯ ν›„ EC2에 전솑
docker save care-app:latest | bzip2 | ssh -i ec2-key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST 'bunzip2 | docker load'
scp -i ec2-key.pem -r ./ $EC2_USER@$EC2_HOST:/home/$EC2_USER/app
# κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆ 쀑지 및 μ‚­μ œ ν›„ μƒˆ μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰
- name: Mock Build and Deploy on EC2
env:
EC2_HOST: ${{ secrets.EC2_HOST }}
EC2_USER: ${{ secrets.EC2_USER }}
run: |
ssh -i ec2-key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << 'EOF'
echo "κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆ 쀑지 및 μ‚­μ œ 쀑..."
docker stop care-app-container || echo "μ‹€ν–‰ 쀑인 μ»¨ν…Œμ΄λ„ˆ μ—†μŒ"
docker rm care-app-container || echo "μ œκ±°ν•  μ»¨ν…Œμ΄λ„ˆ μ—†μŒ"
echo "μƒˆλ‘œμš΄ μ»¨ν…Œμ΄λ„ˆλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€."
docker run -d --name care-app-container -v /path/on/ec2/application-secret.yml:/app/config/application-secret.yml -p 8080:8080 care-app:latest
# μ‹€μ œ μ‹€ν–‰ 없이 λΉŒλ“œ 및 Docker 싀행을 λͺ¨μ˜
MOCK=true
# λΉŒλ“œ 단계
if [ "$MOCK" = true ]; then
echo "EC2μ—μ„œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λΉŒλ“œ 쀑..."
echo "./gradlew clean build -x test"
else
./gradlew clean build -x test
fi
# Docker 이미지 λΉŒλ“œ 단계
if [ "$MOCK" = true ]; then
echo "Docker 이미지λ₯Ό λΉŒλ“œν•˜λŠ” 쀑..."
echo "docker build -t care-app:latest ."
else
docker build -t care-app:latest .
fi
# κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆ 쀑지 및 μ‚­μ œ
if [ "$MOCK" = true ]; then
echo "κΈ°μ‘΄ Docker μ»¨ν…Œμ΄λ„ˆ 쀑지 및 μ‚­μ œ 쀑..."
echo "docker stop care-app-container || true"
echo "docker rm care-app-container || true"
else
docker stop care-app-container || true
docker rm care-app-container || true
fi
# μƒˆ Docker μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰
if [ "$MOCK" = true ]; then
echo "μƒˆλ‘œμš΄ Docker μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰ 쀑..."
echo "docker run -d --name care-app-container -v /home/$EC2_USER/app/config/application-secret.yml:/app/config/application-secret.yml -p 8080:8080 care-app:latest"
else
docker run -d --name care-app-container -v /home/$EC2_USER/app/config/application-secret.yml:/app/config/application-secret.yml -p 8080:8080 care-app:latest
fi
echo "λͺ¨μ˜ 배포 μ™„λ£Œ!"
EOF

0 comments on commit b5e4d71

Please sign in to comment.