Skip to content

Commit

Permalink
feat(actions): Add preview
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilsang committed Jun 2, 2024
1 parent 05a2483 commit 08eb5ab
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
42 changes: 27 additions & 15 deletions .github/actions/netlify-preview/action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
name: netlify-preview

inputs:
NETLIFY_SITE_ID:
required: true
NETLIFY_API_TOKEN:
required: true
HEAD_COMMIT:
required: true

runs:
using: composite

Expand All @@ -9,43 +17,47 @@ runs:
id: netlify-deploy
shell: bash
run: |
pnpm nf \
pnpm nf deploy \
--dir out \
--site ${{ secrets.NETLIFY_SITE_ID }} \
--auth ${{ secrets.NETLIFY_API_TOKEN }} \
--site ${{ inputs.NETLIFY_SITE_ID }} \
--auth ${{ inputs.NETLIFY_API_TOKEN }} \
--json \
> deploy_output.json
- name: Generate URL Preview
id: url-preview
if: ${{ env.BRANCH_NAME == 'main' }}
shell: bash
# pnpm 샌드박스 로그가 json 파일에 상단에 추가되는 버그가 있음.
# 로그 삭제후 5번째 라인부터 JSON 파일 재생성
run: |
NETLIFY_PREVIEW_URL=$(jq -r '.deploy_url' deploy_output.json)
tail -n +5 deploy_output.json > parsed_output.json
echo $(cat parsed_output.json)
NETLIFY_PREVIEW_URL=$(jq -r '.deploy_url' parsed_output.json)
echo "NETLIFY_PREVIEW_URL=$NETLIFY_PREVIEW_URL" >> "$GITHUB_OUTPUT"
- name: Comment URL Preview on PR
uses: actions/github-script@v7
if: ${{ env.BRANCH_NAME == 'main' }}
env:
NETLIFY_PREVIEW_URL: ${{ steps.url-preview.outputs.NETLIFY_PREVIEW_URL }}
HEAD_COMMIT: ${{ inputs.HEAD_COMMIT }}
with:
script: |
async function comment(){
const result = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: context.sha,
})
const issueNumber = result.data[0].number
if(issueNumber){
commit_sha: process.env.HEAD_COMMIT,
});
const issueNumber = result.data[0]?.number;
if (issueNumber) {
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Preview URL: ' + process.env.NETLIFY_PREVIEW_URL
})
}else{
console.log('No PR found for commit ' + context.sha)
body: `Preview URL: ${process.env.NETLIFY_PREVIEW_URL}`
});
} else {
console.log('No PR found for commit ' + process.env.HEAD_COMMIT);
}
}
comment()
comment();
10 changes: 10 additions & 0 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ jobs:
steps:
- uses: actions/checkout@v4

# 캐싱 된 값을 사용
- name: 🌱 Install pnpm
uses: ./.github/actions/pnpm-install
- name: 🏗 Build and Export
uses: ./.github/actions/nextjs-build-export

- name: 🌈 Netlify preview
uses: ./.github/actions/netlify-preview
with:
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_API_TOKEN: ${{ secrets.NETLIFY_API_TOKEN }}
HEAD_COMMIT: ${{ github.event.pull_request.head.sha }}
Loading

0 comments on commit 08eb5ab

Please sign in to comment.