-
Notifications
You must be signed in to change notification settings - Fork 807
111 lines (103 loc) · 3.94 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
name: deploy preview
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
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
- name: Gatsby Cache Folder
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
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
run: yarn build
env:
GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES: true
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 }} \
--json \
> deploy_output.json
- 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: |
// 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=\"${{ process.env.NETLIFY_PREVIEW_URL }}\">${{ process.env.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
})
}