Preview by @lnhsingh #3
Workflow file for this run
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: Build Preview | |
| run-name: Preview by @${{ github.actor }} | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| workflow_dispatch: | |
| concurrency: | |
| group: build-preview-${{ github.ref_name }} | |
| cancel-in-progress: true | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| build-preview: | |
| runs-on: ubuntu-latest | |
| # Skip for PRs from forks - they don't have write access | |
| if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'workflow_dispatch' | |
| outputs: | |
| preview_branch: ${{ steps.branch-name.outputs.branch_name }} | |
| env: | |
| SOURCE_BRANCH: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }} | |
| GITHUB_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} | |
| steps: | |
| - name: Checkout PR branch | |
| uses: actions/checkout@v5 | |
| with: | |
| ref: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }} | |
| fetch-depth: 0 | |
| - name: Set up Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: '3.12' | |
| - name: Install uv | |
| uses: astral-sh/setup-uv@v6 | |
| - name: Install dependencies | |
| run: make install | |
| - name: Build documentation | |
| run: make build | |
| - name: Generate preview branch name | |
| id: branch-name | |
| run: | | |
| set -euo pipefail | |
| # Use PR number for cleaner branch names | |
| if [ "${{ github.event_name }}" = "pull_request" ]; then | |
| PR_NUM="${{ github.event.pull_request.number }}" | |
| PREVIEW_BRANCH="preview-pr-${PR_NUM}" | |
| else | |
| # For manual runs, use branch name + timestamp | |
| safe_prefix=$(echo "$SOURCE_BRANCH" | tr -cd '[:alnum:]' | cut -c1-10) | |
| timestamp=$(date +%s) | |
| PREVIEW_BRANCH="preview-${safe_prefix}-${timestamp}" | |
| fi | |
| echo "branch_name=$PREVIEW_BRANCH" >> $GITHUB_OUTPUT | |
| echo "Preview branch: $PREVIEW_BRANCH" | |
| - name: Configure Git | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| - name: Create or update preview branch with build artifacts | |
| run: | | |
| PREVIEW_BRANCH="${{ steps.branch-name.outputs.branch_name }}" | |
| # Check if preview branch already exists | |
| if git ls-remote --exit-code --heads origin "$PREVIEW_BRANCH" >/dev/null 2>&1; then | |
| echo "Preview branch exists, will update it" | |
| BRANCH_EXISTS=true | |
| else | |
| echo "Preview branch does not exist, will create it" | |
| BRANCH_EXISTS=false | |
| fi | |
| # Create preview branch from current HEAD | |
| git checkout -b "$PREVIEW_BRANCH" | |
| # Add build artifacts (force add since build/ is in .gitignore) | |
| git add -f build/ | |
| # Commit build artifacts | |
| git commit -m "Add build artifacts for preview deployment | |
| Source PR: #${{ github.event.pull_request.number }} | |
| Source branch: $SOURCE_BRANCH | |
| Source commit: $GITHUB_SHA | |
| Generated: $(date -u +"%Y-%m-%d %H:%M:%S UTC") | |
| This branch contains preprocessed documentation for preview deployment. | |
| Do not merge this branch." | |
| # Push preview branch (force push if updating existing branch) | |
| if [ "$BRANCH_EXISTS" = true ]; then | |
| git push --force origin "$PREVIEW_BRANCH" | |
| echo "Preview branch updated: $PREVIEW_BRANCH" | |
| else | |
| git push origin "$PREVIEW_BRANCH" | |
| echo "Preview branch created: $PREVIEW_BRANCH" | |
| fi | |
| comment-on-pr: | |
| runs-on: ubuntu-latest | |
| needs: build-preview | |
| if: github.event_name == 'pull_request' && needs.build-preview.result == 'success' | |
| permissions: | |
| pull-requests: write | |
| steps: | |
| - name: Comment on PR | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const previewBranch = `${{ needs.build-preview.outputs.preview_branch }}`; | |
| const repoUrl = context.payload.repository.html_url; | |
| const commentMarker = '<!-- preview-build-comment -->'; | |
| const previewUrl = `https://${previewBranch}--langchain-5e9cc07a.mintlify.app`; | |
| const commentBody = `${commentMarker} | |
| ## Preview Build Ready ✨ | |
| A preview branch with build artifacts has been created and will be automatically deployed by Mintlify. | |
| **Preview Branch:** \`${previewBranch}\` | |
| **Branch URL:** ${repoUrl}/tree/${previewBranch} | |
| ### View the Preview: | |
| 👉 **[View Preview Deployment](${previewUrl})** | |
| Or visit the [Mintlify Previews Dashboard](https://dashboard.mintlify.com/langchain-5e9cc07a/langchain-5e9cc07a?section=previews) to see all preview deployments. | |
| > **Note:** Your PR branch remains clean - build artifacts are only in the preview branch. | |
| _Last updated: ${new Date().toUTCString()}_`; | |
| // Find existing preview comment | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| }); | |
| const existingComment = comments.data.find(comment => | |
| comment.body.includes(commentMarker) | |
| ); | |
| if (existingComment) { | |
| // Update existing comment | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| body: commentBody | |
| }); | |
| } else { | |
| // Create new comment | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: commentBody | |
| }); | |
| } |