From 7de84194f25f553208ac01bf7291c2c4efc42491 Mon Sep 17 00:00:00 2001
From: mtyszczak <mateusz.tyszczak@gmail.com>
Date: Fri, 21 Feb 2025 15:32:29 +0100
Subject: [PATCH] Fix npm_push_doc_template stage

---
 scripts/bash/npm-helpers/npm_set_doc_url.sh | 27 +++++++---
 templates/npm_projects.gitlab-ci.yml        | 57 ++++++++++++++++-----
 2 files changed, 65 insertions(+), 19 deletions(-)

diff --git a/scripts/bash/npm-helpers/npm_set_doc_url.sh b/scripts/bash/npm-helpers/npm_set_doc_url.sh
index 9482389..fb7e78e 100755
--- a/scripts/bash/npm-helpers/npm_set_doc_url.sh
+++ b/scripts/bash/npm-helpers/npm_set_doc_url.sh
@@ -1,9 +1,14 @@
-#! /bin/bash
-set -e
+#!/bin/bash
+set -xe
 
-PROJECT_URL="${1:?Missing arg #1 pointing project url. It is provided by CI_PROJECT_URL variable}"
-FEATURE_BRANCH_NAME="${2:?Missing arg #2 pointing a branch name}"
-FINAL_MERGE="${3:?Missing arg #3 pointing the final merge flag value}"
+PROJECT_DIR="${1:?Missing arg #1 specifying a project source directory}"
+PROJECT_URL="${2:?Missing arg #2 pointing project url. It is provided by CI_PROJECT_URL variable}"
+FEATURE_BRANCH_NAME="${3:?Missing arg #3 pointing a branch name}"
+FINAL_MERGE="${4:?Missing arg #4 pointing the final merge flag value}"
+REPLACE_ENV_NAME="${5:-GEN_DOC_URL}"
+REPLACE_FILE_PATH="${6:-${PROJECT_DIR}/README.md}"
+
+pushd "${PROJECT_DIR}"
 
 if [ "${FINAL_MERGE}" = "true" ]; then
   FINAL_MERGE=1
@@ -19,6 +24,14 @@ fi
 
 echo "Documentation url: ${DOC_URL}"
 
-export DOC_URL="${DOC_URL}"
+sed -i "s<\${${REPLACE_ENV_NAME}}<${DOC_URL}<g" "${REPLACE_FILE_PATH}"
+if grep -q "\${${REPLACE_ENV_NAME}}" "${REPLACE_FILE_PATH}"; then
+  echo "Failed to replace the documentation url in ${REPLACE_FILE_PATH}"
+  exit 1;
+fi # Ensure that the placeholder was replaced
+
+echo "Replaced the documentation url: \"\${${REPLACE_ENV_NAME}}\" => \"${DOC_URL}\" in file: \"${REPLACE_FILE_PATH}\""
+
+popd
 
-echo GEN_DOC_URL="${DOC_URL}" > "gen_doc.env"
+echo ${REPLACE_ENV_NAME}="${DOC_URL}" > "gen_doc.env"
diff --git a/templates/npm_projects.gitlab-ci.yml b/templates/npm_projects.gitlab-ci.yml
index e28db22..85e0d92 100644
--- a/templates/npm_projects.gitlab-ci.yml
+++ b/templates/npm_projects.gitlab-ci.yml
@@ -217,36 +217,69 @@ variables:
       when: manual
       allow_failure: true
 
-# Allows to store generated documentation files inside a wiki repo specific to given project. Wiki repo must be explicitly created before using this job.
-# Documentation files coming to protected branches are committed into root directory in the wiki repo. All other changes are pushed into non-stable/<branch-name> subdirectory what allows to render them by wiki.
-.npm_push_doc_template:
-  extends: .npm_based_job_base
+.npm_generate_doc_url:
+  extends: .npm_process_built_package_tarball
   variables:
-    # Must be overrided by derived job
-    WIKI_PUSH_TOKEN: ""
-
     # The directory containing sources to be built - it can be overrided by derived job
     SOURCE_DIR: "${CI_PROJECT_DIR}"
     PROJECT_URL: "${CI_PROJECT_URL}"
-    # Output directory where should be stored `docs' subdirectory containing generated documentation files
     DIST_DIR: "$CI_PROJECT_DIR/dist"
+    PACKAGE_TGZ_PATH: "${BUILT_PACKAGE_PATH}"
 
-    # This variable will be set in before_script, so we can perform operations in "script:" stage based on it.
+    # This variable will be set in after_script, so we can perform operations in "script:" stage based on it.
     DOC_URL: ""
 
   before_script:
     # start always at project root dir, to make sure .env file (included in artifacts) is generated there
     - cd "${CI_PROJECT_DIR}"
-    - /home/emscripten/scripts/npm_push_doc.sh "${PROJECT_URL}" "${CI_COMMIT_REF_NAME}" "${CI_COMMIT_REF_PROTECTED}"
+    - /home/emscripten/scripts/npm_set_doc_url.sh "${PROJECT_URL}" "${CI_COMMIT_REF_NAME}" "${CI_COMMIT_REF_PROTECTED}"
+
+  artifacts:
+    reports:
+      dotenv:
+        - "gen_doc.env"  # contains URL to generated documentation pointed by `GEN_DOC_URL` variable
+
+    when: always
+    expire_in: 1 week
+
+.npm_generate_doc_url:
+  extends: .npm_based_job_base
+  variables:
+    # The directory containing sources to be built - it can be overrided by derived job
+    SOURCE_DIR: "${CI_PROJECT_DIR}"
+    PROJECT_URL: "${CI_PROJECT_URL}"
+    REPLACE_ENV_NAME: "GEN_DOC_URL"
+    REPLACE_FILE_PATH: "${SOURCE_DIR}/README.md"
 
-  after_script:
+  before_script:
+    # start always at project root dir, to make sure .env file (included in artifacts) is generated there
     - cd "${CI_PROJECT_DIR}"
-    - /home/emscripten/scripts/npm_push_doc.sh "${SOURCE_DIR}" "${PROJECT_URL}" "${WIKI_PUSH_TOKEN}" "${DIST_DIR}" "${CI_COMMIT_REF_NAME}" "${CI_COMMIT_REF_PROTECTED}"
+    - /home/emscripten/scripts/npm_set_doc_url.sh "${SOURCE_DIR}" "${PROJECT_URL}" "${CI_COMMIT_REF_NAME}" "${CI_COMMIT_REF_PROTECTED}" "${REPLACE_ENV_NAME}" "${REPLACE_FILE_PATH}"
 
   artifacts:
+    paths:
+      - "${REPLACE_FILE_PATH}"
     reports:
       dotenv:
         - "gen_doc.env"  # contains URL to generated documentation pointed by `GEN_DOC_URL` variable
 
     when: always
     expire_in: 1 week
+
+# Allows to store generated documentation files inside a wiki repo specific to given project. Wiki repo must be explicitly created before using this job.
+# Documentation files coming to protected branches are committed into root directory in the wiki repo. All other changes are pushed into non-stable/<branch-name> subdirectory what allows to render them by wiki.
+.npm_push_doc_template:
+  extends: .npm_based_job_base
+  variables:
+    # Must be overrided by derived job
+    WIKI_PUSH_TOKEN: ""
+
+    # The directory containing sources to be built - it can be overrided by derived job
+    SOURCE_DIR: "${CI_PROJECT_DIR}"
+    PROJECT_URL: "${CI_PROJECT_URL}"
+    # Output directory where should be stored `docs' subdirectory containing generated documentation files
+    DIST_DIR: "$CI_PROJECT_DIR/dist"
+
+  script:
+    - cd "${CI_PROJECT_DIR}"
+    - /home/emscripten/scripts/npm_push_doc.sh "${SOURCE_DIR}" "${PROJECT_URL}" "${WIKI_PUSH_TOKEN}" "${DIST_DIR}" "${CI_COMMIT_REF_NAME}" "${CI_COMMIT_REF_PROTECTED}"
-- 
GitLab