This repository was archived by the owner on Apr 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
138 lines (128 loc) · 6.02 KB
/
Copy pathupdate-wiki.yml
File metadata and controls
138 lines (128 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
name: Update GitHub Wiki
on:
push:
branches: [ main ]
paths: [ 'docs/wiki/**' ]
workflow_dispatch: {}
jobs:
publish-wiki:
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: true
- name: Print event info
run: |
echo "GITHUB_EVENT_NAME=$GITHUB_EVENT_NAME"
echo "GITHUB_REF=$GITHUB_REF"
echo "GITHUB_SHA=$GITHUB_SHA"
echo "Event payload path: $GITHUB_EVENT_PATH"
if [ -f "$GITHUB_EVENT_PATH" ]; then echo "--- event payload ---"; cat "$GITHUB_EVENT_PATH"; echo "--- end payload ---"; fi
- name: List changed files (best-effort)
id: list_changed
run: |
git fetch --no-tags --prune --depth=5 origin "${{ github.ref }}" || true
echo "Files changed in this commit/PR:"
git --no-pager show --name-only --pretty="" "${{ github.sha }}" || git ls-files | sed -n '1,200p'
- name: Extract docs markdown files
id: docs_files
run: |
files=$(git --no-pager show --name-only --pretty="" "${{ github.sha }}" || git ls-files)
docs_md=$(echo "$files" | grep -iE '^docs/.*\.md$' || true)
if [ -z "$docs_md" ]; then
echo "docs_files=[]" >> "$GITHUB_OUTPUT"
else
arr=$(echo "$docs_md" | jq -R -s -c 'split("\n")[:-1]')
echo "docs_files=$arr" >> "$GITHUB_OUTPUT"
fi
- name: Add changed files to job summary
if: always()
run: |
echo "### Changed files" >> "$GITHUB_STEP_SUMMARY"
git --no-pager show --name-only --pretty="" "${{ github.sha }}" >> "$GITHUB_STEP_SUMMARY" || git ls-files | sed -n '1,200p' >> "$GITHUB_STEP_SUMMARY"
- name: Select token
id: select_token
run: |
if [ -n "${{ secrets.DEPLOY_WIKI_TOKEN }}" ]; then
echo "token=${{ secrets.DEPLOY_WIKI_TOKEN }}" >> "$GITHUB_OUTPUT"
echo "source=DEPLOY_WIKI_TOKEN" >> "$GITHUB_OUTPUT"
else
echo "token=${{ secrets.GITHUB_TOKEN }}" >> "$GITHUB_OUTPUT"
echo "source=GITHUB_TOKEN" >> "$GITHUB_OUTPUT"
fi
echo "Using token from: ${{ steps.select_token.outputs.source }}"
- name: Publish docs/wiki to GitHub Wiki
env:
GITHUB_TOKEN: ${{ steps.select_token.outputs.token }}
REPO: ${{ github.repository }}
run: |
set -e
if [ ! -d docs/wiki ]; then
echo "docs/wiki not found"; exit 0
fi
TMP_DIR="$(mktemp -d)"
WIKI_URL="https://x-access-token:${GITHUB_TOKEN}@github.com/${REPO}.wiki.git"
echo "Attempting to clone wiki repo"
if git clone "$WIKI_URL" "$TMP_DIR"; then
echo "Cleaning wiki repo (preserving .git)"
find "$TMP_DIR" -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} +
else
echo "Wiki repo not found. Auto-initializing..."
mkdir -p "$TMP_DIR"
git init "$TMP_DIR"
git -C "$TMP_DIR" remote add origin "$WIKI_URL"
touch "$TMP_DIR/.keep"
git -C "$TMP_DIR" add -A
git -C "$TMP_DIR" config user.name "${GITHUB_ACTOR:-github-actions}"
git -C "$TMP_DIR" config user.email "${GITHUB_ACTOR:-github-actions}@users.noreply.github.com"
git -C "$TMP_DIR" commit -m "init wiki"
git -C "$TMP_DIR" push -u origin HEAD:master || git -C "$TMP_DIR" push -u origin HEAD:main || git -C "$TMP_DIR" push -u origin HEAD
echo "Wiki repo initialized."
fi
echo "Copying markdown from docs/wiki"
rsync -a --prune-empty-dirs --include='*/' --include='*.md' --exclude='*' docs/wiki/ "$TMP_DIR"/
cd "$TMP_DIR"
git config user.name "${GITHUB_ACTOR:-github-actions}"
git config user.email "${GITHUB_ACTOR:-github-actions}@users.noreply.github.com"
if [ -n "$(git status --porcelain)" ]; then
git add -A
git commit -m "Publish docs/wiki to GitHub Wiki"
git push origin HEAD:master || git push origin HEAD:main || git push
echo "Published changes to GitHub Wiki"
else
echo "No changes to publish."
fi
- name: Comment on PR with changed docs files
if: github.event_name == 'pull_request' && steps.docs_files.outputs.docs_files != '[]'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCS_FILES: ${{ steps.docs_files.outputs.docs_files }}
run: |
pr_number=$(jq -r '.pull_request.number // empty' "$GITHUB_EVENT_PATH")
head_sha=$(jq -r '.pull_request.head.sha // empty' "$GITHUB_EVENT_PATH")
if [ -z "$pr_number" ] || [ -z "$head_sha" ]; then
echo "No PR number or head SHA found in event payload; skipping comment."
exit 0
fi
owner=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f1)
repo=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2)
files_json="$DOCS_FILES"
body="### Docs files changed\n\n"
body+=$(echo "$files_json" | jq -r --arg repo "$owner/$repo" --arg sha "$head_sha" '.[] | "- [\(. )](https://github.com/" + $repo + "/blob/" + $sha + "/" + .)')
payload=$(jq -n --arg body "$body" '{body: $body}')
curl -s -S -H "Authorization: token $GITHUB_TOKEN" -H "Accept: application/vnd.github+json" \
-d "$payload" "https://api.github.com/repos/$owner/$repo/issues/$pr_number/comments"
- name: Post final publish comment
env:
GITHUB_TOKEN: ${{ steps.select_token.outputs.token }}
DOCS_FILES: ${{ steps.docs_files.outputs.docs_files }}
run: |
owner=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f1)
repo=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2)
echo "Wiki: https://github.com/$owner/$repo/wiki"
echo "Run: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"