Skip to content

Commit

Permalink
ci : Add github action workflow to generate documentation preview
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Jan 31, 2025
1 parent 590aebb commit 190f2ca
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/documentation-preview-cleanup-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Documentation Preview Cleanup Request

on:
pull_request:
types:
- closed

jobs:
on-close:
runs-on: ubuntu-latest

steps:
- name: Save Pull Request to a file
run: echo ${{ github.event.pull_request.number }} > pr_number.txt
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: cleanup-info
path: pr_number.txt

40 changes: 40 additions & 0 deletions .github/workflows/documentation-preview-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Documentation Preview Cleanup

on:
workflow_run:
workflows: [Documentation Preview Cleanup Request]
types:
- completed

env:
PREVIEW_PUBLISH_USERNAME: ${{ secrets.PREVIEW_PUBLISH_USERNAME }}
PREVIEW_PUBLISH_REPOSITORY: ${{ secrets.PREVIEW_PUBLISH_REPOSITORY }}
PREVIEW_PUBLISH_BRANCH: ${{ secrets.PREVIEW_PUBLISH_BRANCH }}
PREVIEW_PUBLISH_TOKEN: ${{ secrets.PREVIEW_PUBLISH_TOKEN }}

jobs:
on-close:
runs-on: ubuntu-latest

steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: cleanup-info
path: .
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Extract Pull Request ID from artifact
run: echo "PR_NUMBER=$(cat ./pr_number.txt)" >> $GITHUB_ENV
- name: Push updates to external repository
run: |
git clone https://${PREVIEW_PUBLISH_USERNAME}:${PREVIEW_PUBLISH_TOKEN}@github.com/${PREVIEW_PUBLISH_USERNAME}/${PREVIEW_PUBLISH_REPOSITORY}.git -b ${PREVIEW_PUBLISH_BRANCH}
cd ${PREVIEW_PUBLISH_REPOSITORY}
rm -rf preview/pr/${PR_NUMBER}
git add preview/pr/${PR_NUMBER}
git commit -am "cleanup (preview) : remove preview for Pull Request #${PR_NUMBER}"
git push origin ${PREVIEW_PUBLISH_BRANCH}
30 changes: 30 additions & 0 deletions .github/workflows/documentation-preview-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Documentation Preview Request

on:
pull_request: {}

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Build docs
run: CI=true make build_docs
- name: Check links in docs
run: make docs_check_links
- name: Create robots.txt
run: |
echo "User-agent: *" > ./public/robots.txt
echo "Disallow: /" >> ./public/robots.txt
- name: Save pull request context to artifact directory
run: |
echo ${{ github.event.pull_request.number }} > ./public/pr_number.txt
echo ${{ github.sha }} | cut -c 1-7 > ./public/sha.txt
- name: Upload GitHub artifact
uses: actions/upload-artifact@v4
with:
name: documentation
path: public

47 changes: 47 additions & 0 deletions .github/workflows/documentation-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Documentation Preview Generator

on:
workflow_run:
workflows: [Documentation Preview Request]
types:
- completed

env:
PREVIEW_PUBLISH_USERNAME: ${{ secrets.PREVIEW_PUBLISH_USERNAME }}
PREVIEW_PUBLISH_REPOSITORY: ${{ secrets.PREVIEW_PUBLISH_REPOSITORY }}
PREVIEW_PUBLISH_BRANCH: ${{ secrets.PREVIEW_PUBLISH_BRANCH }}
PREVIEW_PUBLISH_TOKEN: ${{ secrets.PREVIEW_PUBLISH_TOKEN }}

jobs:
documentation-preview:
runs-on: ubuntu-latest
steps:
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: documentation
path: ./artifact
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Extract Pull Request ID from artifact
run: |
echo "PR_NUMBER=$(cat ./artifact/pr_number.txt)" >> $GITHUB_ENV
echo "PR_SHA=$(cat ./artifact/sha.txt)" >> $GITHUB_ENV
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
personal_token: ${{ env.PREVIEW_PUBLISH_TOKEN }}
external_repository: ${{ env.PREVIEW_PUBLISH_USERNAME }}/${{ env.PREVIEW_PUBLISH_REPOSITORY }}
publish_dir: ./artifact
publish_branch: ${{ env.PREVIEW_PUBLISH_BRANCH }}
destination_dir: preview/pr/${{ env.PR_NUMBER }}/${{ env.PR_SHA}}
commit_message: "deploy documentation preview to GitHub Pages for PR #${{ env.PR_NUMBER }}"

- name: Comment PR with preview link
run: |
PREVIEW_URL="https://${PREVIEW_PUBLISH_USERNAME}.github.io/${PREVIEW_PUBLISH_REPOSITORY}/preview/pr/${PR_NUMBER}/${PR_SHA}/index.html"
curl -s --request POST \
--url "https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
--header "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
--header "Content-Type: application/json" \
--data "{\"body\":\"🚀 Documentation preview: ${PREVIEW_URL}\"}"

0 comments on commit 190f2ca

Please sign in to comment.