-
Notifications
You must be signed in to change notification settings - Fork 36
123 lines (104 loc) · 3.65 KB
/
preview.yml
File metadata and controls
123 lines (104 loc) · 3.65 KB
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
name: Deploy Preview
on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- '**'
- '!**--skip-ci'
- '!**--visual-reports'
- '!wip/**'
- '!experiments/**'
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
jobs:
deploy-preview:
name: Build and deploy (branch preview)
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: read
pull-requests: write
steps:
- name: Git checkout
uses: actions/checkout@v5
with:
persist-credentials: false
- name: Use Node.js
uses: actions/setup-node@v5
with:
node-version-file: 'package.json'
cache: yarn
- name: Install dependencies
run: yarn install --immutable
- name: Build
run: yarn workspace dnb-design-system-portal build:mini
- name: Deploy to Cloudflare Pages (branch preview)
id: pages_deploy
run: |
set -euo pipefail
OUTPUT="$(yarn workspace dnb-design-system-portal wrangler pages deploy ./public \
--project-name eufemia \
--branch "${{ github.head_ref || github.ref_name }}" \
--commit-dirty=true)"
echo "$OUTPUT"
ALIAS_URL="$(echo "$OUTPUT" | sed -nE 's/.*Deployment alias URL:\s*(https:\/\/[^ ]+).*/\1/p')"
DEPLOYMENT_URL="$(echo "$OUTPUT" | sed -nE 's/.*Take a peek over at\s*(https:\/\/[^ ]+).*/\1/p')"
echo "alias_url=$ALIAS_URL" >> "$GITHUB_OUTPUT"
echo "deployment_url=$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
- name: Post or update preview comment
if: success() && github.event.pull_request
uses: actions/github-script@v7
env:
ALIAS_URL: ${{ steps.pages_deploy.outputs.alias_url }}
DEPLOYMENT_URL: ${{ steps.pages_deploy.outputs.deployment_url }}
with:
script: |
const aliasUrl = process.env.ALIAS_URL;
const deploymentUrl = process.env.DEPLOYMENT_URL;
const marker = "<!-- pages-preview-comment -->";
const body = [
marker,
"**Branch Preview URL (stable):**",
aliasUrl,
"",
"```",
aliasUrl,
"```",
"",
"**Deployment URL (unique):**",
deploymentUrl,
"",
"```",
deploymentUrl,
"```",
""
].join("\n");
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(c => c.body?.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}