Skip to content

Commit 8cf1531

Browse files
authored
[Feat]: GithubActions CI/CD 워크플로우 구현
deploy.yml: 깃허브액션 워크플로우 설정 .dockerignore: 빌드 최적화
2 parents bc0c998 + 8c76126 commit 8cf1531

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed

.dockerignore

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Git
2+
.git
3+
.gitignore
4+
.gitattributes
5+
6+
# Gradle
7+
.gradle
8+
**/build/
9+
!gradle/wrapper/gradle-wrapper.jar
10+
11+
# IDE
12+
.idea
13+
.vscode
14+
*.iml
15+
*.iws
16+
*.ipr
17+
out/
18+
19+
# OS
20+
.DS_Store
21+
Thumbs.db
22+
23+
# Documentation
24+
*.md
25+
!README.md
26+
27+
# Terraform
28+
terraform/
29+
30+
# GitHub
31+
.github/
32+
33+
# Test
34+
**/test/
35+
36+
# Logs
37+
*.log
38+
39+
# Temporary files
40+
*.tmp
41+
*.temp
42+
*.swp
43+
44+
# Database
45+
*.db
46+
*.mv.db
47+
*.trace.db
48+
49+
# Environment
50+
.env
51+
.env.local
52+
53+
# Docker
54+
docker-compose.yml
55+
Dockerfile

.github/workflows/deploy.yml

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
name: CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
pull_request:
9+
branches:
10+
- main
11+
12+
env:
13+
AWS_REGION: ap-northeast-2
14+
JAVA_VERSION: 21
15+
16+
jobs:
17+
test:
18+
name: Run Tests
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
25+
- name: Set up JDK 21
26+
uses: actions/setup-java@v3
27+
with:
28+
java-version: '21'
29+
distribution: 'temurin'
30+
31+
- name: Setup Gradle
32+
uses: gradle/gradle-build-action@v2
33+
34+
- name: Run tests
35+
run: ./gradlew test
36+
37+
- name: Upload test results
38+
uses: actions/upload-artifact@v3
39+
if: always()
40+
with:
41+
name: test-results
42+
path: build/test-results/
43+
44+
build:
45+
name: Build and Push Image
46+
runs-on: ubuntu-latest
47+
needs: test
48+
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
49+
50+
outputs:
51+
image: ${{ steps.image.outputs.image }}
52+
tag: ${{ steps.image.outputs.tag }}
53+
54+
steps:
55+
- name: Checkout code
56+
uses: actions/checkout@v4
57+
58+
- name: Set environment
59+
id: env
60+
run: |
61+
if [[ $GITHUB_REF == 'refs/heads/main' ]]; then
62+
echo "environment=prod" >> $GITHUB_OUTPUT
63+
else
64+
echo "environment=dev" >> $GITHUB_OUTPUT
65+
fi
66+
67+
- name: Configure AWS credentials
68+
uses: aws-actions/configure-aws-credentials@v2
69+
with:
70+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
71+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
72+
aws-region: ${{ env.AWS_REGION }}
73+
74+
- name: Login to Amazon ECR
75+
id: login-ecr
76+
uses: aws-actions/amazon-ecr-login@v1
77+
78+
- name: Build, tag, and push image to Amazon ECR
79+
id: image
80+
env:
81+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
82+
ENVIRONMENT: ${{ steps.env.outputs.environment }}
83+
run: |
84+
IMAGE_TAG=$GITHUB_SHA
85+
IMAGE_URI=$ECR_REGISTRY/team12-ecr-1:$IMAGE_TAG
86+
87+
docker build -t $IMAGE_URI .
88+
docker push $IMAGE_URI
89+
90+
echo "image=$IMAGE_URI" >> $GITHUB_OUTPUT
91+
echo "tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
92+
93+
# Also tag as latest
94+
docker tag $IMAGE_URI $ECR_REGISTRY/team12-ecr-1:latest
95+
docker push $ECR_REGISTRY/team12-ecr-1:latest
96+
97+
deploy-dev:
98+
name: Deploy to Development
99+
runs-on: ubuntu-latest
100+
needs: build
101+
if: github.ref == 'refs/heads/develop'
102+
environment: development
103+
104+
steps:
105+
- name: Checkout code
106+
uses: actions/checkout@v4
107+
108+
- name: Configure AWS credentials
109+
uses: aws-actions/configure-aws-credentials@v2
110+
with:
111+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
112+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
113+
aws-region: ${{ env.AWS_REGION }}
114+
115+
- name: Deploy Application
116+
run: |
117+
# Trigger instance refresh to deploy new version
118+
aws autoscaling start-instance-refresh \
119+
--auto-scaling-group-name team12-asg-1 \
120+
--preferences '{"MinHealthyPercentage": 50}'
121+
122+
echo "Deployment initiated for team12-asg-1"
123+
124+
- name: Wait for deployment
125+
run: |
126+
echo "Waiting for instance refresh to complete..."
127+
aws autoscaling wait instance-refresh-complete \
128+
--auto-scaling-group-name team12-asg-1 \
129+
--max-attempts 60
130+
131+
- name: Health Check
132+
run: |
133+
echo "Running health check on https://api.bid-market.shop"
134+
for i in {1..30}; do
135+
if curl -f https://api.bid-market.shop/actuator/health; then
136+
echo "Health check passed!"
137+
exit 0
138+
fi
139+
echo "Waiting for application... ($i/30)"
140+
sleep 10
141+
done
142+
echo "Health check failed!"
143+
exit 1
144+
145+
deploy-prod:
146+
name: Deploy to Production
147+
runs-on: ubuntu-latest
148+
needs: build
149+
if: github.ref == 'refs/heads/main'
150+
environment: production
151+
152+
steps:
153+
- name: Checkout code
154+
uses: actions/checkout@v4
155+
156+
- name: Configure AWS credentials
157+
uses: aws-actions/configure-aws-credentials@v2
158+
with:
159+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
160+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
161+
aws-region: ${{ env.AWS_REGION }}
162+
163+
- name: Setup Terraform
164+
uses: hashicorp/setup-terraform@v2
165+
with:
166+
terraform_version: 1.5.0
167+
168+
- name: Terraform Init
169+
run: |
170+
cd terraform/environments/prod
171+
terraform init
172+
173+
- name: Terraform Apply
174+
run: |
175+
cd terraform/environments/prod
176+
terraform apply -auto-approve
177+
178+
- name: Deploy Application
179+
run: |
180+
cd terraform/environments/prod
181+
ASG_NAME=$(terraform output -raw asg_name)
182+
183+
# Gradual rollout with 90% healthy instances maintained
184+
aws autoscaling start-instance-refresh \
185+
--auto-scaling-group-name $ASG_NAME \
186+
--preferences '{"MinHealthyPercentage": 90, "InstanceWarmup": 300}'
187+
188+
echo "Production deployment initiated for $ASG_NAME"
189+
190+
- name: Wait for deployment
191+
run: |
192+
cd terraform/environments/prod
193+
ASG_NAME=$(terraform output -raw asg_name)
194+
195+
echo "Waiting for instance refresh to complete..."
196+
aws autoscaling wait instance-refresh-complete \
197+
--auto-scaling-group-name $ASG_NAME \
198+
--max-attempts 60
199+
200+
- name: Health Check
201+
run: |
202+
cd terraform/environments/prod
203+
ELASTIC_IP=$(terraform output -raw elastic_ip)
204+
205+
echo "Running health check on https://$ELASTIC_IP"
206+
for i in {1..30}; do
207+
if curl -f -k https://$ELASTIC_IP/actuator/health; then
208+
echo "Production deployment successful!"
209+
exit 0
210+
fi
211+
echo "Waiting for application... ($i/30)"
212+
sleep 10
213+
done
214+
echo "Health check failed!"
215+
exit 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Production 환경은 dev와 동일한 구조이지만 일부 설정이 다름
2+
# dev/main.tf와 동일한 내용을 사용하되, variables에서 값을 다르게 설정
3+
4+
# dev/main.tf의 내용을 그대로 사용
5+
# (실제로는 심볼릭 링크나 모듈화를 고려할 수 있음)

0 commit comments

Comments
 (0)