From dfe7714a251668e6b951e659186fc7c71b7fea99 Mon Sep 17 00:00:00 2001 From: Taylor Thomas Date: Thu, 2 Jan 2025 15:35:13 -0700 Subject: [PATCH] chore(ci): Adds automatic doc syncing Right now, this is specific to go, in a follow up, I'll add support for TS and Rust and might rename the script if it can be used generically Signed-off-by: Taylor Thomas --- .github/workflows/sync-examples.yml | 44 +++++++++++++++++++++++++++++ scripts/update-go-docs.sh | 43 ++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 .github/workflows/sync-examples.yml create mode 100755 scripts/update-go-docs.sh diff --git a/.github/workflows/sync-examples.yml b/.github/workflows/sync-examples.yml new file mode 100644 index 00000000..4fbc1db7 --- /dev/null +++ b/.github/workflows/sync-examples.yml @@ -0,0 +1,44 @@ +name: Sync example docs + +on: + pull_request: + branches: ['main'] + schedule: + - cron: '0 1 * * *' + workflow_dispatch: + +permissions: + contents: read + +jobs: + sync-docs: + permissions: + contents: write + runs-on: ubuntu-latest + steps: + - name: Checkout docs repo + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + path: ./wasmcloud.com + - name: Checkout Go repo + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + path: ./wasmcloud-go + repository: wasmCloud/go + - name: Sync docs + id: sync-docs + working-directory: ./wasmcloud.com + run: ./scripts/update-go-docs.sh ../wasmcloud-go + - name: Create Docs PR + if: steps.sync-docs.outputs.docs_changed == 'true' + uses: peter-evans/create-pull-request@5e914681df9dc83aa4e4905692ca88beb2f9e91f # v7.0.5 + with: + path: ./wasmcloud.com + commit-message: Updates Go example docs from wasmcloud-go + title: 'chore: Update Go example docs' + body: Updates Go example docs from wasmcloud-go. This is an automatic PR. + branch: chore/update-go-example-docs + delete-branch: true + author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com> + committer: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com> + signoff: true diff --git a/scripts/update-go-docs.sh b/scripts/update-go-docs.sh new file mode 100755 index 00000000..a77c833c --- /dev/null +++ b/scripts/update-go-docs.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# If no arguments are passed, print help message and exit +if [ $# -eq 0 ]; then + echo "Usage: $0 " + exit 1 +fi + +go_repo=$1 + +# Get the top-level directory of the repository +pushd "${go_repo}"; + docs_dir="$(git rev-parse --show-toplevel)/examples"; +popd; + +while IFS= read -r -d '' file +do + # Get the relative path of the file from the repo root + rel_path=$(dirname "${file#"$docs_dir/"}") + # This uppercases the first letter of each word in the path so the sidebar is correct. We may + # change this in the future with some metadata in the docs repo + target_dir="$(git rev-parse --show-toplevel)/docs/Examples/Go/${rel_path^}" + # Create the target directory structure + mkdir -p "${target_dir}" + # Copy the file to the docs repo + cp "$file" "${target_dir}/index.md" +done < <(find "${docs_dir}" -name "README.md" -type f -print0) + +# Now check if there are any git changes in the repo +if [[ -z "$(git status --porcelain)" ]]; then + echo "No changes detected in docs repo. Exiting..." + exit 0 +fi + +echo "Changes detected in docs repo after syncing" +# If GITHUB_OUTPUT is set, write a variable to it. This is mostly here so people can run it locally +# if desired and for testing. +if [ -n "${GITHUB_OUTPUT:-}" ]; then + echo "docs_changed=true" >> "$GITHUB_OUTPUT" +fi +exit 0