mirror of
https://github.com/TRaSH-Guides/Guides.git
synced 2026-05-24 11:49:02 -05:00
feat(gha): Cloudflare Pages and PR builds with comments (#2256)
Cloudflare Pages and PR builds with comments
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
name: Build PR & Deploy
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["Trigger PR Build"]
|
||||
types:
|
||||
- completed
|
||||
|
||||
concurrency:
|
||||
group: pull-request-builds
|
||||
cancel-in-progress: false
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
actions: read
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.repository.fork == false
|
||||
outputs:
|
||||
url: ${{ steps.cloudflare.outputs.deployment-url }}
|
||||
pr_number: ${{ steps.pr_info.outputs.pr_number }}
|
||||
pr_sha: ${{ steps.pr_info.outputs.pr_sha }}
|
||||
steps:
|
||||
- name: 'Download artifact'
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
run-id: ${{ github.event.workflow_run.id }}
|
||||
github-token: ${{ github.token }}
|
||||
name: pr_info
|
||||
path: ${{ github.workspace }}/pr_info
|
||||
|
||||
- name: Display structure of downloaded files
|
||||
run: tree ${{ github.workspace }}
|
||||
|
||||
- name: Read PR info
|
||||
id: pr_info
|
||||
run: |
|
||||
echo "pr_number=$(cat ${{ github.workspace }}/pr_info/PR_NUMBER)" >> $GITHUB_OUTPUT
|
||||
echo "pr_sha=$(cat ${{ github.workspace }}/pr_info/PR_SHA)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Checkout repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ steps.pr_info.outputs.pr_sha }}
|
||||
fetch-depth: 0
|
||||
sparse-checkout: |
|
||||
docs
|
||||
images
|
||||
includes
|
||||
overrides
|
||||
|
||||
- name: Setup python
|
||||
uses: actions/setup-python@v5.3.0
|
||||
with:
|
||||
python-version: 3.x
|
||||
|
||||
- name: Get pip cache directory
|
||||
id: pip-cache
|
||||
run: |
|
||||
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v4.2.0
|
||||
with:
|
||||
path: ${{ steps.pip-cache.outputs.dir }}
|
||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pip-
|
||||
|
||||
- name: Install dependencies
|
||||
run: pip install -r docs/requirements.txt
|
||||
|
||||
- name: Build mkdocs
|
||||
run: |
|
||||
mkdocs build --clean
|
||||
mkdocs --version
|
||||
|
||||
- name: Publish to Cloudflare Pages
|
||||
id: cloudflare
|
||||
uses: cloudflare/wrangler-action@v3
|
||||
with:
|
||||
apiToken: ${{ secrets.CF_API_TOKEN }}
|
||||
accountId: ${{ secrets.CF_ACCOUNT_ID }}
|
||||
command: >
|
||||
pages deploy site
|
||||
--project-name=trash-guides
|
||||
--branch=pr-${{ steps.pr_info.outputs.pr_number }}
|
||||
--commit-hash=${{ steps.pr_info.outputs.pr_sha }}
|
||||
|
||||
comment:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
if: always() && github.event.repository.fork == false
|
||||
steps:
|
||||
- name: Checkout repo
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Get PR last commit SHA
|
||||
run: |
|
||||
PR_DATA=$(curl -H "Authorization: token ${{ secrets.GH_TOKEN }}" \
|
||||
-H "Accept: application/vnd.github.v3+json" \
|
||||
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ needs.build.outputs.pr_number }}")
|
||||
LAST_COMMIT_SHA=$(echo "$PR_DATA" | jq -r .head.sha)
|
||||
echo "LAST_COMMIT_SHA=$LAST_COMMIT_SHA" >> $GITHUB_ENV
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
|
||||
|
||||
- name: Comment PR
|
||||
run: |
|
||||
PREVIEW_URL=${{ needs.build.outputs.url }}
|
||||
BRANCH_PREVIEW_URL="https://pr-${{ needs.build.outputs.pr_number }}.trash-guides.pages.dev"
|
||||
BUILD_STATUS=${{ needs.build.result }}
|
||||
|
||||
if [ "$BUILD_STATUS" == "success" ]; then
|
||||
STATUS_EMOJI="✅"
|
||||
STATUS_MESSAGE="Deploy successful!"
|
||||
else
|
||||
STATUS_EMOJI="❌"
|
||||
STATUS_MESSAGE="Build failed!"
|
||||
fi
|
||||
|
||||
COMMENT_BODY="Deploying with ⚡ Cloudflare Pages<br><table><tr><td><strong>Latest commit:</strong></td><td><code>$LAST_COMMIT_SHA</code></td></tr><tr><td><strong>Status:</strong></td><td> $STATUS_EMOJI $STATUS_MESSAGE</td></tr><tr><td><strong>Preview URL:</strong></td><td><a href='$PREVIEW_URL'>$PREVIEW_URL</a></td></tr><tr><td><strong>Branch Preview URL:</strong></td><td><a href='$BRANCH_PREVIEW_URL'>$BRANCH_PREVIEW_URL</a></td></tr></table>"
|
||||
|
||||
ESCAPED_BODY=$(echo "$COMMENT_BODY" | jq -aRs .)
|
||||
COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ needs.build.outputs.pr_number }}/comments"
|
||||
|
||||
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
|
||||
-H "Accept: application/vnd.github+json" \
|
||||
-X POST \
|
||||
-d "{\"body\": $ESCAPED_BODY }" \
|
||||
$COMMENTS_URL
|
||||
@@ -4,52 +4,11 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build docs
|
||||
runs-on: ubuntu-latest
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4.1.6
|
||||
with:
|
||||
fetch-depth: 0
|
||||
sparse-checkout: |
|
||||
docs
|
||||
images
|
||||
includes
|
||||
overrides
|
||||
- name: Setup python
|
||||
uses: actions/setup-python@v5.4.0
|
||||
with:
|
||||
python-version: 3.x
|
||||
- name: Get pip cache directory
|
||||
id: pip-cache
|
||||
run: |
|
||||
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v4.2.0
|
||||
with:
|
||||
path: ${{ steps.pip-cache.outputs.dir }}
|
||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pip-
|
||||
- name: Install dependencies
|
||||
run: pip install -r docs/requirements.txt
|
||||
- name: Build documentation
|
||||
run: mkdocs build
|
||||
|
||||
deploy:
|
||||
if: github.event_name == 'push' && contains(fromJson('["refs/heads/master", "refs/heads/main"]'), github.ref)
|
||||
needs: build
|
||||
name: Deploy docs
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
@@ -63,14 +22,17 @@ jobs:
|
||||
images
|
||||
includes
|
||||
overrides
|
||||
|
||||
- name: Setup python
|
||||
uses: actions/setup-python@v5.4.0
|
||||
with:
|
||||
python-version: 3.x
|
||||
|
||||
- name: Get pip cache directory
|
||||
id: pip-cache
|
||||
run: |
|
||||
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v4.2.0
|
||||
with:
|
||||
@@ -78,7 +40,24 @@ jobs:
|
||||
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-pip-
|
||||
|
||||
- name: Install dependencies
|
||||
run: pip install -r docs/requirements.txt
|
||||
- name: Deploy to GitHub Pages
|
||||
run: mkdocs gh-deploy --force
|
||||
|
||||
- name: Build mkdocs
|
||||
run: |
|
||||
mkdocs build --clean
|
||||
mkdocs --version
|
||||
|
||||
- name: Publish to Cloudflare Pages
|
||||
if: github.event_name == 'push' && contains(fromJson('["refs/heads/master", "refs/heads/main"]'), github.ref)
|
||||
id: cloudflare
|
||||
uses: cloudflare/wrangler-action@v3
|
||||
with:
|
||||
apiToken: ${{ secrets.CF_API_TOKEN }}
|
||||
accountId: ${{ secrets.CF_ACCOUNT_ID }}
|
||||
command: >
|
||||
pages deploy site
|
||||
--project-name=trash-guides
|
||||
--branch=master
|
||||
--commit-hash=${{ github.sha }}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
name: Trigger PR Build
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
concurrency:
|
||||
group: pr-${{ github.event.pull_request.number }}
|
||||
cancel-in-progress: false
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.repository.fork == false
|
||||
steps:
|
||||
- name: Save PR info
|
||||
run: |
|
||||
mkdir -p ./pr_info
|
||||
echo ${{ github.event.pull_request.number }} > ./pr_info/PR_NUMBER
|
||||
echo ${{ github.event.pull_request.head.sha }} > ./pr_info/PR_SHA
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: pr_info
|
||||
path: pr_info/
|
||||
@@ -0,0 +1,28 @@
|
||||
document$.subscribe(function() {
|
||||
console.log("Script loaded and running.");
|
||||
|
||||
const announceElement = document.querySelector('[data-md-component="announce"] .md-banner__inner');
|
||||
|
||||
if (announceElement) {
|
||||
const canonicalUrlElement = document.querySelector('link[rel="canonical"]');
|
||||
|
||||
if (canonicalUrlElement) {
|
||||
const currentUrl = window.location.href;
|
||||
const canonicalHref = canonicalUrlElement.href;
|
||||
|
||||
const normalizeUrl = (url) => {
|
||||
let normalized = url.replace(/\/+$/, '');
|
||||
return normalized;
|
||||
};
|
||||
|
||||
const normalizedCurrentUrl = normalizeUrl(currentUrl);
|
||||
const normalizedCanonicalHref = normalizeUrl(canonicalHref);
|
||||
|
||||
if (normalizedCurrentUrl !== normalizedCanonicalHref) {
|
||||
announceElement.style.display = 'block';
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -85,3 +85,18 @@ td {
|
||||
border-radius:0rem;
|
||||
padding:.625em 2.2em;
|
||||
}
|
||||
|
||||
[data-md-component="announce"] {
|
||||
position: sticky;
|
||||
inset-inline: 0;
|
||||
top: 0;
|
||||
z-index: 5;
|
||||
display: block;
|
||||
}
|
||||
.md-banner {
|
||||
background-color: #f44336;
|
||||
color: #ffffff;
|
||||
}
|
||||
[data-md-component="announce"] .md-banner__inner {
|
||||
display: none;
|
||||
}
|
||||
|
||||
@@ -31,6 +31,9 @@ extra_css:
|
||||
- stylesheets/extra.css
|
||||
- stylesheets/github-permalink-style.css
|
||||
|
||||
extra_javascript:
|
||||
- js/hide_announce.js
|
||||
|
||||
markdown_extensions:
|
||||
- abbr # used for Tooltips
|
||||
- attr_list
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block announce %}
|
||||
Docs built by Pull Request - Do not use these.
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{{ super() }}
|
||||
<footer class="sponsorship">
|
||||
|
||||
Reference in New Issue
Block a user