diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 000000000..94a29c083 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,95 @@ +stages: + - build + - deploy + - comment + +build_hugo: + stage: build + image: "${CI_TEMPLATE_REGISTRY_HOST}/pages/hugo/hugo_extended:0.135.0" + tags: + - build_docs + rules: + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' + variables: + GIT_SUBMODULE_STRATEGY: recursive + NAME: "${CI_COMMIT_REF_SLUG}" + script: + - hugo --gc --minify --environment staging --baseURL "${DOCS_PREVIEW_URL_BASE}/${NAME}" + # use branch name like directory name for the URL path going forward + - mv -v public "${NAME}" + - tar -czf archive.tar.gz "${NAME}" + artifacts: + paths: + - archive.tar.gz + expire_in: 1 week + +deploy_preview_hugo: + stage: deploy + image: espressif/scp + tags: + - deploy_docs + - shiny + rules: + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' + needs: ["build_hugo"] + variables: + SSH_KEY: "$DOCS_PREVIEW_PRIVATEKEY" # SSH_KEY used inside espressif/scp + SERVER_PATH: "$DOCS_PREVIEW_PATH" + SERVER_URL_BASE: "$DOCS_PREVIEW_URL_BASE" + USER: "$DOCS_PREVIEW_SERVER_USER" + SERVER: "$DOCS_PREVIEW_SERVER" + NAME: "${CI_COMMIT_REF_SLUG}" + script: + # upload and extract the archive, + # delete the old directory with the same name (if doesn't contain . or /) + # so as not to accumulate garbage from the previous run + - cat archive.tar.gz | ssh ${USER}@${SERVER} + "cd ${SERVER_PATH}; + [[ \"$NAME\" != *.* && \"$NAME\" != */* ]] && [ -d \"$NAME\" ] && rm -rf \"$NAME\"; + pwd; tar xzvf -" + - echo "Preview ${SERVER_URL_BASE}/${NAME}" + +post_preview_link: + stage: comment + image: badouralix/curl-jq + tags: + - deploy_docs + - shiny + rules: + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' + needs: ["deploy_preview_hugo"] + variables: + SERVER_URL_BASE: "$DOCS_PREVIEW_URL_BASE" + NAME: "${CI_COMMIT_REF_SLUG}" + MR_ID: "$CI_MERGE_REQUEST_IID" + PROJECT_ID: "$CI_MERGE_REQUEST_PROJECT_ID" + script: + - | + # Print MR_ID and PROJECT_ID for debugging + echo "MR_ID: ${MR_ID}" + echo "PROJECT_ID: ${PROJECT_ID}" + echo "$CI_SERVER_HOST" + echo "${CI_SERVER_PORT}" + + GITLAB_API="https://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${PROJECT_ID}/merge_requests/${MR_ID}/notes" + AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_BOT_API_TOKEN}" + + # Get existing comments + API_RESPONSE=$(curl --silent --header "$AUTH_HEADER" "$GITLAB_API") + echo "API Response: $API_RESPONSE" # Add this line for debugging + + # Check if the response contains the expected structure + COMMENTS=$(echo "$API_RESPONSE" | jq -r '.[] | select(.body | contains("🎉 Preview for this MR")) | .id') + + # Delete previous preview comments + if [ -n "$COMMENTS" ]; then + for COMMENT_ID in $COMMENTS; do + curl --silent --request DELETE --header "$AUTH_HEADER" "${GITLAB_API}/${COMMENT_ID}" + done + fi + + # Post new preview link + PREVIEW_LINK="${SERVER_URL_BASE}/${NAME}" + COMMENT_BODY="🎉 Preview for this MR: ${PREVIEW_LINK}" + curl --silent --request POST --header "$AUTH_HEADER" --header "Content-Type: application/json" \ + --data "{\"body\": \"${COMMENT_BODY}\"}" "$GITLAB_API"