-
Notifications
You must be signed in to change notification settings - Fork 4
113 lines (94 loc) · 3.79 KB
/
deployment-config.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
name: CD
run-name: Running
on:
push:
branches:
- release/*
- hotfix/*
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: chzz-rep
ECS_SERVICE: chzz-market
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: check out
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Extract version from branch name
id: extract-version
run: |
BRANCH_NAME="${{ github.ref }}" # Full ref name, e.g., refs/heads/release/5.5.6
BRANCH_NAME=${BRANCH_NAME#refs/heads/} # Remove 'refs/heads/' prefix to get 'release/5.5.6'
# Display the extracted branch name for debugging
echo "Extracted branch name: $BRANCH_NAME"
# Check if the branch name matches the expected pattern
if [[ "$BRANCH_NAME" =~ ^(release|hotfix)/([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
VERSION="${BASH_REMATCH[2]}"
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "Extracted version: $VERSION"
else
echo "❌ Branch name does not match release/x.x.x or hotfix/x.x.x format."
exit 1
fi
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'
- name: AWS credential 설정
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: ECR 로그인
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: S3에서 설정파일 복사
run: |
aws s3 cp s3://chzzmarket-production-storage/chzz-production-properties/application-prod.yml \
./src/main/resources/application-prod.yml
- name: gradle 실행 권한 부여
run: chmod +x ./gradlew
- name: gradle 빌드
run: ./gradlew build -x test
- name: Docker 이미지 빌드 및 ECR 배포
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ env.VERSION }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: EC2로 파일 복사 (compose.yaml 및 nginx.conf 포함)
uses: appleboy/scp-action@master
with:
host: ${{ secrets.EC2_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
source: './compose.yaml,./nginx.conf'
target: '/home/ec2-user'
- name: EC2에서 Docker Compose를 통한 애플리케이션 실행
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
export AWS_REGION=${{ env.AWS_REGION }}
export ECR_URI=${{ secrets.ECR_URI }}
# .env 파일 생성 및 ECR_IMAGE 변수 추가
touch /home/ec2-user/.env
echo ECR_IMAGE=${{ steps.build-image.outputs.image }} >> /home/ec2-user/.env
# ECR 레지스트리에 로그인
aws ecr get-login-password --region $AWS_REGION | sudo docker login --username AWS --password-stdin $ECR_URI/chzz-rep
# 기존 컨테이너 정지 및 삭제
sudo docker compose down
# 최신 이미지를 pull
sudo docker compose pull
# Docker Compose로 애플리케이션 실행
sudo docker compose up -d