diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d68c6ea..20c2f22 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -89,29 +89,51 @@ 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 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 } - found && /^## / { found=0 } - !found { print } - ') + + 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 } + found && /^## / { found=0 } + !found { print } + ') + else + 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" \ + --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 - UPDATED=$(printf '%s\n\n%s' "${WIKI_CONTENT}" "${NEW_SECTION}") + echo "ERROR: GitLab API returned HTTP ${HTTP_CODE}" + cat /tmp/wiki.json + exit 1 fi - curl -sf -X PUT \ - --header "PRIVATE-TOKEN:${GITLAB_TOKEN}" \ - --header "Content-Type:application/json" \ - --data "$(jq -n --arg c "${UPDATED}" '{content:$c}')" \ - "${API_URL}/wikis/Asteria" rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH