-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automatically build a preview instance of iD for tagging PRs
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Build Preview | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build-preview: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '18' | ||
- run: npm install | ||
- run: npm run dist | ||
- uses: actions/checkout@v3 | ||
with: | ||
repository: openstreetmap/iD | ||
path: './iD' | ||
- run: npm install | ||
working-directory: './iD' | ||
- run: npm run dist | ||
working-directory: './iD' | ||
env: | ||
ID_PRESETS_CDN_URL: '../../' | ||
|
||
- uses: actions/upload-artifact@v3 | ||
with: | ||
name: preview | ||
path: | | ||
dist | ||
iD/dist | ||
- name: Store pull request number for later use | ||
run: | | ||
echo ${{github.event.number}} > ./pr_number | ||
- uses: actions/upload-artifact@v3 | ||
with: | ||
name: pr | ||
path: ./pr_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
name: Deploy Preview | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["Build Preview"] | ||
types: | ||
- completed | ||
|
||
jobs: | ||
deploy-preview: | ||
runs-on: ubuntu-latest | ||
if: ${{github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'}} | ||
steps: | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: '18' | ||
- run: npm install --global netlify-cli@6 | ||
- run: npm install unzipper@0.10 | ||
|
||
- name: Get pull request number | ||
uses: actions/github-script@v6 | ||
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@v2 | ||
with: | ||
github_token: ${{secrets.GITHUB_TOKEN}} | ||
workflow: build-preview.yml | ||
pr: ${{steps.pull-request-number.outputs.result}} | ||
name: site | ||
path: build/site | ||
|
||
- name: Deploy to Netlify | ||
env: | ||
NETLIFY_AUTH_TOKEN: ${{secrets.NETLIFY_AUTH_TOKEN}} | ||
NETLIFY_SITE_ID: ${{secrets.NETLIFY_SITE_ID}} | ||
run: netlify deploy --dir=build/site --alias=pr-${{steps.pull-request-number.outputs.result}} | ||
|
||
- name: Add comment to pull request | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const pullRequestNumber = parseInt(${{steps.pull-request-number.outputs.result}}, 10); | ||
const start = ':package:'; | ||
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} Preview the website for this branch here: https://pr-${pullRequestNumber}--ideditor-presets-preview.netlify.app/.` | ||
}); | ||
} else { | ||
console.log(`Preview URL comment already added to PR #${pullRequestNumber}`); | ||
} |