|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +## |
| 4 | +## Copyright © contributors to CloudNativePG, established as |
| 5 | +## CloudNativePG a Series of LF Projects, LLC. |
| 6 | +## |
| 7 | +## Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +## you may not use this file except in compliance with the License. |
| 9 | +## You may obtain a copy of the License at |
| 10 | +## |
| 11 | +## http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +## |
| 13 | +## Unless required by applicable law or agreed to in writing, software |
| 14 | +## distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +## See the License for the specific language governing permissions and |
| 17 | +## limitations under the License. |
| 18 | +## |
| 19 | +## SPDX-License-Identifier: Apache-2.0 |
| 20 | +## |
| 21 | + |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +SOURCE_REPO="https://github.com/cloudnative-pg/cloudnative-pg.git" |
| 25 | +SOURCE_DOCS_PATH="docs/src" |
| 26 | +TMP_BASE="$(mktemp -d)" |
| 27 | +trap 'rm -rf "$TMP_BASE"' EXIT |
| 28 | + |
| 29 | +VERSION_ARG="${1:-}" |
| 30 | +if [[ -z "$VERSION_ARG" ]]; then |
| 31 | + cat <<EOF |
| 32 | +Usage: $0 <tag|main> [branch name] |
| 33 | +
|
| 34 | +This script brings in the documentation from GitHub at |
| 35 | +github.com/cloudnative-pg/cloudnative-pg into the documentation repository. |
| 36 | +The first argument can either be "main" to import the most recent development |
| 37 | +snapshot or a specific version tag. The second argument is optional and allows |
| 38 | +you to import the documentation using a specified Git reference rather than |
| 39 | +the first argument. |
| 40 | +
|
| 41 | +### Examples: |
| 42 | +
|
| 43 | +To import the latest development snapshot: |
| 44 | +
|
| 45 | + ./scripts/import_docs main |
| 46 | +
|
| 47 | +To import the documentation for a new release: |
| 48 | +
|
| 49 | + ./scripts/import_docs v1.28.0 |
| 50 | +
|
| 51 | +To update the existing documentation for the specified tag using the |
| 52 | +release branch: |
| 53 | +
|
| 54 | + ./scripts/import_docs v1.28.0 release-1.28 |
| 55 | +
|
| 56 | +EOF |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +GIT_REF="${2:-$1}" |
| 61 | + |
| 62 | +# Normalize version label (strip leading 'v') |
| 63 | +VERSION_LABEL="$(echo "$VERSION_ARG" | sed 's/^v//')" |
| 64 | + |
| 65 | +echo "=== Import job starting for: $VERSION_ARG (label: $VERSION_LABEL) ===" |
| 66 | +echo "Working temp: $TMP_BASE" |
| 67 | + |
| 68 | +# Determine type |
| 69 | +IS_MAIN=false |
| 70 | +IS_RC=false |
| 71 | + |
| 72 | +if [[ "$VERSION_ARG" == "main" ]]; then |
| 73 | + IS_MAIN=true |
| 74 | +elif [[ "$VERSION_LABEL" =~ ^[0-9]+\.[0-9]+\.[0-9]+-rc[0-9]+$ ]]; then |
| 75 | + IS_RC=true |
| 76 | + VERSION_DIR=$(echo "$VERSION_ARG" | sed -E 's/^v([0-9]+.[0-9]+).*/\1/') |
| 77 | +elif [[ "$VERSION_LABEL" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 78 | + VERSION_DIR=$(echo "$VERSION_ARG" | sed -E 's/^v([0-9]+.[0-9]+).*/\1/') |
| 79 | +else |
| 80 | + echo "Invalid version argument: '$VERSION_ARG'" |
| 81 | + exit 1 |
| 82 | +fi |
| 83 | + |
| 84 | +# Clone the repo |
| 85 | +SRC_TMP="$TMP_BASE/source" |
| 86 | +echo "Cloning $SOURCE_REPO ($VERSION_ARG) -> $SRC_TMP" |
| 87 | + |
| 88 | +if ! git clone --depth 1 --branch "${GIT_REF}" "$SOURCE_REPO" "$SRC_TMP" 2>/dev/null; then |
| 89 | + echo "ERROR: failed to clone branch/tag for ${GIT_REF}." |
| 90 | + exit 1 |
| 91 | +else |
| 92 | + echo "Cloned using ref ${GIT_REF}" |
| 93 | +fi |
| 94 | + |
| 95 | +SOURCE_PATH="$SRC_TMP/$SOURCE_DOCS_PATH" |
| 96 | +if [[ ! -d "$SOURCE_PATH" ]]; then |
| 97 | + echo "ERROR: expected docs folder at $SOURCE_PATH" |
| 98 | + exit 1 |
| 99 | +fi |
| 100 | + |
| 101 | +# Install node modules if missing |
| 102 | +if ! command -v yarn >/dev/null 2>&1; then |
| 103 | + echo "ERROR: yarn not found. Install Yarn." |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | + |
| 107 | +if [[ ! -d "node_modules" ]]; then |
| 108 | + echo "node_modules missing: running yarn install" |
| 109 | + yarn install --silent |
| 110 | +fi |
| 111 | + |
| 112 | +# ===== MAIN BRANCH ===== |
| 113 | +if [[ "$IS_MAIN" == true ]]; then |
| 114 | + echo "Copying imported docs -> ./docs" |
| 115 | + mkdir -p ./docs |
| 116 | + rsync -av --delete "$SOURCE_PATH/" --exclude "css" ./docs/ |
| 117 | + |
| 118 | + echo "Updated ./docs from main — import completed." |
| 119 | + exit 0 |
| 120 | +fi |
| 121 | + |
| 122 | +# ===== VERSION TAG ===== |
| 123 | +if grep -q "\"${VERSION_DIR}\"" versions.json 2>/dev/null; then |
| 124 | + # Import the new version in the correct folder |
| 125 | + TARGET_DIRECTORY="./versioned_docs/version-${VERSION_DIR}" |
| 126 | + echo "Copying imported docs -> ${TARGET_DIRECTORY}" |
| 127 | + rsync -av --delete "$SOURCE_PATH/" --exclude "css" "${TARGET_DIRECTORY}" |
| 128 | +else |
| 129 | + # Create Docusaurus version |
| 130 | + echo "Running: yarn docusaurus docs:version ${VERSION_DIR}" |
| 131 | + yarn docusaurus docs:version "${VERSION_DIR}" |
| 132 | + |
| 133 | + # Import the new version in the correct folder |
| 134 | + TARGET_DIRECTORY="./versioned_docs/version-${VERSION_DIR}" |
| 135 | + echo "Copying imported docs -> ${TARGET_DIRECTORY}" |
| 136 | + rsync -av --delete "$SOURCE_PATH/" --exclude "css" "${TARGET_DIRECTORY}" |
| 137 | + |
| 138 | + echo "=== Done: Docusaurus version ${VERSION_DIR} created ===" |
| 139 | +fi |
| 140 | + |
| 141 | +# Mark RC as preview |
| 142 | +VDIR="versioned_docs/version-${VERSION_DIR}" |
| 143 | +if [[ "$IS_RC" == true ]]; then |
| 144 | + echo "Marking ${VDIR} as preview (rc)" |
| 145 | + echo "preview: true" > "${VDIR}/.preview" |
| 146 | +fi |
| 147 | + |
0 commit comments