|
| 1 | +name: Sync llms.txt to github/github |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + paths: |
| 9 | + - 'data/llms-txt-config.yml' |
| 10 | + - 'src/workflows/generate-llms-txt.ts' |
| 11 | + schedule: |
| 12 | + - cron: '20 16 * * 1-5' # Weekdays at ~9:20am Pacific |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}' |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +jobs: |
| 22 | + sync: |
| 23 | + name: Sync llms.txt |
| 24 | + if: github.repository == 'github/docs-internal' |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Checkout docs-internal |
| 28 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 29 | + with: |
| 30 | + persist-credentials: false |
| 31 | + |
| 32 | + - uses: ./.github/actions/node-npm-setup |
| 33 | + |
| 34 | + - name: Generate llms.txt |
| 35 | + env: |
| 36 | + DOCS_BOT_PAT_BASE: ${{ secrets.DOCS_BOT_PAT_BASE }} |
| 37 | + run: | |
| 38 | + echo "Generating llms.txt from page catalog and popularity data..." |
| 39 | + npm run generate-llms-txt --silent > /tmp/llms.txt |
| 40 | + echo "Generated llms.txt ($(wc -l < /tmp/llms.txt) lines, $(wc -c < /tmp/llms.txt) bytes)" |
| 41 | +
|
| 42 | + - name: Fetch current llms.txt from github/github |
| 43 | + id: fetch |
| 44 | + env: |
| 45 | + GH_TOKEN: ${{ secrets.DOCS_BOT_PAT_BASE }} |
| 46 | + run: | |
| 47 | + echo "Fetching current public/llms.txt from github/github..." |
| 48 | + if gh api repos/github/github/contents/public/llms.txt \ |
| 49 | + --jq '.content' 2>/dev/null | base64 -d > /tmp/current.txt; then |
| 50 | + echo "Fetched current file ($(wc -l < /tmp/current.txt) lines)" |
| 51 | + else |
| 52 | + echo "No existing file found (first run)" |
| 53 | + rm -f /tmp/current.txt |
| 54 | + fi |
| 55 | +
|
| 56 | + - name: Diff generated vs current |
| 57 | + id: diff |
| 58 | + run: | |
| 59 | + echo "Comparing generated llms.txt against current..." |
| 60 | + if [ -f /tmp/current.txt ] && diff -q /tmp/llms.txt /tmp/current.txt > /dev/null 2>&1; then |
| 61 | + echo "No changes detected, skipping push" |
| 62 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 63 | + else |
| 64 | + echo "Changes detected:" |
| 65 | + diff --unified=0 /tmp/current.txt /tmp/llms.txt | head -30 || true |
| 66 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 67 | + fi |
| 68 | +
|
| 69 | + - name: Ensure sync branch exists in github/github |
| 70 | + if: steps.diff.outputs.changed == 'true' |
| 71 | + env: |
| 72 | + GH_TOKEN: ${{ secrets.DOCS_BOT_PAT_BASE }} |
| 73 | + run: | |
| 74 | + BRANCH="auto/sync-llms-txt" |
| 75 | + REPO="github/github" |
| 76 | +
|
| 77 | + echo "Checking if branch '$BRANCH' exists..." |
| 78 | + BRANCH_SHA=$(gh api "repos/$REPO/git/ref/heads/$BRANCH" --jq '.object.sha' 2>/dev/null || true) |
| 79 | + if [ -n "$BRANCH_SHA" ]; then |
| 80 | + echo "Branch exists at $BRANCH_SHA" |
| 81 | + else |
| 82 | + echo "Branch does not exist, creating from default branch..." |
| 83 | + DEFAULT_BRANCH=$(gh api "repos/$REPO" --jq '.default_branch') |
| 84 | + BASE_SHA=$(gh api "repos/$REPO/git/ref/heads/$DEFAULT_BRANCH" --jq '.object.sha') |
| 85 | + gh api "repos/$REPO/git/refs" \ |
| 86 | + --method POST \ |
| 87 | + -f ref="refs/heads/$BRANCH" \ |
| 88 | + -f sha="$BASE_SHA" |
| 89 | + echo "Created branch from $DEFAULT_BRANCH at $BASE_SHA" |
| 90 | + fi |
| 91 | +
|
| 92 | + - name: Commit llms.txt to github/github |
| 93 | + if: steps.diff.outputs.changed == 'true' |
| 94 | + env: |
| 95 | + GH_TOKEN: ${{ secrets.DOCS_BOT_PAT_BASE }} |
| 96 | + run: | |
| 97 | + BRANCH="auto/sync-llms-txt" |
| 98 | + REPO="github/github" |
| 99 | + CONTENT=$(base64 -w 0 /tmp/llms.txt) |
| 100 | +
|
| 101 | + echo "Checking for existing file SHA on branch..." |
| 102 | + EXISTING_SHA=$(gh api "repos/$REPO/contents/public/llms.txt?ref=$BRANCH" \ |
| 103 | + --jq '.sha' 2>/dev/null || true) |
| 104 | + if [ -n "$EXISTING_SHA" ]; then |
| 105 | + echo "Existing file SHA: $EXISTING_SHA" |
| 106 | + else |
| 107 | + echo "No existing file on branch (new file)" |
| 108 | + fi |
| 109 | +
|
| 110 | + echo "Committing llms.txt to $REPO/$BRANCH..." |
| 111 | + COMMIT_ARGS=(-f "message=Sync llms.txt from docs.github.com" |
| 112 | + -f "content=$CONTENT" |
| 113 | + -f "branch=$BRANCH") |
| 114 | + if [ -n "$EXISTING_SHA" ]; then |
| 115 | + COMMIT_ARGS+=(-f "sha=$EXISTING_SHA") |
| 116 | + fi |
| 117 | + gh api "repos/$REPO/contents/public/llms.txt" \ |
| 118 | + --method PUT \ |
| 119 | + "${COMMIT_ARGS[@]}" --jq '.commit.sha' |
| 120 | + echo "Committed successfully" |
| 121 | +
|
| 122 | + - name: Create PR if needed |
| 123 | + if: steps.diff.outputs.changed == 'true' |
| 124 | + env: |
| 125 | + GH_TOKEN: ${{ secrets.DOCS_BOT_PAT_BASE }} |
| 126 | + run: | |
| 127 | + BRANCH="auto/sync-llms-txt" |
| 128 | + REPO="github/github" |
| 129 | +
|
| 130 | + echo "Checking for existing PR from '$BRANCH'..." |
| 131 | + EXISTING_PR=$(gh pr list --repo "$REPO" --head "$BRANCH" \ |
| 132 | + --json number --jq '.[0].number' 2>/dev/null || true) |
| 133 | + if [ -n "$EXISTING_PR" ]; then |
| 134 | + echo "PR #$EXISTING_PR already exists, updated with new commit" |
| 135 | + exit 0 |
| 136 | + fi |
| 137 | +
|
| 138 | + RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" |
| 139 | + DEFAULT_BRANCH=$(gh api "repos/$REPO" --jq '.default_branch') |
| 140 | +
|
| 141 | + PR_BODY="The [sync-llms-txt workflow]($RUN_URL) generated this PR. |
| 142 | +
|
| 143 | + Updates \`public/llms.txt\` served at \`github.com/llms.txt\`. The docs-internal script builds this file from the page catalog and popularity data. |
| 144 | +
|
| 145 | + No feature flags. Static file in \`public/\`, no code changes. |
| 146 | +
|
| 147 | + <!-- |
| 148 | + Labels for github/github PR template automation: |
| 149 | + (\`environment:production-dotcom\`) |
| 150 | + (\`risk:low\`) |
| 151 | + (\`validate:other\`) |
| 152 | + (\`mitigate:rollback\`) |
| 153 | + (\`backend/rails/api-only\`) |
| 154 | + pull_request_template_version=2 |
| 155 | + -->" |
| 156 | +
|
| 157 | + echo "Creating PR..." |
| 158 | + gh pr create \ |
| 159 | + --repo "$REPO" \ |
| 160 | + --title "Sync llms.txt from docs.github.com" \ |
| 161 | + --body "$PR_BODY" \ |
| 162 | + --head "$BRANCH" \ |
| 163 | + --base "$DEFAULT_BRANCH" \ |
| 164 | + --label "docs" |
| 165 | + echo "PR created successfully" |
| 166 | +
|
| 167 | + - uses: ./.github/actions/slack-alert |
| 168 | + if: ${{ failure() && github.event_name != 'workflow_dispatch' }} |
| 169 | + with: |
| 170 | + slack_channel_id: ${{ secrets.DOCS_ALERTS_SLACK_CHANNEL_ID }} |
| 171 | + slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} |
0 commit comments