|
| 1 | +name: Deploy docs to Aliyun OSS |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Build docs"] |
| 6 | + types: [completed] |
| 7 | + workflow_dispatch: |
| 8 | + release: |
| 9 | + types: [published] |
| 10 | + pull_request_target: |
| 11 | + types: [closed] |
| 12 | + |
| 13 | +permissions: |
| 14 | + actions: read |
| 15 | + contents: read |
| 16 | + issues: write |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: docs-deploy |
| 20 | + cancel-in-progress: false |
| 21 | + |
| 22 | +env: |
| 23 | + ASTRO_SITE: https://vide.pascal-lab.net |
| 24 | + |
| 25 | +jobs: |
| 26 | + deploy-preview: |
| 27 | + name: Deploy PR preview |
| 28 | + if: > |
| 29 | + github.event_name == 'workflow_run' && |
| 30 | + github.event.workflow_run.event == 'pull_request' && |
| 31 | + github.event.workflow_run.conclusion == 'success' && |
| 32 | + vars.ENABLE_DOCS_DEPLOY == 'true' |
| 33 | + runs-on: ubuntu-latest |
| 34 | + steps: |
| 35 | + - name: Resolve preview target |
| 36 | + id: preview |
| 37 | + run: | |
| 38 | + set -euo pipefail |
| 39 | +
|
| 40 | + pr_number="$(jq -r '.workflow_run.pull_requests[0].number // empty' "${GITHUB_EVENT_PATH}")" |
| 41 | + if ! [[ "${pr_number}" =~ ^[0-9]+$ ]]; then |
| 42 | + echo "No pull request number found for workflow run ${GITHUB_RUN_ID}." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +
|
| 46 | + preview_path="preview/pr-${pr_number}/" |
| 47 | + preview_url="https://vide.pascal-lab.net/${preview_path}" |
| 48 | +
|
| 49 | + echo "pr_number=${pr_number}" >> "${GITHUB_OUTPUT}" |
| 50 | + echo "path=${preview_path}" >> "${GITHUB_OUTPUT}" |
| 51 | + echo "url=${preview_url}" >> "${GITHUB_OUTPUT}" |
| 52 | +
|
| 53 | + - name: Download preview artifact |
| 54 | + uses: actions/download-artifact@v4 |
| 55 | + with: |
| 56 | + name: vide-docs-preview |
| 57 | + run-id: ${{ github.event.workflow_run.id }} |
| 58 | + github-token: ${{ github.token }} |
| 59 | + path: preview-dist |
| 60 | + |
| 61 | + - name: Setup ossutil |
| 62 | + uses: manyuanrong/setup-ossutil@v2.0 |
| 63 | + with: |
| 64 | + endpoint: ${{ secrets.endpoint }} |
| 65 | + access-key-id: ${{ secrets.accessKeyId }} |
| 66 | + access-key-secret: ${{ secrets.accessKeySecret }} |
| 67 | + |
| 68 | + - name: Deploy preview to OSS |
| 69 | + env: |
| 70 | + OSS_BUCKET_NAME: ${{ secrets.bucketName }} |
| 71 | + PREVIEW_PATH: ${{ steps.preview.outputs.path }} |
| 72 | + PREVIEW_URL: ${{ steps.preview.outputs.url }} |
| 73 | + run: | |
| 74 | + set -euo pipefail |
| 75 | +
|
| 76 | + if [ -z "${OSS_BUCKET_NAME}" ]; then |
| 77 | + echo "OSS bucketName secret is required." |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +
|
| 81 | + case "${PREVIEW_PATH}" in |
| 82 | + preview/pr-[0-9]*/) ;; |
| 83 | + *) |
| 84 | + echo "Invalid preview path: ${PREVIEW_PATH}" |
| 85 | + exit 1 |
| 86 | + ;; |
| 87 | + esac |
| 88 | +
|
| 89 | + target="oss://${OSS_BUCKET_NAME}/${PREVIEW_PATH}" |
| 90 | + ossutil rm "${target}" -rf || true |
| 91 | + ossutil cp preview-dist "${target}" -rf |
| 92 | +
|
| 93 | + echo "Preview deployed to ${PREVIEW_URL}" >> "${GITHUB_STEP_SUMMARY}" |
| 94 | +
|
| 95 | + - name: Comment preview URL |
| 96 | + uses: actions/github-script@v8 |
| 97 | + env: |
| 98 | + PR_NUMBER: ${{ steps.preview.outputs.pr_number }} |
| 99 | + PREVIEW_URL: ${{ steps.preview.outputs.url }} |
| 100 | + with: |
| 101 | + script: | |
| 102 | + const prNumber = Number(process.env.PR_NUMBER); |
| 103 | + const previewUrl = process.env.PREVIEW_URL; |
| 104 | + const marker = `<!-- vide-docs-preview:${prNumber} -->`; |
| 105 | + const body = `${marker}\nDocs preview: ${previewUrl}`; |
| 106 | + const { owner, repo } = context.repo; |
| 107 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 108 | + owner, |
| 109 | + repo, |
| 110 | + issue_number: prNumber, |
| 111 | + per_page: 100, |
| 112 | + }); |
| 113 | + const existing = comments.find((comment) => comment.body?.includes(marker)); |
| 114 | + if (existing) { |
| 115 | + await github.rest.issues.updateComment({ |
| 116 | + owner, |
| 117 | + repo, |
| 118 | + comment_id: existing.id, |
| 119 | + body, |
| 120 | + }); |
| 121 | + } else { |
| 122 | + await github.rest.issues.createComment({ |
| 123 | + owner, |
| 124 | + repo, |
| 125 | + issue_number: prNumber, |
| 126 | + body, |
| 127 | + }); |
| 128 | + } |
| 129 | +
|
| 130 | + deploy-production: |
| 131 | + name: Deploy production docs |
| 132 | + if: > |
| 133 | + (github.event_name == 'release' && github.event.release.prerelease != true && vars.ENABLE_DOCS_DEPLOY == 'true') || |
| 134 | + (github.event_name == 'workflow_dispatch' && vars.ENABLE_DOCS_DEPLOY == 'true') |
| 135 | + runs-on: ubuntu-latest |
| 136 | + environment: |
| 137 | + name: docs |
| 138 | + url: https://vide.pascal-lab.net |
| 139 | + env: |
| 140 | + ASTRO_BASE: / |
| 141 | + steps: |
| 142 | + - name: Checkout |
| 143 | + uses: actions/checkout@v4 |
| 144 | + |
| 145 | + - name: Setup Node |
| 146 | + uses: actions/setup-node@v4 |
| 147 | + with: |
| 148 | + node-version: 22 |
| 149 | + cache: npm |
| 150 | + cache-dependency-path: docs/package-lock.json |
| 151 | + |
| 152 | + - name: Install dependencies |
| 153 | + run: npm ci |
| 154 | + working-directory: docs |
| 155 | + |
| 156 | + - name: Build docs |
| 157 | + run: npm run build |
| 158 | + working-directory: docs |
| 159 | + |
| 160 | + - name: Setup ossutil |
| 161 | + uses: manyuanrong/setup-ossutil@v2.0 |
| 162 | + with: |
| 163 | + endpoint: ${{ secrets.endpoint }} |
| 164 | + access-key-id: ${{ secrets.accessKeyId }} |
| 165 | + access-key-secret: ${{ secrets.accessKeySecret }} |
| 166 | + |
| 167 | + - name: Deploy production to OSS |
| 168 | + env: |
| 169 | + OSS_BUCKET_NAME: ${{ secrets.bucketName }} |
| 170 | + run: | |
| 171 | + set -euo pipefail |
| 172 | +
|
| 173 | + if [ -z "${OSS_BUCKET_NAME}" ]; then |
| 174 | + echo "OSS bucketName secret is required." |
| 175 | + exit 1 |
| 176 | + fi |
| 177 | +
|
| 178 | + for key in _astro pagefind advanced-guide changelog en playground schemas user-guide developer-guide staging 404.html index.html robots.txt sitemap-0.xml sitemap-index.xml; do |
| 179 | + ossutil rm "oss://${OSS_BUCKET_NAME}/${key}" -rf || true |
| 180 | + done |
| 181 | +
|
| 182 | + ossutil cp docs/dist "oss://${OSS_BUCKET_NAME}/" -rf |
| 183 | +
|
| 184 | + cleanup-preview: |
| 185 | + name: Remove closed PR preview |
| 186 | + if: > |
| 187 | + github.event_name == 'pull_request_target' && |
| 188 | + github.event.action == 'closed' && |
| 189 | + vars.ENABLE_DOCS_DEPLOY == 'true' |
| 190 | + runs-on: ubuntu-latest |
| 191 | + steps: |
| 192 | + - name: Setup ossutil |
| 193 | + uses: manyuanrong/setup-ossutil@v2.0 |
| 194 | + with: |
| 195 | + endpoint: ${{ secrets.endpoint }} |
| 196 | + access-key-id: ${{ secrets.accessKeyId }} |
| 197 | + access-key-secret: ${{ secrets.accessKeySecret }} |
| 198 | + |
| 199 | + - name: Delete preview from OSS |
| 200 | + env: |
| 201 | + OSS_BUCKET_NAME: ${{ secrets.bucketName }} |
| 202 | + PREVIEW_PATH: preview/pr-${{ github.event.pull_request.number }}/ |
| 203 | + run: | |
| 204 | + set -euo pipefail |
| 205 | +
|
| 206 | + if [ -z "${OSS_BUCKET_NAME}" ]; then |
| 207 | + echo "OSS bucketName secret is required." |
| 208 | + exit 1 |
| 209 | + fi |
| 210 | +
|
| 211 | + ossutil rm "oss://${OSS_BUCKET_NAME}/${PREVIEW_PATH}" -rf || true |
0 commit comments