feat: cicd docker λ²μ 2 #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |