Skip to content

Commit 381f052

Browse files
ci: deploy preview docs action (#66)
1 parent a6464bc commit 381f052

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: "Pin Comment"
2+
description: "Pins a comment by hiding it with HTML comments and a unique ID"
3+
4+
inputs:
5+
message:
6+
description: "The message to pin"
7+
required: true
8+
token:
9+
description: "GitHub token"
10+
required: true
11+
issue_number:
12+
description: "The issue or PR number"
13+
required: true
14+
comment_id:
15+
description: "Unique identifier for this pinned comment"
16+
required: true
17+
working_directory:
18+
description: "Path to the repository working directory"
19+
required: true
20+
21+
runs:
22+
using: "composite"
23+
steps:
24+
- name: Pin comment
25+
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
26+
env:
27+
GITHUB_TOKEN: ${{ inputs.token }}
28+
MESSAGE: ${{ inputs.message }}
29+
ISSUE_NUMBER: ${{ inputs.issue_number }}
30+
COMMENT_ID: ${{ inputs.comment_id }}
31+
WORKING_DIRECTORY: ${{ inputs.working_directory }}
32+
run: |
33+
# Change to working directory
34+
cd "${WORKING_DIRECTORY}"
35+
36+
# Create comment with hidden marker and ID
37+
COMMENT="<!--pin-comment-marker-${COMMENT_ID}-->
38+
${MESSAGE}"
39+
40+
# Properly escape the comment for JSON
41+
ESCAPED_COMMENT=$(echo "$COMMENT" | jq -R -s '.')
42+
43+
# Find and delete old comment with same ID using GitHub token
44+
echo "Searching for existing pinned comment with ID: ${COMMENT_ID}"
45+
response=$(curl -s -w "%{http_code}" -H "Authorization: token ${GITHUB_TOKEN}" \
46+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${ISSUE_NUMBER}/comments?per_page=100")
47+
status_code=${response: -3}
48+
comments=${response:0:-3}
49+
50+
if [ "$status_code" != "200" ]; then
51+
echo "Failed to fetch comments. Status code: ${status_code}"
52+
exit 1
53+
fi
54+
55+
old_comment_id=$(echo "$comments" | jq -r ".[] | select(.body | startswith(\"<!--pin-comment-marker-${COMMENT_ID}-->\")) | .id")
56+
57+
# Update old comment if found
58+
if [ ! -z "$old_comment_id" ]; then
59+
echo "Updating existing comment with ID: ${old_comment_id}"
60+
response=$(curl -s -w "%{http_code}" -X PATCH -H "Authorization: token ${GITHUB_TOKEN}" \
61+
-H "Content-Type: application/json" \
62+
-d "{\"body\":${ESCAPED_COMMENT}}" \
63+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/comments/${old_comment_id}")
64+
status_code=${response: -3}
65+
66+
if [ "$status_code" != "200" ]; then
67+
echo "Failed to update comment. Status code: ${status_code}"
68+
exit 1
69+
fi
70+
else
71+
# Create new pinned comment using GitHub API directly
72+
echo "Creating new pinned comment"
73+
response=$(curl -s -w "%{http_code}" -X POST -H "Authorization: token ${GITHUB_TOKEN}" \
74+
-H "Content-Type: application/json" \
75+
-d "{\"body\":${ESCAPED_COMMENT}}" \
76+
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${ISSUE_NUMBER}/comments")
77+
status_code=${response: -3}
78+
79+
if [ "$status_code" != "201" ]; then
80+
echo "Failed to create comment. Status code: ${status_code}"
81+
exit 1
82+
fi
83+
fi

.github/workflows/docs_preview.yml

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: Docs Preview
2+
3+
concurrency:
4+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
5+
cancel-in-progress: true
6+
7+
on:
8+
pull_request_target:
9+
branches:
10+
- main
11+
paths:
12+
- 'docs/**'
13+
- 'tools/github_readme_sync/**'
14+
- '.github/workflows/docs_preview.yml'
15+
16+
permissions:
17+
pull-requests: write
18+
19+
jobs:
20+
deploy_docs_preview:
21+
name: deploy-docs-preview
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout tbp.monty
25+
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
lfs: true
29+
path: tbp.monty
30+
31+
- name: Create initial PR comment
32+
uses: ./tbp.monty/.github/actions/pin_comment
33+
with:
34+
token: ${{ secrets.GITHUB_TOKEN }}
35+
issue_number: ${{ github.event.pull_request.number }}
36+
comment_id: docs-preview
37+
working_directory: tbp.monty
38+
message: |
39+
📚 **Documentation Preview**
40+
41+
Building documentation preview... ⏳
42+
43+
Check the [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for progress.
44+
- name: Set up ~/tbp
45+
run: |
46+
mkdir -p ~/tbp
47+
ln -s $GITHUB_WORKSPACE/tbp.monty ~/tbp/tbp.monty
48+
- name: Set up Python 3.8
49+
uses: actions/setup-python@v5
50+
with:
51+
python-version: "3.8"
52+
- name: Install miniconda
53+
run: |
54+
if [ ! -d ~/miniconda ]
55+
then
56+
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
57+
bash ~/miniconda.sh -b -p ~/miniconda
58+
rm ~/miniconda.sh
59+
fi
60+
export PATH="$HOME/miniconda/bin:$PATH"
61+
conda --version
62+
- name: Create conda environment
63+
working-directory: tbp.monty
64+
run: |
65+
export PATH="$HOME/miniconda/bin:$PATH"
66+
(conda env list | grep tbp.monty) && conda remove --name tbp.monty --all --yes || true
67+
conda env create
68+
source activate tbp.monty
69+
pip install -e .[dev,github_readme_sync_tool,print_version_tool]
70+
- name: Get version and branch
71+
working-directory: tbp.monty
72+
run: |
73+
export PATH="$HOME/miniconda/bin:$PATH"
74+
source activate tbp.monty
75+
echo "MONTY_VERSION=$(python -m tools.print_version.cli minor)" >> $GITHUB_ENV
76+
# Make branch name safe for semver by replacing invalid chars with dash
77+
# Remove trailing dash if present
78+
echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr -c '[:alnum:]' '-' | sed 's/-*$//')" >> $GITHUB_ENV
79+
# Get current date in ISO format
80+
echo "MONTY_DATE=$(date -u +'%Y-%m-%d %H:%M UTC')" >> $GITHUB_ENV
81+
- name: Deploy docs
82+
working-directory: tbp.monty
83+
run: |
84+
export PATH="$HOME/miniconda/bin:$PATH"
85+
source activate tbp.monty
86+
export README_API_KEY=${{ secrets.README_API_KEY }}
87+
export IMAGE_PATH=${{ vars.IMAGE_PATH }}
88+
python -m tools.github_readme_sync.cli upload docs "${MONTY_VERSION}-${BRANCH_NAME}"
89+
- name: Update PR comment on success
90+
if: success()
91+
uses: ./tbp.monty/.github/actions/pin_comment
92+
with:
93+
token: ${{ secrets.GITHUB_TOKEN }}
94+
issue_number: ${{ github.event.pull_request.number }}
95+
comment_id: docs-preview
96+
working_directory: tbp.monty
97+
message: |
98+
📚 **Documentation Preview**
99+
100+
✅ A preview of the documentation changes in this PR is available for maintainers at:
101+
${{ vars.DOCS_URL_PREFIX }}v${{ env.MONTY_VERSION }}-${{ env.BRANCH_NAME }}
102+
103+
Last updated: ${{ env.MONTY_DATE }}
104+
- name: Update PR comment on failure
105+
if: failure()
106+
uses: ./tbp.monty/.github/actions/pin_comment
107+
with:
108+
token: ${{ secrets.GITHUB_TOKEN }}
109+
issue_number: ${{ github.event.pull_request.number }}
110+
comment_id: docs-preview
111+
working_directory: tbp.monty
112+
message: |
113+
📚 **Documentation Preview**
114+
115+
❌ Failed to build documentation preview.
116+
117+
Check the [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details.

0 commit comments

Comments
 (0)