-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate
executable file
·156 lines (130 loc) · 4.21 KB
/
generate
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
#!/bin/bash
set -ex
if [ $# -ne 4 ]
then
echo "Usage: $0 <directory> <gitlab URL> <project NAME> <GitHub project NAME>" >&2
exit 1
fi
DEPLOY_DIR="${1}"
mkdir -p "${DEPLOY_DIR}"
cd "${DEPLOY_DIR}"
DEPLOY_DIR="${PWD}"
GITLAB_URL="$2"
PROJECT_NAME="$3"
GH_PROJECT_NAME="$4"
set +x
if [ -z ${CI_PAGES_TOKEN} ]
then
echo "CI_PAGES_TOKEN is unset"
exit 1
fi
set -ex
cd "$(mktemp -t -d WEB_DEPLOY.tmdir.XXXXXXXXXXXX || exit 1)"
WORK_DIR="${PWD}"
trap "cd '${DEPLOY_DIR}'; rm -rf '${WORK_DIR}'" EXIT
mkdir -p "${WORK_DIR}/public/"
cat << EOF > "${WORK_DIR}/public/index.html"
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>PDI: Documentation versions</title>
</head>
<body>
<h1>PDI documentation for:</h1>
<ul>
EOF
########################
## GITHUB
########################
DOWNLOAD_URL=$(curl -fsS -H "Authorization: Bearer ${CI_PAGES_TOKEN}" https://api.github.com/repos/${GH_PROJECT_NAME}/actions/artifacts \
| jq -r '.artifacts
| map(select(.workflow_run.head_branch=="main" and .name == "pages"))
| sort_by(.created_at)
| last
| .archive_download_url')
mkdir -p "${WORK_DIR}/public/main"
cd "${WORK_DIR}/public/main"
curl -H "Authorization: Bearer ${CI_PAGES_TOKEN}" -LfsSo artifacts.zip "${DOWNLOAD_URL}"
unzip -o -d public artifacts.zip
rm artifacts.zip
while [ 1 -eq "$(find . -mindepth 1 -maxdepth 1|wc -l)" ]
do
SUBDIR="$(find . -mindepth 1 -maxdepth 1)"
find "$SUBDIR" -mindepth 1 -maxdepth 1 -exec mv '{}' . ';'
rmdir "$SUBDIR"
done
echo "<li><a href='main/'>latest version from GIT</a></li>" >> "${WORK_DIR}/public/index.html"
curl -fsS -H "Authorization: Bearer ${CI_PAGES_TOKEN}" https://api.github.com/repos/${GH_PROJECT_NAME}/actions/artifacts \
| jq -cr '.artifacts
| map(select(.name == "pages" and (.workflow_run.head_branch
| test("^v[0-9]+\\.[0-9]+$"))))
| group_by(.workflow_run.head_branch)
| map(sort_by(.created_at))
| map(last)
| map({url:.archive_download_url,branch:.workflow_run.head_branch})
| .[]' \
| while read TAG_INFO
do
DOWNLOAD_URL="$(jq -r .url <<< "$TAG_INFO")"
TAG="$(jq -r .branch <<< "$TAG_INFO" | sed 's/^v//')"
mkdir -p "${WORK_DIR}/public/${TAG}"
cd "${WORK_DIR}/public/${TAG}"
curl -H "Authorization: Bearer ${CI_PAGES_TOKEN}" -LfsSo artifacts.zip "${DOWNLOAD_URL}"
unzip -o -d public artifacts.zip
rm artifacts.zip
while [ 1 -eq "$(find . -mindepth 1 -maxdepth 1|wc -l)" ]
do
SUBDIR="$(find . -mindepth 1 -maxdepth 1)"
find "$SUBDIR" -mindepth 1 -maxdepth 1 -exec mv '{}' . ';'
rmdir "$SUBDIR"
done
echo "<li><a href='${TAG}/'>version ${TAG}</a></li>" >> "${WORK_DIR}/public/index.html"
done
########################
## GITLAB
########################
INERROR=true
ALLTAGS="$(curl -fsS "${GITLAB_URL}/api/v4/projects/${PROJECT_NAME}/repository/tags" | tr ',' '\n' | grep '"name"' | tr '"' ' ' | awk '{print $4}' | grep '^[0-9]*\.[0-9]*\.[0-9]*$' | sort -rVu)"
for TAG_BASE in $(echo "${ALLTAGS}" | sed 's/^\([0-9]*\.[0-9]*\.\)[0-9]*$/\1/' | sort -rVu)
do
for TAG in $(echo "${ALLTAGS}" | fgrep "${TAG_BASE}" | sort -rVu)
do
cd "${WORK_DIR}"
mkdir -p "${TAG}"
cd "${TAG}"
if curl -fsSo artifacts.zip "${GITLAB_URL}/api/v4/projects/${PROJECT_NAME}/jobs/artifacts/${TAG}/download?job=pages"
then
INERROR=false
STAG="$(echo "${TAG}" | sed 's/\.[0-9]*$//')"
unzip -o artifacts.zip
rm artifacts.zip
if [ -d "public/${TAG}" ]
then
mv "public/${TAG}" "${WORK_DIR}/public/${STAG}"
elif [ -d "${TAG}" ]
then
mv "${TAG}" "${WORK_DIR}/public/${STAG}"
elif [ -d "public" ]
then
mv public "${WORK_DIR}/public/${STAG}"
fi
echo "<li><a href='${STAG}/'>version ${STAG}</a></li>" >> "${WORK_DIR}/public/index.html"
cd "${WORK_DIR}"
rm -rf "${WORK_DIR}/${TAG}"
break
fi
cd "${WORK_DIR}"
rm -rf "${WORK_DIR}/${TAG}"
done
done
if [ true = "${INERROR}" ]
then
rm -rf "${WORK_DIR}"
exit 1
fi
cat << EOF >> "${WORK_DIR}/public/index.html"
</ul>
</body>
EOF
mv "${WORK_DIR}/public"/* "${DEPLOY_DIR}"