Skip to content

Commit 5511ffd

Browse files
authored
프로덕션 CI / CD 파이프라인을 분리한다. (#132)
chore: separate production workflow from development workflow
1 parent b7160c3 commit 5511ffd

File tree

2 files changed

+136
-17
lines changed

2 files changed

+136
-17
lines changed
Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
name: Deploy to AWS EC2
1+
name: Deploy to develop environment
22
on:
33
push:
44
branches:
5-
- prod
65
- dev
76
env:
7+
ENVIRONMENT: dev
88
AWS_REGION: ap-northeast-2
99
CONTAINER_NAME: gotchai-server
1010
API_SPECIFICATION_PATH: ./api/src/main/resources/static/docs
1111
jobs:
12-
build:
12+
deploy-api-spec:
1313
name: Deploy API specification
1414
runs-on: ubuntu-latest
15-
if: github.ref_name == 'dev'
16-
environment: ${{ github.ref_name }}
15+
environment: ${{ env.ENVIRONMENT }}
1716
steps:
1817
- name: Checkout repository
1918
uses: actions/checkout@v4
@@ -46,8 +45,8 @@ jobs:
4645
deploy-image:
4746
name: Deploy image
4847
runs-on: ubuntu-latest
49-
environment: ${{ github.ref_name }}
50-
needs: build
48+
environment: ${{ env.ENVIRONMENT }}
49+
needs: deploy-api-spec
5150
outputs:
5251
image: ${{ steps.set-image.outputs.image }}
5352
steps:
@@ -86,7 +85,7 @@ jobs:
8685
name: Deploy container
8786
runs-on: ubuntu-latest
8887
needs: deploy-image
89-
environment: ${{ github.ref_name }}
88+
environment: ${{ env.ENVIRONMENT }}
9089
steps:
9190
- name: Checkout
9291
uses: actions/checkout@v4
@@ -96,7 +95,7 @@ jobs:
9695
host: ${{ secrets.EC2_HOST }}
9796
username: ${{ secrets.EC2_USER }}
9897
key: ${{ secrets.EC2_PRIVATE_KEY }}
99-
source: docker/docker-compose-${{ github.ref_name }}.yml
98+
source: docker/docker-compose-${{ env.ENVIRONMENT }}.yml
10099
target: "~"
101100
strip_components: 1
102101
- name: Build and deploy container to AWS EC2
@@ -106,13 +105,12 @@ jobs:
106105
username: ${{ secrets.EC2_USER }}
107106
key: ${{ secrets.EC2_PRIVATE_KEY }}
108107
script: |
109-
aws secretsmanager get-secret-value --secret-id ${{ github.ref_name }}-env --region ap-northeast-2 --query SecretString --output text | jq -r '. | to_entries | map("\(.key)=\(.value)") | .[]' > .env
108+
aws secretsmanager get-secret-value --secret-id ${{ env.ENVIRONMENT }}-env --region ap-northeast-2 --query SecretString --output text | jq -r '. | to_entries | map("\(.key)=\(.value)") | .[]' > .env
109+
aws s3 cp s3://${{ secrets.API_SPECIFICATION_BUCKET }}/api.yml .
110+
110111
export IMAGE_URI=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}
111-
docker-compose -f docker-compose-${{ github.ref_name }}.yml --env-file .env up -d --build
112-
if [ "${{ github.ref_name }}" = "dev" ]; then
113-
aws s3 cp s3://${{ secrets.API_SPECIFICATION_BUCKET }}/api.yml .
114-
docker restart swagger
115-
fi
112+
docker-compose -f docker-compose-${{ env.ENVIRONMENT }}.yml --env-file .env up -d --build
113+
docker restart swagger
116114
notify-discord:
117115
name: Notify Discord
118116
runs-on: ubuntu-latest
@@ -130,8 +128,8 @@ jobs:
130128
"avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
131129
"embeds": [
132130
{
133-
"title": "**${{ github.ref_name == 'prod' && '프로덕션' || '개발' }} 환경 배포 성공**",
134-
"description": "서버가 성공적으로 배포되었습니다.",
131+
"title": "**개발 환경 배포 성공**",
132+
"description": "개발 서버가 성공적으로 배포되었습니다.",
135133
"color": 3066993,
136134
"fields": [
137135
{

.github/workflows/deploy-prod.yml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
name: Deploy to production environment
2+
on:
3+
push:
4+
branches:
5+
- prod
6+
env:
7+
ENVIRONMENT: prod
8+
AWS_REGION: ap-northeast-2
9+
CONTAINER_NAME: gotchai-server
10+
jobs:
11+
deploy-image:
12+
name: Deploy image
13+
runs-on: ubuntu-latest
14+
environment: ${{ env.ENVIRONMENT }}
15+
outputs:
16+
image: ${{ steps.set-image.outputs.image }}
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
- name: Set up JDK
21+
uses: actions/setup-java@v4
22+
with:
23+
distribution: corretto
24+
java-version: 21
25+
- name: Cache Gradle packages
26+
uses: actions/cache@v3
27+
with:
28+
path: |
29+
~/.gradle/caches
30+
~/.gradle/wrapper
31+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
32+
restore-keys: |
33+
${{ runner.os }}-gradle-
34+
- name: Configure AWS credentials
35+
uses: aws-actions/configure-aws-credentials@v1
36+
with:
37+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
38+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
39+
aws-region: ${{ env.AWS_REGION }}
40+
- name: Set image
41+
id: set-image
42+
run: echo "image=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}" >> $GITHUB_OUTPUT
43+
- name: Build and deploy image to AWS ECR
44+
run: |
45+
./gradlew :api:jib \
46+
-Djib.to.image=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }} \
47+
-Djib.to.auth.username=AWS \
48+
-Djib.to.auth.password=$(aws ecr get-login-password)
49+
deploy-container:
50+
name: Deploy container
51+
runs-on: ubuntu-latest
52+
needs: deploy-image
53+
environment: ${{ env.ENVIRONMENT }}
54+
steps:
55+
- name: Checkout
56+
uses: actions/checkout@v4
57+
- name: Send necessary files to EC2
58+
uses: appleboy/[email protected]
59+
with:
60+
host: ${{ secrets.EC2_HOST }}
61+
username: ${{ secrets.EC2_USER }}
62+
key: ${{ secrets.EC2_PRIVATE_KEY }}
63+
source: docker/docker-compose-${{ env.ENVIRONMENT }}.yml
64+
target: "~"
65+
strip_components: 1
66+
- name: Build and deploy container to AWS EC2
67+
uses: appleboy/[email protected]
68+
with:
69+
host: ${{ secrets.EC2_HOST }}
70+
username: ${{ secrets.EC2_USER }}
71+
key: ${{ secrets.EC2_PRIVATE_KEY }}
72+
script: |
73+
aws secretsmanager get-secret-value --secret-id ${{ env.ENVIRONMENT }}-env --region ap-northeast-2 --query SecretString --output text | jq -r '. | to_entries | map("\(.key)=\(.value)") | .[]' > .env
74+
75+
export IMAGE_URI=${{ secrets.ECR_REGISTRY }}/${{ secrets.ECR_REPOSITORY }}:${{ github.sha }}
76+
docker-compose -f docker-compose-${{ env.ENVIRONMENT }}.yml --env-file .env up -d --build
77+
notify-discord:
78+
name: Notify Discord
79+
runs-on: ubuntu-latest
80+
needs: deploy-container
81+
steps:
82+
- name: Checkout
83+
uses: actions/checkout@v4
84+
- name: Get commit
85+
id: get_commit
86+
run: echo "commit_message=$(git log -1 --pretty=%s)" >> "$GITHUB_OUTPUT"
87+
- name: Send Discord notification
88+
run: |
89+
curl -X POST -H "Content-Type: application/json" -d '{
90+
"username": "GitHub Actions",
91+
"avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png",
92+
"embeds": [
93+
{
94+
"title": "**프로덕션 환경 배포 성공**",
95+
"description": "프로덕션 서버가 성공적으로 배포되었습니다.",
96+
"color": 3066993,
97+
"fields": [
98+
{
99+
"name": "Repository",
100+
"value": "[${{ github.repository }}](https://github.com/${{ github.repository }})",
101+
"inline": true
102+
},
103+
{
104+
"name": "Branch",
105+
"value": "${{ github.ref_name }}",
106+
"inline": true
107+
},
108+
{
109+
"name": "Commit",
110+
"value": "[${{ steps.get_commit.outputs.commit_message }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }})",
111+
"inline": true
112+
},
113+
{
114+
"name": "Author",
115+
"value": "[${{ github.actor }}](${{ github.event.sender.html_url }})",
116+
"inline": true
117+
}
118+
]
119+
}
120+
]
121+
}' ${{ secrets.DISCORD_WEBHOOK_URL }}

0 commit comments

Comments
 (0)