Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated preview instances of iD for tagging PRs #800

Merged
merged 5 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/build-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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-file: '.nvmrc'
- run: npm clean-install
- run: npm run dist
- uses: actions/checkout@v3
with:
repository: openstreetmap/iD
path: './iD'
- run: npm install
working-directory: './iD'
- run: npm run all
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
107 changes: 107 additions & 0 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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-file: '.nvmrc'
- 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: preview

- name: Deploy to Netlify
env:
NETLIFY_AUTH_TOKEN: ${{secrets.NETLIFY_AUTH_TOKEN}}
NETLIFY_SITE_ID: ${{secrets.NETLIFY_SITE_ID}}
run: netlify deploy --dir=. --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 = ':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} Preview the tagging presets of this pull request here: https://pr-${pullRequestNumber}--ideditor-presets-preview.netlify.app/id/dist/.`
});
} else {
console.log(`Preview URL comment already added to PR #${pullRequestNumber}`);
}

- name: Clean up artifact
uses: actions/github-script@v6
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
});
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
node-version-file: '.nvmrc'

- name: Install Node.js dependencies
run: npm install
run: npm clean-install

- name: Build
run: npm run build
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: code style checks
name: Code Style Checks
on: [push, pull_request]
jobs:
lint:
Expand All @@ -21,8 +21,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install
node-version-file: '.nvmrc'
- run: npm clean-install
- run: 'for f in `find data/ -type f -not -iname "*.json"`; do echo "::error File $f is not a .json file."; done'
- run: npm run lint

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will do a clean install of node dependencies and run tests across different versions of node.
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: test
name: Test

on:
push:
Expand All @@ -23,5 +23,5 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm clean-install
- run: npm run test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
/node_modules/
/.tx/tmp/
npm-debug.log
package-lock.json

transifex.auth
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/*