Preview docs #137
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Preview docs | |
| on: | |
| workflow_run: | |
| workflows: [docs] | |
| types: [completed] | |
| env: | |
| DOCS_BUILD_DIR: /tmp/docs-build | |
| defaults: | |
| run: | |
| shell: bash | |
| jobs: | |
| main: | |
| if: github.repository == 'mlflow/mlflow' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write # To post comments on PRs | |
| actions: write # To delete artifacts | |
| timeout-minutes: 10 | |
| steps: | |
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 | |
| with: | |
| persist-credentials: false | |
| sparse-checkout: | | |
| .github | |
| - name: Set alias | |
| id: alias | |
| env: | |
| EVENT: ${{ github.event.workflow_run.event }} | |
| HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }} | |
| HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| if [ "$EVENT" = "push" ]; then | |
| ALIAS="dev" | |
| else | |
| # Derive PR number from the workflow run's head branch via API | |
| # instead of trusting the artifact (which fork PRs can manipulate) | |
| PR_NUMBER=$(gh api "repos/$REPO/pulls?head=$HEAD_OWNER:$HEAD_BRANCH&state=open" \ | |
| --jq '.[0].number // empty') | |
| if [ -z "$PR_NUMBER" ]; then | |
| echo "::error::Could not find PR for $HEAD_OWNER:$HEAD_BRANCH" | |
| exit 1 | |
| fi | |
| ALIAS="pr-$PR_NUMBER" | |
| fi | |
| echo "value=$ALIAS" >> $GITHUB_OUTPUT | |
| - name: Download build artifact | |
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| name: docs-build-${{ github.event.workflow_run.id }} | |
| path: ${{ env.DOCS_BUILD_DIR }} | |
| - uses: ./.github/actions/setup-node | |
| - name: Deploy to Netlify | |
| id: netlify_deploy | |
| env: | |
| NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} | |
| NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} | |
| ALIAS: ${{ steps.alias.outputs.value }} | |
| RUN_ID: ${{ github.run_id }} | |
| run: |- | |
| OUTPUT=$(npx --min-release-age=14 --yes netlify-cli deploy \ | |
| --dir="$DOCS_BUILD_DIR" \ | |
| --no-build \ | |
| --message="Preview ${ALIAS} - GitHub Run ID: $RUN_ID" \ | |
| --alias="${ALIAS}" \ | |
| --json) | |
| if ! DEPLOY_URL=$(echo "$OUTPUT" | jq -re '.deploy_url'); then | |
| echo "::error::Failed to parse deploy output" | |
| echo "$OUTPUT" | |
| exit 1 | |
| fi | |
| echo "deploy_url=${DEPLOY_URL}/docs/latest/" >> $GITHUB_OUTPUT | |
| continue-on-error: true | |
| - name: Extract PR number | |
| id: pr_number | |
| if: startsWith(steps.alias.outputs.value, 'pr-') | |
| env: | |
| ALIAS: ${{ steps.alias.outputs.value }} | |
| run: | | |
| # Extract number from alias (e.g., "pr-123" -> "123") | |
| PR_NUM=$(echo "$ALIAS" | sed 's/^pr-//') | |
| echo "value=$PR_NUM" >> $GITHUB_OUTPUT | |
| echo "Extracted PR number: $PR_NUM" | |
| - name: Create preview link | |
| if: startsWith(steps.alias.outputs.value, 'pr-') | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| env: | |
| COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} | |
| PULL_NUMBER: ${{ steps.pr_number.outputs.value }} | |
| WORKFLOW_RUN_ID: ${{ github.run_id }} | |
| STAGE: ${{ steps.netlify_deploy.outcome == 'success' && 'completed' || 'failed' }} | |
| NETLIFY_URL: ${{ steps.netlify_deploy.outputs.deploy_url }} | |
| DOCS_WORKFLOW_RUN_URL: ${{ github.event.workflow_run.html_url }} | |
| with: | |
| retries: 3 | |
| script: | | |
| const script = require( | |
| `${process.env.GITHUB_WORKSPACE}/.github/workflows/preview-comment.js` | |
| ); | |
| await script({ context, github, env: process.env }); | |
| - name: Delete Build Artifact | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| env: | |
| RUN_ID: ${{ github.event.workflow_run.id }} | |
| ARTIFACT_NAME: docs-build-${{ github.event.workflow_run.id }} | |
| with: | |
| retries: 3 | |
| script: | | |
| const script = require( | |
| `${process.env.GITHUB_WORKSPACE}/.github/workflows/delete-artifact.js` | |
| ); | |
| await script({ context, github, env: process.env }); |