ci: Fix deploying previews for forked repositories

This commit is contained in:
Kévin Commaille 2022-06-30 11:50:33 +02:00 committed by GitHub
parent 2dacaf2ea0
commit 96155915fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 33 deletions

View File

@ -249,27 +249,18 @@ jobs:
- name: Build docs
run: target/debug/xtask doc --deny-warnings
- name: Deploy PR preview
id: deploy_preview
- name: Compress docs
if: github.event_name == 'pull_request'
continue-on-error: true
uses: dswistowski/surge-sh-action@v1.0.1
with:
domain: pr-${{ github.event.number }}--ruma-docs.surge.sh
project: target/doc
login: ${{ secrets.SURGE_LOGIN }}
token: ${{ secrets.SURGE_TOKEN }}
run: |
tar --zstd -cf docs.tar.zstd -C target doc
- name: Comment deploy PR preview URL
if: steps.deploy_preview.outcome == 'success'
uses: Beakyn/gha-comment-pull-request@v1.0.2
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Upload docs as artifact
if: github.event_name == 'pull_request'
uses: actions/upload-artifact@v3
with:
description-message: |
----
Preview: https://pr-${{ github.event.number }}--ruma-docs.surge.sh
name: docs
path: docs.tar.zstd
- name: Deploy to docs repo
if: github.event_name == 'push' && github.ref == 'refs/heads/main'

62
.github/workflows/docs-preview.yml vendored Normal file
View File

@ -0,0 +1,62 @@
name: Docs Preview
env:
CARGO_TERM_COLOR: always
on:
workflow_run:
workflows: [CI]
types:
- completed
jobs:
deploy:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.pull_requests[0] != null
steps:
- name: Download artifact
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "docs";
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/docs.zip`, Buffer.from(download.data));
- name: Unzip artifact
run: |
unzip docs.zip
tar -xf docs.tar.zstd
- name: Deploy PR preview
continue-on-error: true
id: deploy_preview
uses: dswistowski/surge-sh-action@v1.0.1
with:
domain: pr-${{ github.event.workflow_run.pull_requests[0].number }}--ruma-docs.surge.sh
project: doc
login: ${{ secrets.SURGE_LOGIN }}
token: ${{ secrets.SURGE_TOKEN }}
- name: Comment PR preview URL
if: steps.deploy_preview.outcome == 'success'
uses: Beakyn/gha-comment-pull-request@v1.0.2
env:
GITHUB_TOKEN: ${{ github.token }}
with:
pull-request-number: ${{ github.event.workflow_run.pull_requests[0].number }}
description-message: |
----
Preview: https://pr-${{ github.event.workflow_run.pull_requests[0].number }}--ruma-docs.surge.sh

View File

@ -0,0 +1,57 @@
name: Docs Remove Preview
env:
CARGO_TERM_COLOR: always
on:
workflow_run:
workflows: [PR Closed]
types:
- completed
jobs:
teardown:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request'
steps:
- name: Get PR number
uses: actions/github-script@v6
id: get_pr
continue-on-error: true
with:
script: |
let login = context.payload.workflow_run.head_repository.owner.login;
let branch = context.payload.workflow_run.head_branch;
let head = `${login}:${branch}`;
let prs = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: "closed",
head,
});
if (prs.data.length > 0) {
core.setOutput("prnumber", prs.data[0].number);
} else {
core.setFailed("Could not find PR");
}
- name: Teardown preview
if: steps.get_pr.outcome == 'success'
continue-on-error: true
uses: adrianjost/actions-surge.sh-teardown@v1.0.3
with:
regex: pr-${{ steps.get_pr.outputs.prnumber }}--ruma-docs.surge.sh
env:
SURGE_LOGIN: ${{ secrets.SURGE_LOGIN }}
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
- name: Remove PR preview URL
if: steps.get_pr.outcome == 'success'
uses: Beakyn/gha-comment-pull-request@v1.0.2
env:
GITHUB_TOKEN: ${{ github.token }}
with:
pull-request-number: ${{ steps.get_pr.outputs.prnumber }}
description-message: |
----
Preview Removed

View File

@ -7,20 +7,7 @@ on:
jobs:
docs:
name: Remove Docs Preview
name: Trigger
runs-on: ubuntu-latest
steps:
- name: Teardown
uses: adrianjost/actions-surge.sh-teardown@v1.0.3
with:
regex: pr-${{ github.event.number }}--ruma-docs.surge.sh
env:
SURGE_LOGIN: ${{ secrets.SURGE_LOGIN }}
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
- name: Remove preview URL
uses: Beakyn/gha-comment-pull-request@v1.0.2
env:
GITHUB_TOKEN: ${{ github.token }}
with:
description-message: Preview removed
- run: 'echo "PR number: ${{ github.event.number }}"'