-
Notifications
You must be signed in to change notification settings - Fork 252
109 lines (103 loc) · 4.88 KB
/
deploy-preview.yml
File metadata and controls
109 lines (103 loc) · 4.88 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
name: Deploy Preview
on:
workflow_run:
workflows: ["Build Preview"]
types:
- completed
jobs:
deploy-preview:
environment: pr-previews
runs-on: ubuntu-latest
if: ${{github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'}}
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version-file: '.nvmrc'
- run: npm clean-install
- name: Get pull request number
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
id: pull-request-number
with:
result-encoding: string
script: |
const unzipper = require('unzipper');
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id}}
});
const artifact = artifacts.data.artifacts.filter(
artifact => artifact.name === 'pr'
)[0];
if (!artifact) {
throw new Error('No "pr" artifact found');
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip'
});
const directory = await unzipper.Open.buffer(Buffer.from(download.data));
const file = directory.files.find(d => d.path === 'pr_number');
const content = await file.buffer();
return content.toString();
- uses: dawidd6/action-download-artifact@8305c0f1062bb0d184d09ef4493ecb9288447732 # v20
with:
github_token: ${{secrets.GITHUB_TOKEN}}
workflow: build-preview.yml
pr: ${{steps.pull-request-number.outputs.result}}
name: preview
allow_forks: true
- name: Deploy to Netlify
env:
NETLIFY_AUTH_TOKEN: ${{secrets.NETLIFY_AUTH_TOKEN}}
NETLIFY_SITE_ID: ${{secrets.NETLIFY_SITE_ID}}
run: ./node_modules/.bin/netlify deploy --no-build --dir=. --alias=pr-${{steps.pull-request-number.outputs.result}}
- name: Add comment to pull request
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const pullRequestNumber = parseInt(${{steps.pull-request-number.outputs.result}}, 10);
const start = ':bento:';
const author = 'github-actions[bot]';
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequestNumber
});
const commentExists = comments.data.some(
comment => comment.user.login === author && comment.body.startsWith(start)
);
if (!commentExists) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequestNumber,
body: `${start} **[Your pull request preview is ready](https://pr-${pullRequestNumber}--ideditor-presets-preview.netlify.app/id/dist/#locale=en&map=18.00/48.841708/2.587656)**\n\nPlease use this preview to check your changes. Ideally use [the **test documentation** template](https://github.com/openstreetmap/id-tagging-schema/blob/main/.github/PULL_REQUEST_TEMPLATE.md?plain=1#L38-L69) and document your test results by commenting on the PR. This will speed up the review process for everyone.\n\nFYI, once this PR is merged, you can use [the iD Editor Preview](http://preview.ideditor.com/) to test your changes in interaction with all other changes.`
});
} else {
console.log(`Preview URL comment already added to PR #${pullRequestNumber}`);
}
- name: Clean up artifact
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
result-encoding: string
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id}}
});
const artifact = artifacts.data.artifacts.filter(
artifact => artifact.name === 'preview'
)[0];
if (!artifact) {
throw new Error('No "preview" artifact found');
}
await github.rest.actions.deleteArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id
});