Skip to content

Commit 018dc20

Browse files
Jimmy Chuark0f
authored andcommitted
Script for publishing rustdocs at gh-pages (paritytech#10604)
* wip Signed-off-by: Jimmy Chu <[email protected]> * Added the rustdocs-release script Signed-off-by: Jimmy Chu <[email protected]> * updated gitlab-ci script Signed-off-by: Jimmy Chu <[email protected]> * Updated to set NIGHTLY to false by default Signed-off-by: Jimmy Chu <[email protected]> * Added documentation Signed-off-by: Jimmy Chu <[email protected]>
1 parent 6e9cc80 commit 018dc20

File tree

3 files changed

+269
-27
lines changed

3 files changed

+269
-27
lines changed

.gitlab-ci.yml

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -747,13 +747,7 @@ publish-rustdoc:
747747
image: node:16
748748
variables:
749749
GIT_DEPTH: 100
750-
# --- Following variables are for rustdocs deployment ---
751-
# Space separated values of branches and tags to generate rustdocs
752-
RUSTDOCS_DEPLOY_REFS: "master monthly-2021-09+1 monthly-2021-08 v3.0.0"
753-
# Location of the docs index template
754-
INDEX_TPL: ".maintain/docs-index-tpl.ejs"
755-
# Where the `/latest` symbolic link links to. One of the $RUSTDOCS_DEPLOY_REFS value.
756-
LATEST: "monthly-2021-09+1"
750+
RUSTDOCS_DEPLOY_REFS: "master"
757751
rules:
758752
- if: $CI_PIPELINE_SOURCE == "pipeline"
759753
when: never
@@ -783,34 +777,18 @@ publish-rustdoc:
783777
- git config remote.origin.url "[email protected]:/paritytech/${CI_PROJECT_NAME}.git"
784778
- git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
785779
- git fetch origin gh-pages
786-
# Install `ejs` and generate index.html based on RUSTDOCS_DEPLOY_REFS
787-
- yarn global add ejs
788-
- 'ejs ${INDEX_TPL} -i "{\"deploy_refs\":\"${RUSTDOCS_DEPLOY_REFS}\",\"repo_name\":\"${CI_PROJECT_NAME}\",\"latest\":\"${LATEST}\"}" > /tmp/index.html'
789780
# Save README and docs
790781
- cp -r ./crate-docs/ /tmp/doc/
791782
- cp README.md /tmp/doc/
792783
- git checkout gh-pages
793-
# Remove directories no longer necessary, as specified in $RUSTDOCS_DEPLOY_REFS.
794-
# Also ensure $RUSTDOCS_DEPLOY_REFS is not just empty spaces.
795-
# Even though this block spans multiple lines, they are concatenated to run as a single line
796-
# command, so note for the semi-colons in the inner-most code block.
797-
- if [[ ! -z ${RUSTDOCS_DEPLOY_REFS// } ]]; then
798-
for FILE in *; do
799-
if [[ ! " $RUSTDOCS_DEPLOY_REFS " =~ " $FILE " ]]; then
800-
echo "Removing ${FILE}...";
801-
rm -rf $FILE;
802-
fi
803-
done
804-
fi
805-
# Move the index page & built back
806-
- mv -f /tmp/index.html .
784+
# Install `index-tpl-crud` and generate index.html based on RUSTDOCS_DEPLOY_REFS
785+
- which index-tpl-crud &> /dev/null || yarn global add @jimmychu0807/index-tpl-crud
786+
- index-tpl-crud upsert ./index.html ${CI_COMMIT_REF_NAME}
807787
# Ensure the destination dir doesn't exist.
808788
- rm -rf ${CI_COMMIT_REF_NAME}
809789
- mv -f /tmp/doc ${CI_COMMIT_REF_NAME}
810-
# Add the symlink
811-
- '[[ -e "$LATEST" ]] && ln -sf "${LATEST}" latest'
812790
# Upload files
813-
- git add --all --force
791+
- git add --all
814792
# `git commit` has an exit code of > 0 if there is nothing to commit.
815793
# This causes GitLab to exit immediately and marks this job failed.
816794
# We don't want to mark the entire job failed if there's nothing to

.maintain/rustdocs-release.sh

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
#!/usr/bin/env bash
2+
# set -x
3+
4+
# This script manages the deployment of Substrate rustdocs to https://paritytech.github.io/substrate/.
5+
# - With `deploy` sub-command, it will checkout the passed-in branch/tag ref, build the rustdocs
6+
# locally (this takes some time), update the `index.html` index page, and push it to remote
7+
# `gh-pages` branch. So users running this command need to have write access to the remote
8+
# `gh-pages` branch. This sub-command depends on [@jimmychu0807/index-tpl-crud](https://www.npmjs.com/package/@jimmychu0807/index-tpl-crud)
9+
# to update the DOM of index.html page.
10+
# - With `remove` sub-command, it will remove the deployed rustdocs from `gh-pages`, and update the
11+
# index.html page as necessary. It may remove the `latest` symbolic link.
12+
#
13+
# Examples:
14+
# # Showing help text
15+
# rustdocs-release.sh -h
16+
#
17+
# # Deploy rustdocs of `monthly-2021-10` tag
18+
# rustdocs-release.sh deploy monthly-2021-10
19+
#
20+
# # In addition to the above, the `latest` symlink will point to this version of rustdocs
21+
# rustdocs-release.sh deploy -l monthly-2021-10
22+
#
23+
# # Remove the rustdocs of `monthly-2021-10` from `gh-pages`.
24+
# rustdocs-release.sh remove monthly-2021-10
25+
#
26+
# Dependencies:
27+
# - @jimmychu0807/index-tpl-crud - https://www.npmjs.com/package/@jimmychu0807/index-tpl-crud
28+
#
29+
30+
# Script setting
31+
# The git repo http URL
32+
REMOTE_REPO="https://github.com/paritytech/substrate.git"
33+
TMP_PREFIX="/tmp" # tmp location that the built doc is copied to.
34+
DOC_INDEX_PAGE="sc_service/index.html"
35+
36+
# Set to `true` if using cargo `nightly` toolchain to build the doc.
37+
# Set to `false` to use the default cargo toolchain. This is preferred if you want to build
38+
# the rustdocs with a pinned nightly version set to your default toolchain.
39+
CARGO_NIGHTLY=false
40+
41+
# Set the git remote name. Most of the time the default is `origin`.
42+
GIT_REMOTE="origin"
43+
LATEST=false
44+
45+
# Setting the help text
46+
declare -A HELP_TXT
47+
HELP_TXT["deploy"]=$(cat <<-EOH
48+
Build and deploy the rustdocs of the specified branch/tag to \`gh-pages\` branch.
49+
50+
usage: $0 deploy [-l] <git_branch_ref>
51+
example: $0 deploy -l monthly-2021-10
52+
53+
options:
54+
-l The \`latest\` path will be sym'linked to this rustdocs version
55+
EOH
56+
)
57+
58+
HELP_TXT["remove"]=$(cat <<-EOH
59+
Remove the rustdocs of the specified version from \`gh-pages\` branch.
60+
61+
usage: $0 remove <git_branch_ref>
62+
example: $0 remove monthly-2021-10
63+
EOH
64+
)
65+
66+
set_and_check_rustdoc_ref() {
67+
[[ -z "$1" ]] && {
68+
echo -e "git branch_ref is not specified.\n"
69+
echo "${HELP_TXT[$2]}"
70+
exit 1
71+
}
72+
BUILD_RUSTDOC_REF=$1
73+
}
74+
75+
check_local_change() {
76+
# Check there is no local changes before proceeding
77+
[[ -n $(git status --porcelain) ]] \
78+
&& echo "Local changes exist, please either discard or commit them as this command will change the current checkout branch." \
79+
&& exit 1
80+
}
81+
82+
build_rustdocs() {
83+
# Build the docs
84+
time cargo $($CARGO_NIGHTLY && echo "+nightly") doc --workspace --all-features --verbose \
85+
|| { echo "Generate $1 rustdocs failed" && exit 1; }
86+
rm -f target/doc/.lock
87+
88+
# Moving the built doc to the tmp location
89+
mv target/doc "${2}"
90+
[[ -n "${DOC_INDEX_PAGE}" ]] \
91+
&& echo "<meta http-equiv=refresh content=0;url=${DOC_INDEX_PAGE}>" > "${2}/index.html"
92+
}
93+
94+
upsert_index_page() {
95+
# Check if `index-tpl-crud` exists
96+
which index-tpl-crud &> /dev/null || yarn global add @jimmychu0807/index-tpl-crud
97+
index-tpl-crud upsert $($1 && echo "-l") ./index.html "$2"
98+
}
99+
100+
rm_index_page() {
101+
which index-tpl-crud &> /dev/null || yarn global add @jimmychu0807/index-tpl-crud
102+
index-tpl-crud rm ./index.html "$1"
103+
}
104+
105+
git_add_commit_push() {
106+
git add --all
107+
git commit -m "$1" || echo "Nothing to commit"
108+
git push "${GIT_REMOTE}" gh-pages --force
109+
}
110+
111+
import_gh_key() {
112+
[[ -n $GITHUB_SSH_PRIV_KEY ]] && {
113+
eval $(ssh-agent)
114+
ssh-add - <<< $GITHUB_SSH_PRIV_KEY
115+
}
116+
117+
# Adding github.com as known_hosts
118+
ssh-keygen -F github.com &>/dev/null || {
119+
[[ -e ~/.ssh ]] || mkdir ~/.ssh
120+
[[ -e ~/.ssh/known_hosts ]] || touch ~/.ssh/known_hosts
121+
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
122+
}
123+
}
124+
125+
deploy_main() {
126+
check_local_change
127+
import_gh_key
128+
129+
CURRENT_GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
130+
TMP_PROJECT_PATH="${TMP_PREFIX}/${PROJECT_NAME}"
131+
DOC_PATH="${TMP_PROJECT_PATH}/${BUILD_RUSTDOC_REF}"
132+
133+
# Build the tmp project path
134+
rm -rf "${TMP_PROJECT_PATH}" && mkdir "${TMP_PROJECT_PATH}"
135+
136+
# Copy .gitignore file to tmp
137+
[[ -e "${PROJECT_PATH}/.gitignore" ]] && cp "${PROJECT_PATH}/.gitignore" "${TMP_PROJECT_PATH}"
138+
139+
git fetch --all
140+
git checkout -f ${BUILD_RUSTDOC_REF} || { echo "Checkout \`${BUILD_RUSTDOC_REF}\` error." && exit 1; }
141+
build_rustdocs "${BUILD_RUSTDOC_REF}" "${DOC_PATH}"
142+
143+
# git checkout `gh-pages` branch
144+
git fetch "${GIT_REMOTE}" gh-pages
145+
git checkout gh-pages
146+
# Move the built back
147+
[[ -e "${TMP_PROJECT_PATH}/.gitignore" ]] && cp -f "${TMP_PROJECT_PATH}/.gitignore" .
148+
# Ensure the destination dir doesn't exist under current path.
149+
rm -rf "${BUILD_RUSTDOC_REF}"
150+
mv -f "${DOC_PATH}" "${BUILD_RUSTDOC_REF}"
151+
152+
upsert_index_page $LATEST "${BUILD_RUSTDOC_REF}"
153+
# Add the latest symlink
154+
$LATEST && rm -rf latest && ln -sf "${BUILD_RUSTDOC_REF}" latest
155+
156+
git_add_commit_push "___Deployed rustdocs of ${BUILD_RUSTDOC_REF}___"
157+
# Clean up
158+
# Remove the tmp asset created
159+
rm -rf "${TMP_PROJECT_PATH}"
160+
# Resume back previous checkout branch.
161+
git checkout -f "$CURRENT_GIT_BRANCH"
162+
}
163+
164+
remove_main() {
165+
check_local_change
166+
import_gh_key
167+
168+
CURRENT_GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
169+
170+
# git checkout `gh-pages` branch
171+
git fetch "${GIT_REMOTE}" gh-pages
172+
git checkout gh-pages
173+
174+
rm -rf "${BUILD_RUSTDOC_REF}"
175+
rm_index_page "${BUILD_RUSTDOC_REF}"
176+
# check if the destination of `latest` exists and rmove if not.
177+
[[ -e "latest" ]] || rm latest
178+
179+
git_add_commit_push "___Removed rustdocs of ${BUILD_RUSTDOC_REF}___"
180+
181+
# Resume back previous checkout branch.
182+
git checkout -f "$CURRENT_GIT_BRANCH"
183+
}
184+
185+
# ---- The script execution entry point starts here ----
186+
187+
# Arguments handling
188+
SUBCMD=$1
189+
[[ $SUBCMD == "deploy" || $SUBCMD == "remove" ]] \
190+
|| { echo "Please specify a subcommand of \`deploy\` or \`remove\`" && exit 1 ;}
191+
shift
192+
193+
# After removing the subcommand, there could only be 1 or 2 parameters afterward
194+
[[ $# -lt 1 || $# -gt 2 ]] && {
195+
echo "${HELP_TXT[${SUBCMD}]}"
196+
exit 1
197+
}
198+
199+
# Parsing options and argument for `deploy` subcommand
200+
[[ $SUBCMD == "deploy" ]] && {
201+
while getopts :lh opt; do
202+
case $opt in
203+
l)
204+
LATEST=true
205+
;;
206+
h)
207+
echo "${HELP_TXT[$SUBCMD]}"
208+
exit 0
209+
;;
210+
\?)
211+
echo "Invalid option: -$OPTARG" >&2
212+
exit 1
213+
;;
214+
esac
215+
done
216+
}
217+
# Parsing options and argument for `remove` subcommand
218+
[[ $SUBCMD == "remove" ]] && {
219+
while getopts :h opt; do
220+
case $opt in
221+
h)
222+
echo "${HELP_TXT[$SUBCMD]}"
223+
exit 0
224+
;;
225+
\?)
226+
echo "Invalid option: -$OPTARG" >&2
227+
exit 1
228+
;;
229+
esac
230+
done
231+
}
232+
233+
shift $(($OPTIND - 1))
234+
set_and_check_rustdoc_ref ${1:-''} $SUBCMD
235+
236+
SCRIPT=$(realpath $0)
237+
SCRIPT_PATH=$(dirname $SCRIPT)
238+
PROJECT_PATH=$(dirname ${SCRIPT_PATH})
239+
PROJECT_NAME=$(basename "$PROJECT_PATH")
240+
241+
pushd "${PROJECT_PATH}" &>/dev/null
242+
[[ $SUBCMD == "deploy" ]] && deploy_main
243+
[[ $SUBCMD == "remove" ]] && remove_main
244+
popd &>/dev/null

docs/rustdocs-release.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Rustdocs Release Process
2+
3+
There is [a script in place](../.maintain/rustdocs-release.sh) to manage the deployment of Substrate rustdocs at
4+
https://paritytech.github.io/substrate, which is pushing the rustdocs file in `gh-pages` branch of
5+
https://github.com/paritytech/substrate.
6+
7+
The documentation at the top of the `rustdocs-release.sh` explains most of the mechanics of the script.
8+
9+
Manage the rustdocs deployment with one of the following commands.
10+
11+
```bash
12+
# Deploy rustdocs of `monthly-2021-10` tag
13+
.maintain/rustdocs-release.sh deploy monthly-2021-10
14+
15+
# In addition to the above, the `latest` symlink will point to this version of rustdocs
16+
.maintain/rustdocs-release.sh deploy -l monthly-2021-10
17+
18+
# Remove the rustdocs of `monthly-2021-10` from `gh-pages`.
19+
.maintain/rustdocs-release.sh remove monthly-2021-10
20+
```

0 commit comments

Comments
 (0)