Skip to content

Commit b529552

Browse files
authored
Merge pull request #35 from GDSCINHA/develop
[FEAT] 배포 방법 변경
2 parents 2e922c1 + 7026f74 commit b529552

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

.github/workflows/deploy.yml

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
1-
name: Deploy to EC2
1+
name: Deploy to EC2 via Docker Hub and CodeDeploy
22

33
on:
44
push:
55
branches:
66
- main
77

88
jobs:
9-
deploy:
10-
runs-on: [self-hosted, Linux, X64]
9+
build-and-push:
10+
runs-on: ubuntu-latest
11+
1112
steps:
1213
- name: Checkout Repository
1314
uses: actions/checkout@v3
1415

15-
- name: Stop & Remove Existing Container (if exists)
16+
- name: Log in to Docker Hub
17+
uses: docker/login-action@v2
18+
with:
19+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
20+
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
21+
22+
- name: Build Docker Image
23+
run: |
24+
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/gdgoc-be-app:latest .
25+
26+
- name: Push Docker Image to Docker Hub
27+
run: |
28+
docker push ${{ secrets.DOCKER_HUB_USERNAME }}/gdgoc-be-app:latest
29+
30+
- name: Generate .env file
1631
run: |
17-
docker stop my-app || true
18-
docker rm my-app || true
32+
echo "DOCKER_HUB_USERNAME=${{ secrets.DOCKER_HUB_USERNAME }}" > .env
33+
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
34+
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env
35+
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env
36+
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> .env
37+
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
38+
zip -r deploy.zip docker-compose.yml deploy.sh appspec.yml .env
39+
aws s3 cp deploy.zip s3://${{ secrets.AWS_S3_BUCKET }}/deploy.zip
1940
20-
- name: Build & Run Docker Container
41+
- name: Trigger AWS CodeDeploy
2142
run: |
22-
docker build -t my-app .
23-
docker run -d --name my-app \
24-
-e SPRING_PROFILES_ACTIVE=prod \
25-
-e SPRING_DATASOURCE_URL="jdbc:postgresql://${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.DB_NAME }}" \
26-
-e SPRING_DATASOURCE_USERNAME="${{ secrets.DB_USERNAME }}" \
27-
-e SPRING_DATASOURCE_PASSWORD="${{ secrets.DB_PASSWORD }}" \
28-
-p 8080:8080 my-app
43+
aws deploy create-deployment \
44+
--application-name ${{ secrets.AWS_CODEDEPLOY_APP }} \
45+
--deployment-group-name ${{ secrets.AWS_CODEDEPLOY_GROUP }} \
46+
--s3-location bucket=${{ secrets.AWS_S3_BUCKET }},bundleType=zip,key=deploy.zip

gdgoc/appspec.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 0.0
2+
os: linux
3+
files:
4+
- source: /
5+
destination: /home/ubuntu/gdgoc-be-app
6+
7+
hooks:
8+
AfterInstall:
9+
- location: deploy.sh
10+
timeout: 300
11+
runas: ubuntu

gdgoc/deploy.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
cd /home/ubuntu/gdgoc-be-app
3+
4+
# 기존 컨테이너 중지 및 삭제
5+
docker-compose down
6+
7+
# 사용되지 않는 컨테이너, 이미지, 네트워크, 볼륨 정리
8+
docker system prune -af
9+
10+
# 불필요한 Docker 볼륨도 정리 (옵션)
11+
docker volume prune -f
12+
13+
# 최신 이미지 가져오기
14+
export $(grep -v '^#' .env | xargs)
15+
docker pull ${DOCKER_HUB_USERNAME}/gdgoc-be-app:latest
16+
17+
# 컨테이너 실행
18+
docker-compose --env-file .env up -d

gdgoc/docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: "3.8"
2+
3+
services:
4+
app:
5+
image: "${DOCKER_HUB_USERNAME}/gdgoc-be-app:latest"
6+
container_name: my-app
7+
restart: always
8+
ports:
9+
- "8080:8080"
10+
environment:
11+
SPRING_PROFILES_ACTIVE: prod
12+
SPRING_DATASOURCE_URL: "jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}"
13+
SPRING_DATASOURCE_USERNAME: "${DB_USERNAME}"
14+
SPRING_DATASOURCE_PASSWORD: "${DB_PASSWORD}"

0 commit comments

Comments
 (0)