-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate
More file actions
executable file
·174 lines (149 loc) · 5.26 KB
/
generate
File metadata and controls
executable file
·174 lines (149 loc) · 5.26 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
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
168
169
170
171
172
173
174
#!/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"
ALLTAGS="$(curl -v "https://api.github.com/repos/${GH_PROJECT_NAME}/releases" | jq -cr ' .[] | to_entries[] | select(.key == "tag_name") | .value ')"
## example:
## ALLTAGS=$'1.9.2\n1.9.1-fixed1.9.0\n1.8.1\n1.8.2\n1.8.1\n1.8.0'
## TAG = 1.9.1-fixed (correspond to the tag on pdidev/pdi)
## EX_TAG_BASE=1.9.
## STAG=1.9
##hypothesis: TAG = [0-9].[0-9].[0-9]-fixed[0-9]
## or TAG = [0-9].[0-9].[0-9]-fixed
## or TAG = [0-9].[0-9].[0-9]
## where [0-9] is an integer
for TAG_BASE in "$(echo "${ALLTAGS}" | tr '"' ' ' | sed 's/^\([0-9]*\.[0-9]*\.\)[0-9]*-fixed[0-9]*$/\1/g' | sed 's/^\([0-9]*\.[0-9]*\.\)[0-9]*$/\1/g'| sort -rVu)"
do
list_tag_base=($TAG_BASE) ## get the list of string of TAG_BASE in a array
for EX_TAG_BASE in "${list_tag_base[@]}"
do
for TAG in $(echo "${ALLTAGS}" | fgrep "${EX_TAG_BASE}" | sort -rVu)
do
# remove "." (last character of EX_TAG_BASE)
STAG="$(echo "${EX_TAG_BASE}" | sed 's/\.$//')"
DOWNLOAD_URL="https://github.com/${GH_PROJECT_NAME}/releases/download/${TAG}/pages.zip"
mkdir -p "${WORK_DIR}/public/${STAG}"
cd "${WORK_DIR}/public/${STAG}"
if curl -LfsSo artifacts.zip "${DOWNLOAD_URL}"
then
echo "unzip asset for release: ${TAG} and STAG=${STAG}"
echo "dowload_url=${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='${STAG}/'>version ${STAG}</a></li>" >> "${WORK_DIR}/public/index.html"
echo "<li><a href='${STAG}/'>version ${STAG}</a></li>"
break
else
echo "error in downloading asset for release: ${TAG} and STAG=${STAG}"
echo "dowload_url=${DOWNLOAD_URL}"
exit 1
fi
done
done
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}"