diff --git a/.github/workflows/pr-preview-cleanup.yml b/.github/workflows/pr-preview-cleanup.yml new file mode 100644 index 00000000..b21f4713 --- /dev/null +++ b/.github/workflows/pr-preview-cleanup.yml @@ -0,0 +1,43 @@ +name: PR Preview Cleanup + +on: + pull_request_target: + types: [closed] + +permissions: + contents: write + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: gh-pages + fetch-depth: 0 + + - name: Remove PR preview directory + run: | + PR_NUM=${{ github.event.pull_request.number }} + PREVIEW_DIR="pr-${PR_NUM}" + + if [ -d "$PREVIEW_DIR" ]; then + echo "Removing preview directory: $PREVIEW_DIR" + rm -rf "$PREVIEW_DIR" + + # Check if there are any changes to commit + if [ -n "$(git status --porcelain)" ]; then + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add -A + git commit -m "Remove preview for PR #${PR_NUM}" + git push + echo "Removed preview directory: $PREVIEW_DIR" + else + echo "No changes to commit" + fi + else + echo "Preview directory $PREVIEW_DIR does not exist, skipping cleanup" + fi + diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml new file mode 100644 index 00000000..4ffac92b --- /dev/null +++ b/.github/workflows/pr-preview.yml @@ -0,0 +1,108 @@ +name: PR Preview Build and Deploy + +on: + pull_request_target: + types: [opened, synchronize, reopened] + +permissions: + contents: write + pull-requests: write + pages: write + id-token: write + +concurrency: + group: pr-preview-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout base repository + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.base.ref }} + fetch-depth: 0 + + - name: Checkout PR branch + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + path: pr-source + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.1' + bundler-cache: true + + - name: Install Asciidoctor + run: gem install asciidoctor + + - name: Build HTML from AsciiDoc + working-directory: pr-source/supplementary_style_guide + run: asciidoctor main.adoc + + - name: Prepare preview directory + run: | + PR_NUM=${{ github.event.pull_request.number }} + PREVIEW_DIR="deploy/pr-${PR_NUM}" + mkdir -p "${PREVIEW_DIR}" + + # Copy the generated HTML file from PR source + cp pr-source/supplementary_style_guide/main.html "${PREVIEW_DIR}/" + + # Copy images directory preserving structure + if [ -d "pr-source/supplementary_style_guide/images" ]; then + cp -r pr-source/supplementary_style_guide/images "${PREVIEW_DIR}/" + fi + + # Copy any other image files preserving directory structure + find pr-source/supplementary_style_guide -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" -o -name "*.gif" -o -name "*.svg" \) | while read -r file; do + rel_path="${file#pr-source/supplementary_style_guide/}" + target_dir="${PREVIEW_DIR}/$(dirname "$rel_path")" + mkdir -p "$target_dir" + cp "$file" "$target_dir/" + done + + - name: Deploy to GitHub Pages + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./deploy/pr-${{ github.event.pull_request.number }} + destination_dir: pr-${{ github.event.pull_request.number }} + keep_files: true + + - name: Generate preview URL + id: preview-url + run: | + REPO_NAME="${{ github.repository }}" + PREVIEW_URL="https://${{ github.repository_owner }}.github.io/${REPO_NAME#*/}/pr-${{ github.event.pull_request.number }}/main.html" + echo "url=${PREVIEW_URL}" >> $GITHUB_OUTPUT + echo "Preview URL: ${PREVIEW_URL}" + + - name: Find existing comment + uses: peter-evans/find-comment@v3 + id: find-comment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: 'Preview Build' + + - name: Create or update PR comment + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.find-comment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + ## Preview Build + + A preview of this PR has been built and deployed. + + **Preview URL:** [${{ steps.preview-url.outputs.url }}](${{ steps.preview-url.outputs.url }}) + + This preview will be updated automatically when new commits are pushed to this PR. + + --- + *This comment is automatically generated by the PR Preview workflow.* +