|
| 1 | +stages: |
| 2 | + - build |
| 3 | + - deploy |
| 4 | + - comment |
| 5 | + |
| 6 | +build_hugo: |
| 7 | + stage: build |
| 8 | + image: "${CI_TEMPLATE_REGISTRY_HOST}/pages/hugo/hugo_extended:0.135.0" |
| 9 | + tags: |
| 10 | + - build_docs |
| 11 | + rules: |
| 12 | + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' |
| 13 | + variables: |
| 14 | + GIT_SUBMODULE_STRATEGY: recursive |
| 15 | + NAME: "${CI_COMMIT_REF_SLUG}" |
| 16 | + script: |
| 17 | + - hugo --gc --minify --environment staging --baseURL "${DOCS_PREVIEW_URL_BASE}/${NAME}" |
| 18 | + # use branch name like directory name for the URL path going forward |
| 19 | + - mv -v public "${NAME}" |
| 20 | + - tar -czf archive.tar.gz "${NAME}" |
| 21 | + artifacts: |
| 22 | + paths: |
| 23 | + - archive.tar.gz |
| 24 | + expire_in: 1 week |
| 25 | + |
| 26 | +deploy_preview_hugo: |
| 27 | + stage: deploy |
| 28 | + image: espressif/scp |
| 29 | + tags: |
| 30 | + - deploy_docs |
| 31 | + - shiny |
| 32 | + rules: |
| 33 | + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' |
| 34 | + needs: ["build_hugo"] |
| 35 | + variables: |
| 36 | + SSH_KEY: "$DOCS_PREVIEW_PRIVATEKEY" # SSH_KEY used inside espressif/scp |
| 37 | + SERVER_PATH: "$DOCS_PREVIEW_PATH" |
| 38 | + SERVER_URL_BASE: "$DOCS_PREVIEW_URL_BASE" |
| 39 | + USER: "$DOCS_PREVIEW_SERVER_USER" |
| 40 | + SERVER: "$DOCS_PREVIEW_SERVER" |
| 41 | + NAME: "${CI_COMMIT_REF_SLUG}" |
| 42 | + script: |
| 43 | + # upload and extract the archive, |
| 44 | + # delete the old directory with the same name (if doesn't contain . or /) |
| 45 | + # so as not to accumulate garbage from the previous run |
| 46 | + - cat archive.tar.gz | ssh ${USER}@${SERVER} |
| 47 | + "cd ${SERVER_PATH}; |
| 48 | + [[ \"$NAME\" != *.* && \"$NAME\" != */* ]] && [ -d \"$NAME\" ] && rm -rf \"$NAME\"; |
| 49 | + pwd; tar xzvf -" |
| 50 | + - echo "Preview ${SERVER_URL_BASE}/${NAME}" |
| 51 | + |
| 52 | +post_preview_link: |
| 53 | + stage: comment |
| 54 | + image: badouralix/curl-jq |
| 55 | + tags: |
| 56 | + - deploy_docs |
| 57 | + - shiny |
| 58 | + rules: |
| 59 | + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' |
| 60 | + needs: ["deploy_preview_hugo"] |
| 61 | + variables: |
| 62 | + SERVER_URL_BASE: "$DOCS_PREVIEW_URL_BASE" |
| 63 | + NAME: "${CI_COMMIT_REF_SLUG}" |
| 64 | + MR_ID: "$CI_MERGE_REQUEST_IID" |
| 65 | + PROJECT_ID: "$CI_MERGE_REQUEST_PROJECT_ID" |
| 66 | + script: |
| 67 | + - | |
| 68 | + # Print MR_ID and PROJECT_ID for debugging |
| 69 | + echo "MR_ID: ${MR_ID}" |
| 70 | + echo "PROJECT_ID: ${PROJECT_ID}" |
| 71 | + echo "$CI_SERVER_HOST" |
| 72 | + echo "${CI_SERVER_PORT}" |
| 73 | +
|
| 74 | + GITLAB_API="https://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${PROJECT_ID}/merge_requests/${MR_ID}/notes" |
| 75 | + AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_BOT_API_TOKEN}" |
| 76 | +
|
| 77 | + # Get existing comments |
| 78 | + API_RESPONSE=$(curl --silent --header "$AUTH_HEADER" "$GITLAB_API") |
| 79 | + echo "API Response: $API_RESPONSE" # Add this line for debugging |
| 80 | +
|
| 81 | + # Check if the response contains the expected structure |
| 82 | + COMMENTS=$(echo "$API_RESPONSE" | jq -r '.[] | select(.body | contains("🎉 Preview for this MR")) | .id') |
| 83 | +
|
| 84 | + # Delete previous preview comments |
| 85 | + if [ -n "$COMMENTS" ]; then |
| 86 | + for COMMENT_ID in $COMMENTS; do |
| 87 | + curl --silent --request DELETE --header "$AUTH_HEADER" "${GITLAB_API}/${COMMENT_ID}" |
| 88 | + done |
| 89 | + fi |
| 90 | +
|
| 91 | + # Post new preview link |
| 92 | + PREVIEW_LINK="${SERVER_URL_BASE}/${NAME}" |
| 93 | + COMMENT_BODY="🎉 Preview for this MR: ${PREVIEW_LINK}" |
| 94 | + curl --silent --request POST --header "$AUTH_HEADER" --header "Content-Type: application/json" \ |
| 95 | + --data "{\"body\": \"${COMMENT_BODY}\"}" "$GITLAB_API" |
0 commit comments