-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
167 lines (158 loc) · 5.75 KB
/
action.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
167
name: 'Deploy mdbook docs'
description: 'GitHub Action to automatically build, test and deploy a versioned mdBook project to GitHub Pages.'
branding:
icon: 'book'
color: 'blue'
inputs:
github-token:
description: 'GitHub token for deployment.'
required: true
version:
type: string
description: "Version of the documentation to deploy"
required: false
default: "latest"
docs-dir:
type: string
description: "Directory containing the mdbook documentation"
required: false
default: "docs"
mdbook-version:
type: string
description: "Version of mdbook to use"
required: false
default: "latest"
enable-tests:
type: boolean
description: "Disable mdbook tests"
required: false
default: true
publish-branch:
type: string
description: "Branch to publish the documentation"
required: false
default: "gh-pages"
project:
type: string
description: "Name of the project to deploy"
required: false
default: ""
deploy:
type: boolean
description: "Enable or disable the deployment"
required: false
default: true
runs:
using: composite
steps:
- name: Check runner OS
if: runner.os != 'Linux'
shell: 'bash -ex {0}'
run: |
echo "::error title=⛔ error hint::Only Linux is supported for this action."
exit 1
- name: Install latest mdbook
shell: 'bash -ex {0}'
env:
MDBOOK_LATEST_API: "https://api.github.com/repos/rust-lang/mdbook/releases/${{ inputs.mdbook-version || 'latest' }}"
MDBOOK_DOWNLOAD_URL: "https://github.com/rust-lang/mdbook/releases/download"
run: |
echo "::group::Install mdbook"
TAG=$(curl "${MDBOOK_LATEST_API}" | jq -r .tag_name)
URL="${MDBOOK_DOWNLOAD_URL}/${TAG}/mdbook-${TAG}-x86_64-unknown-linux-gnu.tar.gz"
MDBOOK_INSTALL_DIR="${PWD}/.mdbook_$(date +%s)"
mkdir -p "${MDBOOK_INSTALL_DIR}"
curl -sSL "${URL}" | tar -xz --directory="${MDBOOK_INSTALL_DIR}"
echo "${MDBOOK_INSTALL_DIR}" >> "${GITHUB_PATH}"
echo "::endgroup::"
- name: Build Book
shell: 'bash -ex {0}'
working-directory: ${{ inputs.docs-dir }}
id: build-book
run: |
echo "::group::Building the book"
if [ ${{ inputs.version }} == ${{ github.event.repository.default_branch }} ] || [ ${{ inputs.version }} == 'latest' ]; then
VERSION=latest
else
VERSION=${{ inputs.version }}
fi
mdbook build --dest-dir=${VERSION}
echo version=${VERSION} >> "${GITHUB_OUTPUT}"
echo "::endgroup::"
- name: Test Book
if: ${{ inputs.enable-tests == 'true' || inputs.enable-tests == true }}
shell: 'bash -ex {0}'
working-directory: ${{ inputs.docs-dir }}
run: |
echo "::group::Testing the book"
mdbook test
echo "::endgroup::"
- name: Init publishing branch
if: ${{ inputs.deploy == 'true' || inputs.deploy == true }}
shell: 'bash -ex {0}'
env:
GH_TOKEN: ${{ inputs.github-token }}
run: |
echo "::group::Initizalizing or updating publishing branch"
# Configure git
git config user.name "Deploy from CI"
git config user.email ""
# Check if the deploy branch exists
if [ $(gh api "/repos/${GITHUB_REPOSITORY}/branches/${{ inputs.publish-branch }}" > /dev/null 2>&1; echo $?) -eq 1 ]; then
git checkout --orphan ${{ inputs.publish-branch }}
git rm -rf .
git commit --allow-empty -m "Initialize ${{ inputs.publish-branch }} branch"
else
git fetch origin ${{ inputs.publish-branch }}
git checkout ${{ inputs.publish-branch }}
fi
echo "::endgroup::"
- name: Update versions file
if: ${{ inputs.deploy == 'true' || inputs.deploy == true }}
shell: 'bash -ex {0}'
env:
VERSION: ${{ steps.build-book.outputs.version }}
run: |
echo "::group::Updating versions file"
if [ ! -z "${{ inputs.project }}" ]; then
mkdir -p "${{ inputs.project }}"
cd "${{ inputs.project }}"
fi
JSON_FILE="versions.json"
# Check if the JSON file exists
if [ ! -f "${JSON_FILE}" ]; then
echo "${JSON_FILE} not found, creating..."
echo "{}" > "${JSON_FILE}"
fi
# Read the current JSON content
CURRENT_JSON=$(<"$JSON_FILE")
# Check if the new version already exists
if echo "${CURRENT_JSON}" | grep -q "\"${VERSION}\":"; then
echo "Updating docs for the existing version ${VERSION} in ${JSON_FILE}."
exit 0
fi
# Use jq to update the JSON file
UPDATED_JSON=$(echo "${CURRENT_JSON}" | jq --arg version "${VERSION}" --arg url "/${VERSION}" '. + {($version): $url}')
# Write the updated JSON back to the file
echo "${UPDATED_JSON}" > "${JSON_FILE}"
echo "Version ${VERSION} added successfully to ${JSON_FILE}."
cat "${JSON_FILE}"
echo "::endgroup::"
- name: Deploy book
if: ${{ inputs.deploy == 'true' || inputs.deploy == true }}
shell: 'bash -ex {0}'
env:
VERSION: ${{ steps.build-book.outputs.version }}
run: |
echo "::group::Deploying the book"
BOOK_DIR=$(readlink -f "${{ inputs.docs-dir }}/${VERSION}")
[ ! -z "${{ inputs.project }}" ] && cd "${{ inputs.project }}"
# Remove the previous version of the book
rm -rf ${VERSION}
# Move the new version of the book
mv "${BOOK_DIR}" "${VERSION}"
# Add the new version of the book
git add "${VERSION}" versions.json
git commit --amend -m "Deploy ${GITHUB_SHA::7}"
git push --force --set-upstream origin ${{ inputs.publish-branch }}
echo "::endgroup::"