|
1 | 1 | name: Site build preview |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
5 | | - types: [opened, synchronize] |
6 | | - branches: |
7 | | - - main |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize] |
| 6 | + branches: |
| 7 | + - main |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
8 | 12 |
|
9 | 13 | env: |
10 | | - SYNC_REPO: ${{ secrets.SYNC_REPO }} |
11 | | - SYNC_DIRS: "api-design docs guides mcp openapi" |
12 | | - BRANCH_NAME: docs-preview/pr-${{ github.event.pull_request.number }} |
| 14 | + SYNC_REPO: ${{ secrets.SYNC_REPO }} |
| 15 | + SYNC_DIRS: "api-design docs guides mcp openapi" |
| 16 | + BRANCH_NAME: docs-preview/pr-${{ github.event.pull_request.number }} |
| 17 | + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} |
| 18 | + VERCEL_TEAM_ID: ${{ secrets.VERCEL_TEAM_ID }} |
| 19 | + VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }} |
| 20 | + VERCEL_PROJECT_NAME: ${{ secrets.VERCEL_PROJECT_NAME }} |
| 21 | + VERCEL_GIT_REPO_ID: ${{ secrets.VERCEL_GIT_REPO_ID }} |
| 22 | + |
| 23 | +concurrency: |
| 24 | + group: preview-${{ github.event.pull_request.number }} |
| 25 | + cancel-in-progress: true |
13 | 26 |
|
14 | 27 | jobs: |
15 | | - preview-sync: |
16 | | - runs-on: ubuntu-latest |
17 | | - |
18 | | - steps: |
19 | | - - name: Checkout developer-docs PR |
20 | | - uses: actions/checkout@v4 |
21 | | - with: |
22 | | - fetch-depth: 0 |
23 | | - |
24 | | - - name: Clone marketing-site preview branch |
25 | | - env: |
26 | | - TOKEN: ${{ secrets.SERVICE_BOT_TOKEN }} |
27 | | - run: | |
28 | | - git clone https://x-access-token:${TOKEN}@github.com/${SYNC_REPO}.git private-repo |
29 | | - cd private-repo |
30 | | - git checkout -B $BRANCH_NAME |
31 | | -
|
32 | | - - name: Sync content folders to preview branch |
33 | | - run: | |
34 | | - set -euo pipefail |
35 | | - for dir in $SYNC_DIRS; do |
36 | | - src_dir="$dir" |
37 | | - dest_dir="private-repo/src/content/$dir" |
38 | | - if [ -d "$src_dir" ]; then |
39 | | - echo "Syncing $src_dir → $dest_dir" |
40 | | - rsync -a --delete "$src_dir/" "$dest_dir/" |
41 | | - else |
42 | | - echo "Warning: $src_dir does not exist, skipping..." |
43 | | - fi |
44 | | - done |
45 | | -
|
46 | | - - name: Commit and push preview branch |
47 | | - run: | |
48 | | - set -euo pipefail |
49 | | - cd private-repo |
50 | | - git config user.name "Beezy the bot" |
51 | | - git config user.email "[email protected]" |
52 | | - git add . |
53 | | - if git diff --cached --quiet; then |
54 | | - echo "No changes to sync for preview" |
55 | | - exit 0 |
56 | | - fi |
57 | | - git commit -m "🔍 Preview sync from developer-docs PR #${{ github.event.pull_request.number }}" |
58 | | - git push origin $BRANCH_NAME --force |
59 | | -
|
60 | | - comment-vercel-preview: |
61 | | - needs: preview-sync |
62 | | - runs-on: ubuntu-latest |
63 | | - |
64 | | - steps: |
65 | | - - name: Comment with Predicted Vercel URL |
66 | | - uses: marocchino/sticky-pull-request-comment@v2 |
67 | | - with: |
68 | | - number: ${{ github.event.pull_request.number }} |
69 | | - header: VercelPreview |
70 | | - message: | |
71 | | - 🔗 **Preview your changes** |
72 | | - [https://speakeasycom-git-docs-preview-pr-${{ github.event.pull_request.number }}-speakeasyapi.vercel.app](https://speakeasycom-git-docs-preview-pr-${{ github.event.pull_request.number }}-speakeasyapi.vercel.app) |
73 | | - _(The preview may still be building. Check back at this link in a few minutes.)_ |
| 28 | + preview-sync: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + outputs: |
| 31 | + changed: ${{ steps.diff.outputs.changed }} |
| 32 | + |
| 33 | + steps: |
| 34 | + - name: Checkout developer-docs PR head |
| 35 | + uses: actions/checkout@v4 |
| 36 | + with: |
| 37 | + fetch-depth: 0 |
| 38 | + ref: ${{ github.event.pull_request.head.sha }} |
| 39 | + |
| 40 | + - name: Clone marketing-site preview branch |
| 41 | + env: |
| 42 | + TOKEN: ${{ secrets.SERVICE_BOT_TOKEN }} |
| 43 | + run: | |
| 44 | + set -euo pipefail |
| 45 | + git clone "https://x-access-token:${TOKEN}@github.com/${SYNC_REPO}.git" private-repo |
| 46 | + cd private-repo |
| 47 | + git checkout -B "$BRANCH_NAME" |
| 48 | +
|
| 49 | + - name: Prepare destination dirs |
| 50 | + run: | |
| 51 | + set -euo pipefail |
| 52 | + for dir in $SYNC_DIRS; do |
| 53 | + mkdir -p "private-repo/src/content/$dir" |
| 54 | + done |
| 55 | +
|
| 56 | + - name: Sync content folders to preview branch |
| 57 | + run: | |
| 58 | + set -euo pipefail |
| 59 | + for dir in $SYNC_DIRS; do |
| 60 | + src_dir="$dir" |
| 61 | + dest_dir="private-repo/src/content/$dir" |
| 62 | + if [ -d "$src_dir" ]; then |
| 63 | + echo "Syncing $src_dir → $dest_dir" |
| 64 | + rsync -a --delete "$src_dir/" "$dest_dir/" |
| 65 | + else |
| 66 | + echo "Warning: $src_dir does not exist, skipping..." |
| 67 | + fi |
| 68 | + done |
| 69 | +
|
| 70 | + - name: Commit and push preview branch |
| 71 | + id: diff |
| 72 | + run: | |
| 73 | + set -euo pipefail |
| 74 | + cd private-repo |
| 75 | + git config user.name "Beezy the bot" |
| 76 | + git config user.email "[email protected]" |
| 77 | + git add . |
| 78 | +
|
| 79 | + if git diff --cached --quiet; then |
| 80 | + echo "No changes to sync for preview" |
| 81 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | +
|
| 85 | + git commit -m "🔍 Preview sync from developer-docs PR #${{ github.event.pull_request.number }}" |
| 86 | + git push origin "$BRANCH_NAME" --force |
| 87 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 88 | +
|
| 89 | + create-vercel-deployment: |
| 90 | + needs: preview-sync |
| 91 | + if: needs.preview-sync.outputs.changed == 'true' |
| 92 | + runs-on: ubuntu-latest |
| 93 | + outputs: |
| 94 | + deployment_url: ${{ steps.trigger.outputs.deployment_url }} |
| 95 | + |
| 96 | + steps: |
| 97 | + - name: Install jq |
| 98 | + run: | |
| 99 | + sudo apt-get update |
| 100 | + sudo apt-get install -y jq |
| 101 | +
|
| 102 | + - name: Trigger Vercel deployment |
| 103 | + id: trigger |
| 104 | + env: |
| 105 | + SYNC_REPO: ${{ env.SYNC_REPO }} |
| 106 | + BRANCH_NAME: ${{ env.BRANCH_NAME }} |
| 107 | + VERCEL_TOKEN: ${{ env.VERCEL_TOKEN }} |
| 108 | + VERCEL_TEAM_ID: ${{ env.VERCEL_TEAM_ID }} |
| 109 | + VERCEL_PROJECT_ID: ${{ env.VERCEL_PROJECT_ID }} |
| 110 | + VERCEL_PROJECT_NAME: ${{ env.VERCEL_PROJECT_NAME }} |
| 111 | + VERCEL_GIT_REPO_ID: ${{ env.VERCEL_GIT_REPO_ID }} |
| 112 | + run: | |
| 113 | + set -euo pipefail |
| 114 | +
|
| 115 | + required=(VERCEL_TOKEN VERCEL_PROJECT_ID VERCEL_PROJECT_NAME) |
| 116 | + for var in "${required[@]}"; do |
| 117 | + if [ -z "${!var:-}" ]; then |
| 118 | + echo "Missing required secret: $var" >&2 |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + done |
| 122 | +
|
| 123 | + if [[ -z "${SYNC_REPO:-}" || "$SYNC_REPO" != */* ]]; then |
| 124 | + echo "Invalid SYNC_REPO value: ${SYNC_REPO:-<empty>}" >&2 |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | +
|
| 128 | + org="${SYNC_REPO%%/*}" |
| 129 | + repo="${SYNC_REPO#*/}" |
| 130 | +
|
| 131 | + payload=$(jq -n \ |
| 132 | + --arg name "$VERCEL_PROJECT_NAME" \ |
| 133 | + --arg project "$VERCEL_PROJECT_ID" \ |
| 134 | + --arg org "$org" \ |
| 135 | + --arg repo "$repo" \ |
| 136 | + --arg ref "$BRANCH_NAME" \ |
| 137 | + '{ |
| 138 | + name: $name, |
| 139 | + project: $project, |
| 140 | + target: "staging", |
| 141 | + gitSource: { |
| 142 | + type: "github", |
| 143 | + org: $org, |
| 144 | + repo: $repo, |
| 145 | + ref: $ref |
| 146 | + } |
| 147 | + }') |
| 148 | +
|
| 149 | + if [ -n "${VERCEL_GIT_REPO_ID:-}" ]; then |
| 150 | + payload=$(printf '%s' "$payload" | jq --arg repoId "$VERCEL_GIT_REPO_ID" '.gitSource.repoId = $repoId') |
| 151 | + fi |
| 152 | +
|
| 153 | + api_url="https://api.vercel.com/v13/deployments" |
| 154 | + if [ -n "${VERCEL_TEAM_ID:-}" ]; then |
| 155 | + api_url="${api_url}?teamId=${VERCEL_TEAM_ID}" |
| 156 | + fi |
| 157 | +
|
| 158 | + response=$(curl -sS -X POST "$api_url" \ |
| 159 | + -H "Authorization: Bearer ${VERCEL_TOKEN}" \ |
| 160 | + -H "Content-Type: application/json" \ |
| 161 | + -d "$payload") |
| 162 | +
|
| 163 | + if echo "$response" | jq -e '.error' >/dev/null; then |
| 164 | + echo "Vercel API error:" >&2 |
| 165 | + echo "$response" | jq -r '.error.message // .' >&2 |
| 166 | + exit 1 |
| 167 | + fi |
| 168 | +
|
| 169 | + url=$(echo "$response" | jq -r '.url // empty') |
| 170 | + if [ -z "$url" ]; then |
| 171 | + echo "Vercel API response did not include a deployment URL." >&2 |
| 172 | + echo "$response" >&2 |
| 173 | + exit 1 |
| 174 | + fi |
| 175 | +
|
| 176 | + if [[ "$url" == http*://* ]]; then |
| 177 | + full_url="$url" |
| 178 | + else |
| 179 | + full_url="https://$url" |
| 180 | + fi |
| 181 | +
|
| 182 | + echo "deployment_url=$full_url" >> "$GITHUB_OUTPUT" |
| 183 | +
|
| 184 | + comment-vercel-preview: |
| 185 | + needs: [preview-sync, create-vercel-deployment] |
| 186 | + if: needs.preview-sync.outputs.changed == 'true' |
| 187 | + runs-on: ubuntu-latest |
| 188 | + steps: |
| 189 | + - name: Comment with Vercel preview URL |
| 190 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 191 | + with: |
| 192 | + number: ${{ github.event.pull_request.number }} |
| 193 | + header: VercelPreview |
| 194 | + message: | |
| 195 | + 🔗 **Preview your changes** |
| 196 | + [${{ needs.create-vercel-deployment.outputs.deployment_url }}](${{ needs.create-vercel-deployment.outputs.deployment_url }}) |
| 197 | + _(The preview may still be building. Check back at this link in a few minutes.)_ |
0 commit comments