From ed08b1855a2d6bc18138bbebc562e53eb8e0e262 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 1 Jul 2025 11:31:55 -0500 Subject: [PATCH] Add documentation publishing workflow (#730) Adds a documentation publishing workflow as a part of https://github.com/astral-sh/ty/pull/729 but is based on `main` so we can test the workflow dispatch against the branch. This is copied from ruff with updated names. --- .github/workflows/publish-docs.yml | 147 +++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 .github/workflows/publish-docs.yml diff --git a/.github/workflows/publish-docs.yml b/.github/workflows/publish-docs.yml new file mode 100644 index 0000000..d3aca00 --- /dev/null +++ b/.github/workflows/publish-docs.yml @@ -0,0 +1,147 @@ +# Publish the ty documentation. +# +# Assumed to run as a subworkflow of .github/workflows/release.yml; specifically, as a post-announce +# job within `cargo-dist`. +name: mkdocs + +on: + workflow_dispatch: + inputs: + ref: + description: "The commit SHA, tag, or branch to publish. Uses the default branch if not specified." + default: "" + type: string + workflow_call: + inputs: + plan: + required: true + type: string + +permissions: + contents: read + +jobs: + mkdocs: + runs-on: ubuntu-latest + env: + MKDOCS_INSIDERS_SSH_KEY_EXISTS: ${{ secrets.MKDOCS_INSIDERS_SSH_KEY != '' }} + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: ${{ inputs.ref }} + persist-credentials: true + + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 + with: + python-version: 3.12 + + - name: "Set docs version" + env: + version: ${{ (inputs.plan != '' && fromJson(inputs.plan).announcement_tag) || inputs.ref }} + run: | + # if version is missing, use 'latest' + if [ -z "$version" ]; then + echo "Using 'latest' as version" + version="latest" + fi + + # Use version as display name for now + display_name="$version" + + echo "version=$version" >> "$GITHUB_ENV" + echo "display_name=$display_name" >> "$GITHUB_ENV" + + - name: "Set branch name" + run: | + timestamp="$(date +%s)" + + # create branch_display_name from display_name by replacing all + # characters disallowed in git branch names with hyphens + branch_display_name="$(echo "${display_name}" | tr -c '[:alnum:]._' '-' | tr -s '-')" + + echo "branch_name=update-docs-$branch_display_name-$timestamp" >> "$GITHUB_ENV" + echo "timestamp=$timestamp" >> "$GITHUB_ENV" + + - name: "Add SSH key" + if: ${{ env.MKDOCS_INSIDERS_SSH_KEY_EXISTS == 'true' }} + uses: webfactory/ssh-agent@a6f90b1f127823b31d4d4a8d96047790581349bd # v0.9.1 + with: + ssh-private-key: ${{ secrets.MKDOCS_INSIDERS_SSH_KEY }} + + - name: "Install Rust toolchain" + run: rustup show + + - uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2.7.8 + + - name: "Install Insiders dependencies" + if: ${{ env.MKDOCS_INSIDERS_SSH_KEY_EXISTS == 'true' }} + run: pip install -r docs/requirements-insiders.txt + + - name: "Install dependencies" + if: ${{ env.MKDOCS_INSIDERS_SSH_KEY_EXISTS != 'true' }} + run: pip install -r docs/requirements.txt + + - name: "Copy README File" + run: | + python scripts/transform_readme.py --target mkdocs + python scripts/generate_mkdocs.py + + - name: "Build Insiders docs" + if: ${{ env.MKDOCS_INSIDERS_SSH_KEY_EXISTS == 'true' }} + run: mkdocs build --strict -f mkdocs.insiders.yml + + - name: "Build docs" + if: ${{ env.MKDOCS_INSIDERS_SSH_KEY_EXISTS != 'true' }} + run: mkdocs build --strict -f mkdocs.public.yml + + - name: "Clone docs repo" + run: git clone https://${{ secrets.ASTRAL_DOCS_PAT }}@github.com/astral-sh/docs.git astral-docs + + - name: "Copy docs" + run: rm -rf astral-docs/site/ty && mkdir -p astral-docs/site && cp -r site/ty astral-docs/site/ + + - name: "Commit docs" + working-directory: astral-docs + run: | + git config user.name "astral-docs-bot" + git config user.email "176161322+astral-docs-bot@users.noreply.github.com" + + git checkout -b "${branch_name}" + git add site/ty + git commit -m "Update ty documentation for $version" + + - name: "Create Pull Request" + working-directory: astral-docs + env: + GITHUB_TOKEN: ${{ secrets.ASTRAL_DOCS_PAT }} + run: | + # set the PR title + pull_request_title="Update ty documentation for ${display_name}" + + # Delete any existing pull requests that are open for this version + # by checking against pull_request_title because the new PR will + # supersede the old one. + gh pr list --state open --json title --jq '.[] | select(.title == "$pull_request_title") | .number' | \ + xargs -I {} gh pr close {} + + # push the branch to GitHub + git push origin "${branch_name}" + + # create the PR + gh pr create \ + --base=main \ + --head="${branch_name}" \ + --title="${pull_request_title}" \ + --body="Automated documentation update for ${display_name}" \ + --label="documentation" + + - name: "Merge Pull Request" + if: ${{ inputs.plan != '' && !fromJson(inputs.plan).announcement_tag_is_implicit }} + working-directory: astral-docs + env: + GITHUB_TOKEN: ${{ secrets.ASTRAL_DOCS_PAT }} + run: | + # auto-merge the PR if the build was triggered by a release. Manual builds should be reviewed by a human. + # give the PR a few seconds to be created before trying to auto-merge it + sleep 10 + gh pr merge --squash "${branch_name}"