diff --git a/.github/workflows/pr-docs-preview.yml b/.github/workflows/pr-docs-preview.yml new file mode 100644 index 000000000..0fb4c02ef --- /dev/null +++ b/.github/workflows/pr-docs-preview.yml @@ -0,0 +1,131 @@ +name: Deploy PR Docs Preview + +on: + pull_request: + types: [opened, synchronize, reopened, closed] + +permissions: + contents: write + pull-requests: write + +concurrency: + group: pr-docs-preview-${{ github.event.number }} + cancel-in-progress: true + +jobs: + deploy-preview: + if: github.event.action != 'closed' + runs-on: ubuntu-latest + steps: + - name: Check out + uses: actions/checkout@v4 + + - name: Check out gh-pages + uses: actions/checkout@v4 + with: + ref: gh-pages + path: gh-pages-deploy + + - name: Set up the environment + uses: ./.github/actions/setup-python-env + + - name: Install Pandoc + run: | + sudo apt-get update + sudo apt-get install -y pandoc + + - name: Determine latest version + id: version + run: | + VERSION=$(uv run python -c "from scripts.config import LATEST_VERSION; print(LATEST_VERSION)") + SHORT=$(uv run python -c "from scripts.config import LATEST_VERSION, version_to_short; print(version_to_short(LATEST_VERSION))") + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + echo "version_short=${SHORT}" >> "$GITHUB_OUTPUT" + + - name: Clone EnergyPlus source (sparse, doc/ only) + run: | + mkdir -p build/sources + git clone --filter=blob:none --no-checkout --depth=1 \ + --branch ${{ steps.version.outputs.version }} --single-branch \ + https://github.com/NatLabRockies/EnergyPlus.git \ + build/sources/${{ steps.version.outputs.version }} + git -C build/sources/${{ steps.version.outputs.version }} sparse-checkout set doc + git -C build/sources/${{ steps.version.outputs.version }} checkout + + - name: Convert docs + run: | + uv run python -m scripts.convert \ + --source build/sources/${{ steps.version.outputs.version }} \ + --output build/${{ steps.version.outputs.version_short }} \ + --version ${{ steps.version.outputs.version }} \ + --verbose + + - name: Deploy preview to gh-pages + run: | + rm -rf "gh-pages-deploy/pr-preview/pr-${{ github.event.number }}" + mkdir -p "gh-pages-deploy/pr-preview/pr-${{ github.event.number }}" + cp -r build/${{ steps.version.outputs.version_short }}/site/. "gh-pages-deploy/pr-preview/pr-${{ github.event.number }}/" + cd gh-pages-deploy + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add -A + git diff --cached --quiet || { + git commit -m "Deploy docs preview for PR #${{ github.event.number }}" + git pull --rebase origin gh-pages + git push origin gh-pages + } + + - name: Add or update preview comment + uses: actions/github-script@v7 + with: + script: | + const previewUrl = `https://docs.idfkit.com/pr-preview/pr-${context.issue.number}/`; + const marker = ''; + const body = [ + marker, + `**Docs preview** for this PR is available at:`, + previewUrl, + ].join('\n'); + + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + }); + const existing = comments.find(c => c.body.includes(marker)); + + if (existing) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existing.id, + body, + }); + } else { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body, + }); + } + + cleanup-preview: + if: github.event.action == 'closed' + runs-on: ubuntu-latest + steps: + - name: Check out gh-pages branch + uses: actions/checkout@v4 + with: + ref: gh-pages + + - name: Remove preview directory + run: | + if [ -d "pr-preview/pr-${{ github.event.number }}" ]; then + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git rm -rf "pr-preview/pr-${{ github.event.number }}" + git commit -m "Remove docs preview for PR #${{ github.event.number }}" + git pull --rebase origin gh-pages + git push origin gh-pages + fi