name: Preview docs on: workflow_run: workflows: [docs] types: [completed] env: DOCS_BUILD_DIR: /tmp/docs-build defaults: run: shell: bash permissions: {} 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@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 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=7 --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 env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} REPO: ${{ github.repository }} RUN_ID: ${{ github.event.workflow_run.id }} ARTIFACT_NAME: docs-build-${{ github.event.workflow_run.id }} run: | ARTIFACT_ID=$(gh api "repos/$REPO/actions/runs/$RUN_ID/artifacts?name=$ARTIFACT_NAME" \ --jq '.artifacts[0].id // empty') if [ -z "$ARTIFACT_ID" ]; then echo "::error::Artifact $ARTIFACT_NAME not found for run $RUN_ID" exit 1 fi gh api -X DELETE "repos/$REPO/actions/artifacts/$ARTIFACT_ID"