Merge pull request #13009 from tschaub/deploy-preview
Use workflow_run event to deploy pull request previews
This commit is contained in:
@@ -9,14 +9,21 @@ jobs:
|
|||||||
build-preview:
|
build-preview:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
registry-url: https://registry.npmjs.org/
|
|
||||||
- run: npm ci
|
- run: npm ci
|
||||||
- run: npm run build-site
|
- run: npm run build-site
|
||||||
- uses: actions/upload-artifact@v2
|
- uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
name: site
|
name: site
|
||||||
path: build/site
|
path: build/site
|
||||||
|
- name: Store pull request number for later use
|
||||||
|
run: |
|
||||||
|
mkdir -p build/pr
|
||||||
|
echo ${{github.event.number}} > build/pr/number
|
||||||
|
- uses: actions/upload-artifact@v2
|
||||||
|
with:
|
||||||
|
name: pr
|
||||||
|
path: build/pr
|
||||||
|
|||||||
@@ -1,45 +1,102 @@
|
|||||||
name: Deploy Preview
|
name: Deploy Preview
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request_target:
|
workflow_run:
|
||||||
branches:
|
workflows: ["Build Preview"]
|
||||||
- main
|
types:
|
||||||
|
- completed
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
deploy-preview:
|
deploy-preview:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
if: ${{github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'}}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
registry-url: https://registry.npmjs.org/
|
- run: npm install --global netlify-cli@6
|
||||||
- run: npm install --global netlify-cli
|
- run: npm install unzipper@0.10
|
||||||
- uses: lewagon/wait-on-check-action@v1.1.1
|
|
||||||
|
- name: Get pull request number
|
||||||
|
uses: actions/github-script@v5
|
||||||
|
id: pull-request-number
|
||||||
with:
|
with:
|
||||||
ref: ${{github.event.pull_request.head.sha}}
|
result-encoding: string
|
||||||
check-name: 'build-preview'
|
script: |
|
||||||
repo-token: ${{secrets.GITHUB_TOKEN}}
|
const unzipper = require('unzipper');
|
||||||
wait-interval: 10
|
|
||||||
|
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 === 'number');
|
||||||
|
const content = await file.buffer();
|
||||||
|
return content.toString();
|
||||||
|
|
||||||
- uses: dawidd6/action-download-artifact@v2
|
- uses: dawidd6/action-download-artifact@v2
|
||||||
with:
|
with:
|
||||||
github_token: ${{secrets.GITHUB_TOKEN}}
|
github_token: ${{secrets.GITHUB_TOKEN}}
|
||||||
workflow: build-preview.yml
|
workflow: build-preview.yml
|
||||||
pr: ${{github.event.number}}
|
pr: ${{steps.pull-request-number.outputs.result}}
|
||||||
name: site
|
name: site
|
||||||
path: build/site
|
path: build/site
|
||||||
- env:
|
|
||||||
|
- name: Deploy to Netlify
|
||||||
|
env:
|
||||||
NETLIFY_AUTH_TOKEN: ${{secrets.NETLIFY_AUTH_TOKEN}}
|
NETLIFY_AUTH_TOKEN: ${{secrets.NETLIFY_AUTH_TOKEN}}
|
||||||
NETLIFY_SITE_ID: ${{secrets.NETLIFY_SITE_ID}}
|
NETLIFY_SITE_ID: ${{secrets.NETLIFY_SITE_ID}}
|
||||||
run: netlify deploy --dir=build/site --alias=deploy-preview-${{github.event.number}}
|
run: netlify deploy --dir=build/site --alias=deploy-preview-${{steps.pull-request-number.outputs.result}}
|
||||||
- uses: octokit/request-action@v2.x
|
|
||||||
|
- name: Add comment to pull request
|
||||||
|
uses: actions/github-script@v5
|
||||||
with:
|
with:
|
||||||
route: POST /repos/{owner}/{repo}/statuses/{sha}
|
script: |
|
||||||
owner: openlayers
|
const pullRequestNumber = parseInt(${{steps.pull-request-number.outputs.result}}, 10);
|
||||||
repo: openlayers
|
|
||||||
sha: ${{github.event.pull_request.head.sha}}
|
const start = ':package:';
|
||||||
state: "success"
|
const author = 'github-actions[bot]';
|
||||||
target_url: https://deploy-preview-${{github.event.number}}--ol-site.netlify.app
|
|
||||||
context: "Deploy Preview"
|
const comments = await github.rest.issues.listComments({
|
||||||
description: "Preview URL"
|
owner: context.repo.owner,
|
||||||
env:
|
repo: context.repo.repo,
|
||||||
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
issue_number: pullRequestNumber
|
||||||
|
});
|
||||||
|
|
||||||
|
const commentExists = comments.data.some(
|
||||||
|
comment => comment.user.login === author && comment.body.startsWith(start)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!commentExists) {
|
||||||
|
const body = [
|
||||||
|
`${start} Preview the [examples](https://deploy-preview-${pullRequestNumber}--ol-site.netlify.app/examples/) and`,
|
||||||
|
`[docs](https://deploy-preview-${pullRequestNumber}--ol-site.netlify.app/apidoc/) from this branch`,
|
||||||
|
`here: https://deploy-preview-${pullRequestNumber}--ol-site.netlify.app/.`
|
||||||
|
].join(' ');
|
||||||
|
|
||||||
|
await github.rest.issues.createComment({
|
||||||
|
owner: context.repo.owner,
|
||||||
|
repo: context.repo.repo,
|
||||||
|
issue_number: pullRequestNumber,
|
||||||
|
body: body
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log(`Preview URL comment already added to PR #${pullRequestNumber}`);
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,11 +9,10 @@ jobs:
|
|||||||
publish-npm:
|
publish-npm:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
registry-url: https://registry.npmjs.org/
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
- name: Publish
|
- name: Publish
|
||||||
|
|||||||
+10
-10
@@ -24,9 +24,9 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Set Node.js Version
|
- name: Set Node.js Version
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
|
|
||||||
- name: Determine Cache Directory
|
- name: Determine Cache Directory
|
||||||
id: npm-cache
|
id: npm-cache
|
||||||
@@ -59,9 +59,9 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Set Node.js Version
|
- name: Set Node.js Version
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
|
|
||||||
- name: Determine Cache Directory
|
- name: Determine Cache Directory
|
||||||
id: npm-cache
|
id: npm-cache
|
||||||
@@ -94,9 +94,9 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Set Node.js Version
|
- name: Set Node.js Version
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
|
|
||||||
- name: Determine Cache Directory
|
- name: Determine Cache Directory
|
||||||
id: npm-cache
|
id: npm-cache
|
||||||
@@ -129,9 +129,9 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Set Node.js Version
|
- name: Set Node.js Version
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
|
|
||||||
- name: Determine Cache Directory
|
- name: Determine Cache Directory
|
||||||
id: npm-cache
|
id: npm-cache
|
||||||
@@ -167,9 +167,9 @@ jobs:
|
|||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Set Node.js Version
|
- name: Set Node.js Version
|
||||||
uses: actions/setup-node@v1
|
uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: '16'
|
||||||
|
|
||||||
- name: Determine Cache Directory
|
- name: Determine Cache Directory
|
||||||
id: npm-cache
|
id: npm-cache
|
||||||
|
|||||||
Reference in New Issue
Block a user