fix CI update-wiki: handle missing token and HTTP errors gracefully

- Fail early with clear message when GITLAB_TOKEN is unset
- Capture HTTP status code instead of relying on curl -f
- Handle 404 by creating the wiki page if it doesn't exist yet
- Print API response body on unexpected errors for easier debugging

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
AgelandClaude Sonnet 4.6 committed 2026-03-16 20:44:08 +01:00
1 parent 18e1ba1010
commit 9cbfb4728b
1 file changed
+25 -3
+25 -3
View File
@@ -89,16 +89,27 @@ update-wiki:
before_script:
- apk add --no-cache curl jq
script:
- |
if [ -z "${GITLAB_TOKEN}" ]; then
echo "ERROR: GITLAB_TOKEN CI/CD variable is not set"
exit 1
fi
- PROJECT_URL="https://gitlab.com/asteria-bookreader/asteria-qt"
- API_URL="https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}"
- ARTIFACTS_URL="${PROJECT_URL}/-/jobs/artifacts/${CI_COMMIT_REF_NAME}/raw"
- LINUX_URL="${ARTIFACTS_URL}/build-linux/asteria-qt?job=build-linux"
- WINDOWS_URL="${ARTIFACTS_URL}/build-windows/asteria-qt.exe?job=build-windows"
- WIKI_CONTENT=$(curl -sf --header "PRIVATE-TOKEN:${GITLAB_TOKEN}" "${API_URL}/wikis/Asteria" | jq -r '.content')
- |
HTTP_CODE=$(curl -s -o /tmp/wiki.json -w '%{http_code}' \
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
"${API_URL}/wikis/Asteria")
NEW_SECTION=$(printf '## Latest dev builds\n\n> Updated automatically by pipeline [#%s](%s) from `%s` @ %s\n\n| Platform | Download |\n|----------|----------|\n| Linux (x86_64) | [asteria-qt](%s) |\n| Windows (x86_64) | [asteria-qt.exe](%s) |' \
"${CI_PIPELINE_ID}" "${CI_PIPELINE_URL}" "${CI_COMMIT_REF_NAME}" "${CI_COMMIT_SHORT_SHA}" \
"${LINUX_URL}" "${WINDOWS_URL}")
if [ "${HTTP_CODE}" = "200" ]; then
WIKI_CONTENT=$(jq -r '.content' /tmp/wiki.json)
if printf '%s' "${WIKI_CONTENT}" | grep -q '^## Latest dev builds'; then
UPDATED=$(printf '%s' "${WIKI_CONTENT}" | awk -v new="${NEW_SECTION}" '
/^## Latest dev builds/ { found=1; printf "%s\n", new; next }
@@ -109,9 +120,20 @@ update-wiki:
UPDATED=$(printf '%s\n\n%s' "${WIKI_CONTENT}" "${NEW_SECTION}")
fi
curl -sf -X PUT \
--header "PRIVATE-TOKEN:${GITLAB_TOKEN}" \
--header "Content-Type:application/json" \
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
--header "Content-Type: application/json" \
--data "$(jq -n --arg c "${UPDATED}" '{content:$c}')" \
"${API_URL}/wikis/Asteria"
elif [ "${HTTP_CODE}" = "404" ]; then
curl -sf -X POST \
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
--header "Content-Type: application/json" \
--data "$(jq -n --arg t "Asteria" --arg c "${NEW_SECTION}" '{title:$t,content:$c}')" \
"${API_URL}/wikis"
else
echo "ERROR: GitLab API returned HTTP ${HTTP_CODE}"
cat /tmp/wiki.json
exit 1
fi
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH