174 lines
4.4 KiB
Bash
Executable file
174 lines
4.4 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# CI script to sync .mdx asset files from professional-services examples
|
|
# to the STACKIT docs assetcontainer.
|
|
#
|
|
# Usage:
|
|
# export STACKIT_DOCS_TOKEN=<gh-pat>
|
|
# ./ci-scripts/sync-stackit-docs.sh
|
|
#
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
REPO_ROOT="$(dirname "$SCRIPT_DIR")"
|
|
DOCS_CLONE_DIR="/tmp/stackit-docs-sync"
|
|
GENERATED_DIR="${REPO_ROOT}/.generated-mdx"
|
|
DOCS_ASSET_DIR="docs/cloud-framework/architecture-framework/assetcontainer/professional-services"
|
|
BRANCH_NAME="autopr/sync-professional-service-examples"
|
|
DOCS_REPO="${STACKIT_DOCS_REPO:-stackitcloud/docs-content}"
|
|
|
|
GREEN='\033[0;32m'
|
|
RED='\033[0;31m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
log_info() {
|
|
echo -e "${GREEN}[INFO]${NC} $*"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $*"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $*"
|
|
}
|
|
|
|
cleanup() {
|
|
rm -rf "$DOCS_CLONE_DIR"
|
|
rm -rf "$GENERATED_DIR"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
check_prerequisites() {
|
|
if ! command -v python3 &>/dev/null; then
|
|
log_error "python3 not found"
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v gh &>/dev/null; then
|
|
log_error "gh (GitHub CLI) not found"
|
|
exit 1
|
|
fi
|
|
|
|
if [ -z "${STACKIT_DOCS_TOKEN:-}" ]; then
|
|
log_error "STACKIT_DOCS_TOKEN environment variable is not set"
|
|
exit 1
|
|
fi
|
|
|
|
python3 -c "import yaml" 2>/dev/null || {
|
|
log_info "Installing PyYAML..."
|
|
pip3 install pyyaml
|
|
}
|
|
}
|
|
|
|
generate_mdx_files() {
|
|
log_info "Generating .mdx asset files..."
|
|
MDX_OUTPUT_DIR="$GENERATED_DIR/professional-services" python3 "$SCRIPT_DIR/aggregate_docs.py"
|
|
if [ ! -d "$GENERATED_DIR/professional-services" ]; then
|
|
log_error "Generation did not produce .mdx files"
|
|
exit 1
|
|
fi
|
|
local count
|
|
count=$(find "$GENERATED_DIR/professional-services" -name "*.mdx" | wc -l)
|
|
log_info "Generated $count .mdx files in $GENERATED_DIR/professional-services"
|
|
}
|
|
|
|
checkout_docs_repo() {
|
|
log_info "Cloning STACKIT docs repository..."
|
|
rm -rf "$DOCS_CLONE_DIR"
|
|
gh repo clone "$DOCS_REPO" "$DOCS_CLONE_DIR" -- --quiet
|
|
log_info "Cloned to $DOCS_CLONE_DIR"
|
|
}
|
|
|
|
copy_generated_files() {
|
|
local target_dir="$DOCS_CLONE_DIR/$DOCS_ASSET_DIR"
|
|
mkdir -p "$target_dir"
|
|
cp -R "$GENERATED_DIR/professional-services"/* "$target_dir/"
|
|
log_info "Copied generated files to $target_dir"
|
|
}
|
|
|
|
check_for_changes() {
|
|
copy_generated_files
|
|
|
|
if gh pr list --repo "$DOCS_REPO" --head "$BRANCH_NAME" --state open --json number --jq '.[] | .number' &>/dev/null; then
|
|
log_warn "PR already exists for branch $BRANCH_NAME, skipping"
|
|
return 1
|
|
fi
|
|
|
|
cd "$DOCS_CLONE_DIR"
|
|
local status
|
|
status=$(git status --porcelain)
|
|
if [ -z "$status" ]; then
|
|
log_info "No changes detected in docs"
|
|
return 1
|
|
fi
|
|
|
|
log_info "Changes detected in docs:"
|
|
echo "$status"
|
|
return 0
|
|
}
|
|
|
|
create_pr() {
|
|
copy_generated_files
|
|
|
|
cd "$DOCS_CLONE_DIR"
|
|
|
|
git config user.name "stackit-bot"
|
|
git config user.email "bot@stackit.cloud"
|
|
|
|
git checkout -b "$BRANCH_NAME" || git checkout "$BRANCH_NAME"
|
|
git add "$DOCS_ASSET_DIR"
|
|
git diff --cached --quiet || git commit -m "chore(professional-services): sync Terraform example assets
|
|
|
|
Updated .mdx asset files from professional-service/examples/*/stackit.docs.yaml"
|
|
|
|
log_info "Pushing branch $BRANCH_NAME..."
|
|
git push origin "$BRANCH_NAME" --force-with-lease --quiet
|
|
|
|
local pr_title="Sync Professional Services Terraform Examples"
|
|
local pr_body="## Auto-generated PR
|
|
|
|
This PR syncs the Professional Services Terraform example assets to the STACKIT documentation assetcontainer.
|
|
|
|
### Changes
|
|
- Updated .mdx asset files for all examples in \`professional-service/examples/\`
|
|
- Generated from \`stackit.docs.yaml\` metadata in each example directory
|
|
|
|
### Assets Updated
|
|
\`\`\`
|
|
$DOCS_ASSET_DIR/
|
|
\`\`\`
|
|
|
|
---
|
|
*Created automatically by the professional-services CI pipeline.*"
|
|
|
|
log_info "Creating PR..."
|
|
local pr_url
|
|
pr_url=$(gh pr create \
|
|
--repo "$DOCS_REPO" \
|
|
--title "$pr_title" \
|
|
--body "$pr_body" \
|
|
--head "$BRANCH_NAME" \
|
|
--base main \
|
|
--label "automation" \
|
|
--label "professional-services" \
|
|
2>&1 || true)
|
|
|
|
log_info "PR result: $pr_url"
|
|
}
|
|
|
|
main() {
|
|
check_prerequisites
|
|
generate_mdx_files
|
|
checkout_docs_repo
|
|
|
|
if check_for_changes; then
|
|
create_pr
|
|
else
|
|
log_info "Nothing to do. Docs are up to date."
|
|
fi
|
|
}
|
|
|
|
main "$@"
|