|
| 1 | +name: Build and Preview Manual |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + pull_request: |
| 6 | + types: [opened, synchronize, reopened, closed] |
| 7 | + paths: |
| 8 | + - ".github/workflows/docs-preview.yml" |
| 9 | + - "modules/**" |
| 10 | + - "docs/**" |
| 11 | + |
| 12 | +# Defining permissions here passes it to all workflows. |
| 13 | +# https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/controlling-permissions-for-github_token |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + pull-requests: write |
| 17 | + issues: write |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + build-preview: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Install Nix |
| 28 | + uses: DeterminateSystems/nix-installer-action@main |
| 29 | + - uses: DeterminateSystems/magic-nix-cache-action@main |
| 30 | + |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Set default git branch (to reduce log spam) |
| 35 | + run: git config --global init.defaultBranch main |
| 36 | + |
| 37 | + - name: Build documentation packages |
| 38 | + run: nix build .#docs-html --print-build-logs |
| 39 | + |
| 40 | + - name: Deploy to GitHub Pages preview |
| 41 | + run: | |
| 42 | + PR_NUMBER=${{ github.event.pull_request.number }} |
| 43 | + BRANCH_NAME="gh-pages" |
| 44 | + PREVIEW_DIR="docs-preview-${PR_NUMBER}" |
| 45 | +
|
| 46 | + # Clone the gh-pages branch and move to the preview subdirectory |
| 47 | + git clone --single-branch --branch $BRANCH_NAME https://github.com/${{ github.repository }} gh-pages |
| 48 | + cd gh-pages |
| 49 | +
|
| 50 | + mkdir -p $PREVIEW_DIR |
| 51 | +
|
| 52 | + # Copy the build files to the preview subdirectory |
| 53 | + cp -rvf ../result/share/doc/nvf/* ./$PREVIEW_DIR |
| 54 | +
|
| 55 | + # Configure git to use the GitHub Actions token for authentication |
| 56 | + git config --global user.name "GitHub Actions" |
| 57 | + git config --global user.email "actions@github.com" |
| 58 | +
|
| 59 | + # Set the GitHub token for authentication |
| 60 | + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} |
| 61 | +
|
| 62 | + # Add and commit the changes |
| 63 | + git add --all |
| 64 | + git commit -m "Deploy PR #${PR_NUMBER} preview" || echo "No changes to commit" |
| 65 | + git push --force origin $BRANCH_NAME |
| 66 | +
|
| 67 | + comment-url: |
| 68 | + needs: build-preview |
| 69 | + runs-on: ubuntu-latest |
| 70 | + steps: |
| 71 | + - name: Prepare Environment |
| 72 | + id: prelude |
| 73 | + run: | |
| 74 | + PR_NUMBER=${{ github.event.pull_request.number }} |
| 75 | + URL="https://${{ github.repository_owner }}.github.io/nvf/docs-preview-${PR_NUMBER}/" |
| 76 | +
|
| 77 | + # Propagate non-interpolatable environment vars |
| 78 | + echo "URL=$URL" >> "$GITHUB_OUTPUT" |
| 79 | + echo "REV=$GITHUB_SHA" >> "$GITHUB_OUTPUT" |
| 80 | + echo "ACTOR=$GITHUB_ACTOR" >> "$GITHUB_OUTPUT" |
| 81 | + echo "REF=$GITHUB_HEAD_REF" >> "$GITHUB_OUTPUT" |
| 82 | + echo "RUNS=$GITHUB_RUN_NUMBER" >> "$GITHUB_OUTPUT" |
| 83 | +
|
| 84 | + echo "Live Preview URL: $URL" |
| 85 | + echo "Rev: $GITHUB_SHA" |
| 86 | + echo "Actor: $GITHUB_ACTOR" |
| 87 | + echo "Ref: "$GITHUB_HEAD_REF" |
| 88 | + echo "Reruns: "$GITHUB_RUN_NUMBER" |
| 89 | +
|
| 90 | + echo "### :rocket: Live Preview Deployed " >> "$GITHUB_STEP_SUMMARY" |
| 91 | + echo "Preview can be found at ${URL}" >> "$GITHUB_STEP_SUMMARY" |
| 92 | +
|
| 93 | + - name: Find Comment |
| 94 | + uses: peter-evans/find-comment@v3 |
| 95 | + id: fc |
| 96 | + with: |
| 97 | + comment-author: "github-actions[bot]" |
| 98 | + issue-number: ${{ github.event.pull_request.number }} |
| 99 | + body-includes: "Live preview deployed" |
| 100 | + |
| 101 | + - name: Post live preview comment |
| 102 | + uses: peter-evans/create-or-update-comment@v4 |
| 103 | + env: |
| 104 | + COMMENT_ID: ${{ steps.fc.outputs.comment-id }} |
| 105 | + URL: ${{ steps.prelude.outputs.URL }} |
| 106 | + GITHUB_SHA: ${{ steps.prelude.outputs.REV }} |
| 107 | + ACTOR: ${{ steps.prelude.outputs.ACTOR }} |
| 108 | + REF: ${{ steps.prelude.outputs.REF }} |
| 109 | + RUNS: ${{ steps.prelude.outputs.RUNS }} |
| 110 | + with: |
| 111 | + comment-id: ${{ env.COMMENT_ID }} |
| 112 | + issue-number: ${{ github.event.pull_request.number }} # issue number also applies to pull requests |
| 113 | + edit-mode: replace # replace previous body |
| 114 | + body: | |
| 115 | + ### :rocket: Live preview deployed from ${{ env.GITHUB_SHA }} |
| 116 | +
|
| 117 | + View it [here](${{ env.URL }}): |
| 118 | +
|
| 119 | + <details> |
| 120 | + <summary><strong>Debug Information</strong></summary> |
| 121 | + <p>Triggered by: ${{ env.ACTOR }}</p> |
| 122 | + <p><code>HEAD</code> at: ${{ env.REF }}</p> |
| 123 | + <p>Reruns: ${{ env.RUNS }}</p> |
| 124 | + </details> |
| 125 | +
|
| 126 | + cleanup: |
| 127 | + if: ${{ github.event.pull_request.merged == true || github.event.pull_request.state == 'closed' }} |
| 128 | + runs-on: ubuntu-latest |
| 129 | + steps: |
| 130 | + - name: Checkout repository |
| 131 | + uses: actions/checkout@v4 |
| 132 | + |
| 133 | + - name: Delete preview for closed/merged PR |
| 134 | + run: | |
| 135 | + PR_NUMBER=${{ github.event.pull_request.number }} |
| 136 | + BRANCH_NAME="gh-pages" |
| 137 | + PREVIEW_DIR="docs-preview-${PR_NUMBER}" |
| 138 | +
|
| 139 | + # Clone the gh-pages branch |
| 140 | + git clone --single-branch --branch $BRANCH_NAME https://github.com/${{ github.repository }} gh-pages |
| 141 | + cd gh-pages |
| 142 | +
|
| 143 | + # Check if the preview directory exists, and delete it if it does |
| 144 | + if [ -d "$PREVIEW_DIR" ]; then |
| 145 | + echo "Deleting preview directory $PREVIEW_DIR" |
| 146 | + rm -rf $PREVIEW_DIR |
| 147 | + else |
| 148 | + echo "Preview directory $PREVIEW_DIR does not exist. Skipping deletion." |
| 149 | + fi |
| 150 | +
|
| 151 | + # Configure git to use the GitHub Actions token for authentication |
| 152 | + git config --global user.name "GitHub Actions" |
| 153 | + git config --global user.email "actions@github.com" |
| 154 | +
|
| 155 | + # Set the GitHub token for authentication |
| 156 | + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} |
| 157 | +
|
| 158 | + # Add and commit the changes (only if there's something to delete) |
| 159 | + git add . |
| 160 | + git diff --quiet || git commit -m "Remove preview for PR #${PR_NUMBER}" |
| 161 | + git push origin $BRANCH_NAME |
| 162 | +
|
| 163 | + cleanup-comment: |
| 164 | + needs: cleanup |
| 165 | + runs-on: ubuntu-latest |
| 166 | + steps: |
| 167 | + - name: Checkout repository |
| 168 | + uses: actions/checkout@v4 |
| 169 | + |
| 170 | + - name: Double check preview directory deletion |
| 171 | + run: | |
| 172 | + # Check if the preview directory exists, and delete it if it does |
| 173 | + if [ -d "docs-preview-${{ github.event.pull_request.number }}" ]; then |
| 174 | + echo "Something went wrong, preview directory is not deleted." |
| 175 | + exit 1 |
| 176 | + else |
| 177 | + echo "Preview directory has been deleted successfully, proceeding." |
| 178 | + fi |
| 179 | +
|
| 180 | + - name: Post cleanup verification |
| 181 | + uses: peter-evans/create-or-update-comment@v4 |
| 182 | + with: |
| 183 | + issue-number: ${{ github.event.pull_request.number }} |
| 184 | + body: | |
| 185 | + ✅ Preview has been deleted successfully! |
0 commit comments