|
| 1 | +name: PR Preview Build and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: write |
| 10 | + pages: write |
| 11 | + id-token: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: pr-preview-${{ github.event.pull_request.number }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + build-and-deploy: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - name: Checkout base repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + ref: ${{ github.event.pull_request.base.ref }} |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Checkout PR branch |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + ref: ${{ github.event.pull_request.head.sha }} |
| 31 | + path: pr-source |
| 32 | + |
| 33 | + - name: Set up Ruby |
| 34 | + uses: ruby/setup-ruby@v1 |
| 35 | + with: |
| 36 | + ruby-version: '3.1' |
| 37 | + bundler-cache: true |
| 38 | + |
| 39 | + - name: Install Asciidoctor |
| 40 | + run: gem install asciidoctor |
| 41 | + |
| 42 | + - name: Build HTML from AsciiDoc |
| 43 | + working-directory: pr-source/supplementary_style_guide |
| 44 | + run: asciidoctor main.adoc |
| 45 | + |
| 46 | + - name: Prepare preview directory |
| 47 | + run: | |
| 48 | + PR_NUM=${{ github.event.pull_request.number }} |
| 49 | + PREVIEW_DIR="deploy/pr-${PR_NUM}" |
| 50 | + mkdir -p "${PREVIEW_DIR}" |
| 51 | +
|
| 52 | + # Copy the generated HTML file from PR source |
| 53 | + cp pr-source/supplementary_style_guide/main.html "${PREVIEW_DIR}/" |
| 54 | +
|
| 55 | + # Copy images directory preserving structure |
| 56 | + if [ -d "pr-source/supplementary_style_guide/images" ]; then |
| 57 | + cp -r pr-source/supplementary_style_guide/images "${PREVIEW_DIR}/" |
| 58 | + fi |
| 59 | +
|
| 60 | + # Copy any other image files preserving directory structure |
| 61 | + 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 |
| 62 | + rel_path="${file#pr-source/supplementary_style_guide/}" |
| 63 | + target_dir="${PREVIEW_DIR}/$(dirname "$rel_path")" |
| 64 | + mkdir -p "$target_dir" |
| 65 | + cp "$file" "$target_dir/" |
| 66 | + done |
| 67 | +
|
| 68 | + - name: Deploy to GitHub Pages |
| 69 | + uses: peaceiris/actions-gh-pages@v3 |
| 70 | + with: |
| 71 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 72 | + publish_dir: ./deploy/pr-${{ github.event.pull_request.number }} |
| 73 | + destination_dir: pr-${{ github.event.pull_request.number }} |
| 74 | + keep_files: true |
| 75 | + |
| 76 | + - name: Generate preview URL |
| 77 | + id: preview-url |
| 78 | + run: | |
| 79 | + REPO_NAME="${{ github.repository }}" |
| 80 | + PREVIEW_URL="https://${{ github.repository_owner }}.github.io/${REPO_NAME#*/}/pr-${{ github.event.pull_request.number }}/main.html" |
| 81 | + echo "url=${PREVIEW_URL}" >> $GITHUB_OUTPUT |
| 82 | + echo "Preview URL: ${PREVIEW_URL}" |
| 83 | +
|
| 84 | + - name: Find existing comment |
| 85 | + uses: peter-evans/find-comment@v3 |
| 86 | + id: find-comment |
| 87 | + with: |
| 88 | + issue-number: ${{ github.event.pull_request.number }} |
| 89 | + comment-author: 'github-actions[bot]' |
| 90 | + body-includes: 'Preview Build' |
| 91 | + |
| 92 | + - name: Create or update PR comment |
| 93 | + uses: peter-evans/create-or-update-comment@v4 |
| 94 | + with: |
| 95 | + comment-id: ${{ steps.find-comment.outputs.comment-id }} |
| 96 | + issue-number: ${{ github.event.pull_request.number }} |
| 97 | + body: | |
| 98 | + ## Preview Build |
| 99 | +
|
| 100 | + A preview of this PR has been built and deployed. |
| 101 | +
|
| 102 | + **Preview URL:** [${{ steps.preview-url.outputs.url }}](${{ steps.preview-url.outputs.url }}) |
| 103 | +
|
| 104 | + This preview will be updated automatically when new commits are pushed to this PR. |
| 105 | +
|
| 106 | + --- |
| 107 | + *This comment is automatically generated by the PR Preview workflow.* |
| 108 | +
|
0 commit comments