Skip to content

feat: cicd docker 버전2 #11

feat: cicd docker 버전2

feat: cicd docker 버전2 #11

Workflow file for this run

name: Java CI and Deploy to AWS EC2 with Docker
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
env:
EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }}
run: |
echo "$EC2_SSH_KEY" > ec2-key.pem
chmod 600 ec2-key.pem
- name: Upload Docker Image to EC2 and Run
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'
# κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆ 쀑지 및 μ‚­μ œ ν›„ μƒˆ μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰
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
EOF