Skip to content

Commit c1b6082

Browse files
rohanKanojiapraveenkumar
authored andcommitted
ci : Add github action workflow to generate documentation preview (#2)
Add github action to generate documentation preview by publishing gh-pages on an external repository. This change adds four new github action workflows: - Documentation Preview Request: A workflow that would run on the pull request and generate website and upload it as a github artifact. - Documentation Preview Generator: A workflow that would get triggered on the completion of `Documentation Preview Request` workflow and it would download the uploaded artifact and deploy the content to github repository configured in secrets. - Documentation Preview Cleanup Request: A workflow that would run on pull request upon the closing event of pull request. It would save pull request number as an artifact and upload it. - Documentation Preview Cleanup: A workflow that would get triggered on the completion of `Documentation Preview Cleanup Request` workflow and it would download the uploaded artifact and update the github repository and remove the generated preview directory. Signed-off-by: Rohan Kumar <rohaan@redhat.com>
1 parent 590aebb commit c1b6082

4 files changed

+144
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Documentation Preview Cleanup Request
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
8+
jobs:
9+
on-close:
10+
runs-on: ubuntu-24.04
11+
12+
steps:
13+
- name: Save the GH context in an artifact
14+
env:
15+
GH_CONTEXT: ${{ toJSON(github) }}
16+
run: echo $GH_CONTEXT > github_context.json
17+
- name: Upload the GH context artifact
18+
uses: actions/upload-artifact@v4
19+
with:
20+
name: github-context
21+
path: github_context.json
22+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Documentation Preview Cleanup
2+
3+
on:
4+
workflow_run:
5+
workflows: [Documentation Preview Cleanup Request]
6+
types:
7+
- completed
8+
9+
env:
10+
PREVIEW_PUBLISH_USERNAME: crc-org
11+
PREVIEW_PUBLISH_REPOSITORY: crc-docs-gh-action-generated-preview
12+
PREVIEW_PUBLISH_BRANCH: gh-pages
13+
PREVIEW_PUBLISH_TOKEN: ${{ secrets.PREVIEW_PUBLISH_TOKEN }}
14+
15+
jobs:
16+
on-close:
17+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
18+
runs-on: ubuntu-24.04
19+
20+
steps:
21+
- name: Download artifact
22+
uses: actions/download-artifact@v4
23+
with:
24+
name: cleanup-info
25+
path: .
26+
run-id: ${{ github.event.workflow_run.id }}
27+
github-token: ${{ github.token }}
28+
- name: Configure Git
29+
run: |
30+
git config --global user.name "github-actions[bot]"
31+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
32+
- name: Extract Pull Request ID from artifact
33+
run: |
34+
echo "PR_NUMBER=$(jq '.event.pull_request.number' github_context.json)" >> $GITHUB_ENV
35+
rm github_context.json
36+
- name: Push updates to external repository
37+
run: |
38+
git clone https://${PREVIEW_PUBLISH_USERNAME}:${PREVIEW_PUBLISH_TOKEN}@github.com/${PREVIEW_PUBLISH_USERNAME}/${PREVIEW_PUBLISH_REPOSITORY}.git -b ${PREVIEW_PUBLISH_BRANCH}
39+
cd ${PREVIEW_PUBLISH_REPOSITORY}
40+
rm -rf preview/pr/${PR_NUMBER}
41+
git add preview/pr/${PR_NUMBER}
42+
git commit -am "cleanup (preview) : remove preview for Pull Request #${PR_NUMBER}"
43+
git push origin ${PREVIEW_PUBLISH_BRANCH}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Documentation Preview Request
2+
3+
on:
4+
pull_request: {}
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-24.04
9+
10+
steps:
11+
- name: Check out repository code
12+
uses: actions/checkout@v4
13+
- name: Build docs
14+
run: CI=true make build_docs
15+
- name: Check links in docs
16+
run: make docs_check_links
17+
- name: Create robots.txt
18+
run: |
19+
echo "User-agent: *" > ./public/robots.txt
20+
echo "Disallow: /" >> ./public/robots.txt
21+
- name: Save the GH context in an artifact
22+
env:
23+
GH_CONTEXT: ${{ toJSON(github) }}
24+
run: echo $GH_CONTEXT > ./public/github_context.json
25+
- name: Upload GitHub artifact
26+
uses: actions/upload-artifact@v4
27+
with:
28+
name: documentation
29+
path: public
30+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Documentation Preview Generator
2+
3+
on:
4+
workflow_run:
5+
workflows: [Documentation Preview Request]
6+
types:
7+
- completed
8+
9+
env:
10+
PREVIEW_PUBLISH_USERNAME: crc-org
11+
PREVIEW_PUBLISH_REPOSITORY: crc-docs-gh-action-generated-preview
12+
PREVIEW_PUBLISH_BRANCH: gh-pages
13+
PREVIEW_PUBLISH_TOKEN: ${{ secrets.PREVIEW_PUBLISH_TOKEN }}
14+
15+
jobs:
16+
documentation-preview:
17+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
18+
runs-on: ubuntu-24.04
19+
steps:
20+
- name: Download artifact
21+
uses: actions/download-artifact@v4
22+
with:
23+
name: documentation
24+
path: ./artifact
25+
run-id: ${{ github.event.workflow_run.id }}
26+
github-token: ${{ github.token }}
27+
- name: Extract Pull Request ID from artifact
28+
run: |
29+
echo "PR_NUMBER=$(jq '.event.pull_request.number' ./artifact/github_context.json)" >> $GITHUB_ENV
30+
echo "PR_SHA=$(jq -r '.event.pull_request.head.sha' ./artifact/github_context.json | cut -c 1-7)" >> $GITHUB_ENV
31+
rm ./artifact/github_context.json
32+
- name: Deploy to GitHub Pages
33+
uses: peaceiris/actions-gh-pages@v4
34+
with:
35+
personal_token: ${{ env.PREVIEW_PUBLISH_TOKEN }}
36+
external_repository: ${{ env.PREVIEW_PUBLISH_USERNAME }}/${{ env.PREVIEW_PUBLISH_REPOSITORY }}
37+
publish_dir: ./artifact
38+
publish_branch: ${{ env.PREVIEW_PUBLISH_BRANCH }}
39+
destination_dir: preview/pr/${{ env.PR_NUMBER }}/${{ env.PR_SHA}}
40+
commit_message: "deploy documentation preview to GitHub Pages for PR #${{ env.PR_NUMBER }}"
41+
42+
- name: Comment PR with preview link
43+
run: |
44+
PREVIEW_URL="https://${PREVIEW_PUBLISH_USERNAME}.github.io/${PREVIEW_PUBLISH_REPOSITORY}/preview/pr/${PR_NUMBER}/${PR_SHA}/index.html"
45+
curl -s --request POST \
46+
--url "https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
47+
--header "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
48+
--header "Content-Type: application/json" \
49+
--data "{\"body\":\"🚀 Documentation preview: ${PREVIEW_URL}\"}"

0 commit comments

Comments
 (0)