-
Notifications
You must be signed in to change notification settings - Fork 807
129 lines (121 loc) · 4.74 KB
/
deploy-preview.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
name: deploy preview
on:
pull_request_target:
branches:
- main
permissions:
pull-requests: write
jobs:
netlify:
if: github.repository == 'carbon-design-system/carbon-website'
runs-on: ubuntu-latest
environment:
name: Preview
url: ${{ steps.url_preview.outputs.NETLIFY_PREVIEW_URL }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Use Node.js 20.x
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: 20.x
- name: Install packages
run: yarn install --immutable --network-timeout 300000
- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
id: filter # do not use cache if image files have been changed
with:
filters: |
images:
- 'src/**/*.jpg'
- 'src/**/*.png'
- 'src/**/*.svg'
- name: Gatsby Cache Folder
if: ${{ steps.filter.outputs.images == 'false' }}
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
id: gatsby-cache-folder
with:
path: .cache
key: ${{ runner.os }}-cache-gatsby
restore-keys: |
${{ runner.os }}-cache-gatsby
- name: Gatsby Public Folder
if: ${{ steps.filter.outputs.images == 'false' }}
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
id: gatsby-public-folder
with:
path: public/
key: ${{ runner.os }}-public-gatsby
restore-keys: |
${{ runner.os }}-public-gatsby
- name: Build site with cache
if: ${{ steps.filter.outputs.images == 'false' }}
run: yarn build
env:
GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES: true
NODE_ENV: production
CI: true
- name: Build site without cache
if: ${{ steps.filter.outputs.images == 'true' }}
run: yarn build:clean
env:
NODE_ENV: production
CI: true
- name: Deploy to Netlify
id: netlify_deploy
run: |
yarn netlify deploy \
--cwd=public \
--site=${{ secrets.NETLIFY_SITE_ID }} \
--auth=${{ secrets.NETLIFY_AUTH_TOKEN }} \
--alias=${{ github.event.number }} \
--message=${{ github.event.pull_request.issue_url }} \
--json \
> deploy_output.json
- name: Add Netlify deploy output to job summary
run: echo "$(jq -c . < ./deploy_output.json)" >> $GITHUB_STEP_SUMMARY
- name: Generate URL Preview
id: url_preview
run: |
NETLIFY_PREVIEW_URL=$(jq -r '.deploy_url' deploy_output.json)
echo "NETLIFY_PREVIEW_URL=$NETLIFY_PREVIEW_URL" >> "$GITHUB_OUTPUT"
- name: Comment URL Preview on PR
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
NETLIFY_PREVIEW_URL:
${{ steps.url_preview.outputs.NETLIFY_PREVIEW_URL }}
with:
script: |
const { NETLIFY_PREVIEW_URL } = process.env;
// Define a hidden reference we'll put in new comments and also use
// to find existing comments
let hiddenReference = '<!--CarbonWebsiteDeployPreviewComment-DoNotDelete--> '
// Get the existing comments
const {data: comments} = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.number,
})
// Find the existing deploy preview comment
const deployPreviewComment = comments.find(comment => comment.body.includes(hiddenReference))
// Craft the comment body
let comment = hiddenReference + "Deploy preview "
if (process.env.NETLIFY_PREVIEW_URL) {
comment = comment + "successfully published at <a href=\"" + NETLIFY_PREVIEW_URL + "\">" + NETLIFY_PREVIEW_URL + "</a> with commit (${{ github.sha }})"
} else {
comment = comment + "failed to publish"
}
// Create or update comment
if (deployPreviewComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: deployPreviewComment.id,
body: comment
})
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.number,
body: comment
})
}