forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
166 lines (148 loc) · 7.64 KB
/
documentation.yml
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
on:
workflow_dispatch:
inputs:
artifacts_url:
required: true
type: string
description: 'The url to the artifacts that contain the docs to publish.'
artifact_name:
required: false
type: string
default: cuda_quantum_docs
description: 'The name of the artifact that contain the docs to publish.'
version:
required: true
type: string
description: 'The version that the documentation corresponds to, e.g. 0.3.1 or latest.'
workflow_run:
workflows:
- Deployments
- CI
types:
- completed
name: Documentation
concurrency:
group: ${{ github.workflow }}${{ github.event.workflow_run.name }} # only one docs publishing can be run at a time, since all docs are published to the same location!
cancel-in-progress: false
jobs:
publish_docs:
name: Publish documentation
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.name == 'CI' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.actor.id == '135836288')
permissions:
contents: write
issues: write
pull-requests: write
environment:
name: ${{ (github.event.workflow_run.name == 'CI' && 'default') || 'github-pages' }}
url: ${{ vars.deployment_url || format('https://github.com/{0}', github.repository) }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ (github.event.workflow_run.name == 'CI' && vars.preview_branch) || vars.live_branch }}
token: ${{ (github.event.workflow_run.name == 'CI' && github.token) || secrets.REPO_BOT_ACCESS_TOKEN }}
- name: Download artifacts
id: artifacts
run: |
artifacts_url=${{ (github.event_name == 'workflow_dispatch' && inputs.artifacts_url) || github.event.workflow_run.artifacts_url }}
artifacts=$(gh api $artifacts_url -q '.artifacts[] | {name: .name, url: .archive_download_url}')
artifact_name=${{ (github.event_name == 'workflow_dispatch' && inputs.artifact_name) || 'cuda_quantum_docs' }}
docs_found=${{ github.event.workflow_run.name == 'CI' || false }}
for artifact in `echo "$artifacts"`; do
name=`echo $artifact | jq -r '.name'`
if [ "$name" == "$artifact_name" ]; then
url=`echo $artifact | jq -r '.url'`
gh api $url > cuda_quantum_docs.zip
docs_found=true && echo "docs_archive=cuda_quantum_docs.zip" >> $GITHUB_OUTPUT
elif [ "$name" == "metadata_ci" ]; then
url=`echo $artifact | jq -r '.url'`
gh api $url > metadata.zip
unzip -d metadata metadata.zip
for file in `find metadata/ -type f`; do
cat "$file" >> metadata.txt
done
rm -rf metadata metadata.zip
fi
done
run_id=`echo $artifacts_url | rev | cut -d / -f 2 | rev`
workflow_run=https://github.com/${{ github.repository }}/actions/runs/$run_id
if $docs_found; then
echo "Downloaded $artifact_name artifact from $workflow_run." >> $GITHUB_STEP_SUMMARY
else
echo "::error file=documentation.yml::Failed to find $artifact_name artifact from $workflow_run."
exit 1;
fi
if [ -f metadata.txt ]; then
pr_number=`cat metadata.txt | grep -o 'pr-number: \S*' | cut -d ' ' -f 2`
pr_base=`cat metadata.txt | grep -o 'pr-base: \S*' | cut -d ' ' -f 2`
rm metadata.txt && echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
fi
if ${{ github.event_name == 'workflow_dispatch' }}; then
echo "target_folder=${{ inputs.version }}" >> $GITHUB_OUTPUT
else
# Docs will be checked in under a different folder in the preview.
head_branch=${pr_base:-${{ github.event.workflow_run.head_branch }}}
if [ "$head_branch" == "main" ]; then
echo "target_folder=latest" >> $GITHUB_OUTPUT
else
version=`echo ${head_branch#releases/} | (egrep -o "^v?([0-9]{1,}\.)+[0-9]{1,}$" || true)`
echo "target_folder=${version#v}" >> $GITHUB_OUTPUT
if [ -z "${version:-$pr_number}" ]; then
echo "Unrecognized docs version. Documentation will not be published." >> $GITHUB_STEP_SUMMARY
fi
fi
fi
env:
GH_TOKEN: ${{ github.token }}
- name: Push docs update
id: push_docs
if: steps.artifacts.outputs.docs_archive != '' && (steps.artifacts.outputs.target_folder != '' || steps.artifacts.outputs.pr_number != '')
run: |
pr_number=${{ steps.artifacts.outputs.pr_number }}
target_folder=${pr_number:+pr-$pr_number}
target_folder="${target_folder:-${{ steps.artifacts.outputs.target_folder }}}"
rm -rf "$target_folder"
unzip -d "$target_folder" "${{ steps.artifacts.outputs.docs_archive }}"
git config --global user.name "cuda-quantum-bot"
git config --global user.email "[email protected]"
if [ -f create_redirects.sh ]; then
bash create_redirects.sh >> $GITHUB_STEP_SUMMARY
fi
git add "$target_folder"
docs_changed=`git diff --cached --exit-code > /dev/null && echo false || echo true`
if ${{ github.event_name == 'workflow_dispatch' }}; then
git commit -m "Docs update triggered manually for version ${{ inputs.version }} (artifacts url: ${{ inputs.artifacts_url }}, artifact name: ${{ inputs.artifact_name }})."
elif [ -n "$pr_number" ]; then
git commit --allow-empty -m "Docs preview for PR #$pr_number."
else
git commit --allow-empty -m "Docs update triggered by deployment on head branch ${{ github.event.workflow_run.head_branch }}, commit ${{ github.event.workflow_run.head_sha }}."
fi
git config pull.rebase true
git pull --no-edit && git push
# For the docs preview, we determine whether docs changed compared to the eventual target folder.
if [ "$target_folder" != "${{ steps.artifacts.outputs.target_folder }}" ]; then
rm -rf "${{ steps.artifacts.outputs.target_folder }}"
mv "$target_folder" "${{ steps.artifacts.outputs.target_folder }}"
git add "${{ steps.artifacts.outputs.target_folder }}"
docs_changed=`git diff --cached --exit-code > /dev/null && echo false || echo true`
fi
pr_comment='**CUDA Quantum Docs Bot:** '
if $docs_changed; then
branch_name=`git rev-parse --abbrev-ref HEAD`
preview_link=https://htmlpreview.github.io/?https://github.com/${{ github.repository }}/blob/${branch_name}/${target_folder}/index.html
pr_comment+="A preview of the documentation can be found [here]($preview_link)."
echo "$pr_comment" >> $GITHUB_STEP_SUMMARY
else
pr_comment+='This PR contains no documentation changes.'
echo "No documentation changes." >> $GITHUB_STEP_SUMMARY
fi
echo "pr_comment=$pr_comment" >> $GITHUB_OUTPUT
- name: Comment on PR
if: steps.artifacts.outputs.pr_number != '' && steps.push_docs.outcome != 'skipped'
continue-on-error: true # PR may be locked by the time this workflow runs if auto-merge is set to true
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ steps.artifacts.outputs.pr_number }}
body: ${{ steps.push_docs.outputs.pr_comment }}
edit-mode: replace