Skip to content

Commit 31ff3ca

Browse files
committed
[workflow] 프로젝트에 AWS EC2 배포 파이프라인 적용
GitHub Actions 이용하여 자동으로 배포할 수 있도록 적용하였습니다.
1 parent 2271cf0 commit 31ff3ca

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

.github/workflows/aws.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Build and Deploy Docker Image
2+
3+
on:
4+
push:
5+
branches:
6+
- lass9436
7+
8+
jobs:
9+
build_and_deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Docker Buildx
17+
uses: docker/setup-buildx-action@v2
18+
19+
- name: Build Docker image
20+
run: docker build -t jsp-cafe:latest .
21+
22+
- name: Save Docker image to file
23+
run: docker save jsp-cafe:latest | gzip > jsp-cafe.tar.gz
24+
25+
- name: Upload Docker image file
26+
uses: actions/upload-artifact@v3
27+
with:
28+
name: docker-image
29+
path: jsp-cafe.tar.gz
30+
31+
- name: Deploy to EC2
32+
env:
33+
SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
34+
EC2_USER: ${{ secrets.EC2_USER }}
35+
EC2_HOST: ${{ secrets.EC2_HOST }}
36+
run: |
37+
# Save the SSH private key to a file
38+
echo "$SSH_PRIVATE_KEY" > private_key.pem
39+
chmod 600 private_key.pem
40+
41+
# Transfer the Docker image file to EC2 server
42+
scp -i private_key.pem jsp-cafe.tar.gz $EC2_USER@$EC2_HOST:/tmp/
43+
44+
# Connect to EC2 server and run Docker commands
45+
ssh -i private_key.pem $EC2_USER@$EC2_HOST << 'EOF'
46+
docker load < /tmp/jsp-cafe.tar.gz
47+
docker stop jsp-cafe || true
48+
docker rm jsp-cafe || true
49+
docker run -d --name jsp-cafe -p 8080:8080 jsp-cafe:latest
50+
EOF

0 commit comments

Comments
 (0)