Skip to content

Commit 4b70d38

Browse files
committed
chore: deploy.yml에서 Docker 이미지 빌드 및 푸시 작업을 개선하고 GitOps 업데이트 기능 추가
1 parent 725243a commit 4b70d38

1 file changed

Lines changed: 67 additions & 20 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ on:
44
push:
55
branches:
66
- main
7+
paths:
8+
- 'Dockerfile'
9+
- 'apps/blog/**'
10+
- '.github/workflows/deploy.yml'
711

812
jobs:
9-
deploy:
13+
build-and-push:
1014
runs-on: ubuntu-latest
1115

1216
permissions:
1317
contents: write
1418
packages: write
15-
pull-requests: write
19+
20+
outputs:
21+
image_tag: ${{ steps.set-tag.outputs.image_tag }}
1622

1723
steps:
1824
- name: Start Timer
@@ -21,6 +27,14 @@ jobs:
2127
- name: Checkout source repo
2228
uses: actions/checkout@v4
2329

30+
- name: Setup pnpm store cache
31+
uses: actions/cache@v4
32+
with:
33+
path: ~/.pnpm-store
34+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
35+
restore-keys: |
36+
${{ runner.os }}-pnpm-store-
37+
2438
- name: Install pnpm
2539
uses: pnpm/action-setup@v2
2640
with:
@@ -35,20 +49,37 @@ jobs:
3549
- name: Install dependencies
3650
run: pnpm install
3751

52+
- name: Set image tag
53+
id: set-tag
54+
run: echo "image_tag=$(date +%Y%m%d%H%M%S)" >> $GITHUB_OUTPUT
55+
3856
- name: Set up Docker Buildx
3957
uses: docker/setup-buildx-action@v3
4058

4159
- name: Login to GHCR
4260
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
4361

44-
- name: Build and push Docker image
45-
run: |
46-
IMAGE_NAME=ghcr.io/${{ github.repository_owner }}/blog
47-
IMAGE_TAG=$(date +%Y%m%d%H%M%S)
48-
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
49-
docker build -t $IMAGE_NAME:$IMAGE_TAG .
50-
docker push $IMAGE_NAME:$IMAGE_TAG
62+
- name: Build and push Docker image with cache
63+
uses: docker/build-push-action@v5
64+
with:
65+
context: .
66+
push: true
67+
tags: ghcr.io/${{ github.repository_owner }}/blog:${{ steps.set-tag.outputs.image_tag }}
68+
cache-from: type=gha
69+
cache-to: type=gha,mode=max
70+
71+
update-gitops:
72+
runs-on: ubuntu-latest
73+
needs: build-and-push
74+
75+
permissions:
76+
contents: write
77+
pull-requests: write
5178

79+
outputs:
80+
pr_url: ${{ steps.cpr.outputs.pull-request-url }}
81+
82+
steps:
5283
- name: Checkout GitOps repo
5384
uses: actions/checkout@v4
5485
with:
@@ -57,30 +88,46 @@ jobs:
5788
path: gitops
5889

5990
- name: Update image tag in GitOps repo
91+
id: update-tag
6092
run: |
6193
cd gitops
6294
FILE=./blog/blog-deploy.yaml
6395
IMAGE_NAME=ghcr.io/cobocho/blog
64-
sed -i "s|$IMAGE_NAME:.*|$IMAGE_NAME:${{ env.IMAGE_TAG }}|g" $FILE
65-
66-
- name: Create Pull Request to GitOps repo
96+
OLD_TAG=$(grep "$IMAGE_NAME" $FILE | sed 's/.*://')
97+
NEW_TAG=${{ needs.build-and-push.outputs.image_tag }}
98+
if [ "$OLD_TAG" = "$NEW_TAG" ]; then
99+
echo "No change in tag. Skipping PR."
100+
echo "skip_pr=true" >> $GITHUB_OUTPUT
101+
exit 0
102+
fi
103+
sed -i "s|$IMAGE_NAME:.*|$IMAGE_NAME:$NEW_TAG|g" $FILE
104+
echo "skip_pr=false" >> $GITHUB_OUTPUT
105+
106+
- name: Create Pull Request
107+
if: steps.update-tag.outputs.skip_pr == 'false'
67108
id: cpr
68109
uses: peter-evans/create-pull-request@v6
69110
with:
70111
token: ${{ secrets.GITOPS_REPO_ACCESS }}
71112
path: gitops
72-
commit-message: 'chore: update blog image tag to ${{ env.IMAGE_TAG }}'
73-
branch: update/image-${{ env.IMAGE_TAG }}
74-
title: 'Update blog image tag to ${{ env.IMAGE_TAG }}'
113+
commit-message: 'chore: update blog image tag to ${{ needs.build-and-push.outputs.image_tag }}'
114+
branch: update/image-${{ needs.build-and-push.outputs.image_tag }}
115+
title: 'Update blog image tag to ${{ needs.build-and-push.outputs.image_tag }}'
75116
body: |
76-
This PR updates the image tag in \`blog-deploy.yaml\` to \`${{ env.IMAGE_TAG }}\`.
117+
This PR updates the image tag in \`blog-deploy.yaml\` to \`${{ needs.build-and-push.outputs.image_tag }}\`.
118+
119+
notify:
120+
runs-on: ubuntu-latest
121+
needs: [build-and-push, update-gitops]
77122

123+
if: always()
124+
125+
steps:
78126
- name: Calculate Elapsed Time
79127
run: echo "ELAPSED_TIME=$(($(date +%s) - $START_TIME))" >> $GITHUB_ENV
80-
if: always()
81128

82129
- name: Send Discord Notification (Success)
83-
if: success()
130+
if: ${{ success() }}
84131
uses: Ilshidur/action-discord@master
85132
env:
86133
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}
@@ -89,10 +136,10 @@ jobs:
89136
✅ **undefined.dev 컨테이너 업로드 완료!**
90137
- ⏱ 소요 시간: `${{ env.ELAPSED_TIME }}`초
91138
- 🔗 [Workflow](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
92-
- 🔗 [Pull Request](${{ steps.cpr.outputs.pull-request-url }})
139+
- 🔗 [Pull Request](${{ needs.update-gitops.outputs.pr_url }})
93140
94141
- name: Send Discord Notification (Failed)
95-
if: failure()
142+
if: ${{ failure() }}
96143
uses: Ilshidur/action-discord@master
97144
env:
98145
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_URL }}

0 commit comments

Comments
 (0)