|
| 1 | +name: Preview |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - 'main' |
| 6 | + pull_request_target: |
| 7 | + types: |
| 8 | + - opened |
| 9 | + - synchronize |
| 10 | + - reopened |
| 11 | + - closed |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: preview-${{ github.event_name }}-${{ github.event.number || github.ref_name }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + preview: |
| 19 | + name: Preview |
| 20 | + runs-on: ubuntu-22.04 |
| 21 | + env: |
| 22 | + PREVIEW_REPO: PoliNetworkOrg/preview |
| 23 | + PAGES_BASE: https://PoliNetworkOrg.github.io/preview |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout branch |
| 27 | + if: ${{ !startsWith(github.event_name, 'pull_request') }} |
| 28 | + uses: actions/checkout@v4 |
| 29 | + |
| 30 | + - name: Checkout PR head |
| 31 | + if: ${{ startsWith(github.event_name, 'pull_request') }} |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + ref: refs/pull/${{ github.event.pull_request.number }}/merge |
| 35 | + |
| 36 | + # Assuming you're using Node.js |
| 37 | + - name: Setup Node.js |
| 38 | + uses: actions/setup-node@v4 |
| 39 | + if: ${{ github.event.action != 'closed' }} # Skipping these steps if the PR has been closed |
| 40 | + with: |
| 41 | + node-version-file: "package.json" |
| 42 | + |
| 43 | + - uses: pnpm/action-setup@v4 |
| 44 | + with: |
| 45 | + version: 10 |
| 46 | + run_install: false |
| 47 | + |
| 48 | + - name: Get pnpm store directory |
| 49 | + id: pnpm-cache |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT |
| 53 | +
|
| 54 | + - uses: actions/cache@v4 |
| 55 | + name: Setup pnpm cache |
| 56 | + with: |
| 57 | + path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} |
| 58 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 59 | + restore-keys: | |
| 60 | + ${{ runner.os }}-pnpm-store-V |
| 61 | +
|
| 62 | + - name: Install dependencies |
| 63 | + run: pnpm install |
| 64 | + |
| 65 | + # This will calculate the base URL for the website, based on the event that triggered the workflow. |
| 66 | + # Leave this step as it is, unless you know what you're doing. |
| 67 | + - name: Determine base URL |
| 68 | + if: ${{ github.event.action != 'closed' }} |
| 69 | + id: baseurl |
| 70 | + shell: bash |
| 71 | + run: | |
| 72 | + if [ "${{ github.event_name }}" == "pull_request_target" ]; then |
| 73 | + full="${{ env.PAGES_BASE }}/${{ github.repository }}/pr/${{ github.event.number }}" |
| 74 | + else |
| 75 | + full="${{ env.PAGES_BASE }}/${{ github.repository }}/branch/${{ github.ref_name }}" |
| 76 | + fi |
| 77 | +
|
| 78 | + relative=/$(echo $full | cut -d/ -f4-) |
| 79 | +
|
| 80 | + echo "full=$full" >> $GITHUB_OUTPUT |
| 81 | + echo "relative=$relative" >> $GITHUB_OUTPUT |
| 82 | +
|
| 83 | + # Run your usual build command, but make sure to use the correct base URL |
| 84 | + # This example assumes you're using React, and that you're using the PUBLIC_URL env variable |
| 85 | + - name: Build |
| 86 | + if: ${{ github.event.action != 'closed' }} |
| 87 | + run: pnpm build |
| 88 | + env: |
| 89 | + PUBLIC_URL: ${{ steps.baseurl.outputs.relative }} |
| 90 | + |
| 91 | + # This will trigger the action. Make sure to change the build_dir input to the correct directory |
| 92 | + - uses: EndBug/pages-preview@v1 |
| 93 | + with: |
| 94 | + build_dir: build # Change this! |
| 95 | + preview_base_url: ${{ env.PAGES_BASE }} |
| 96 | + preview_repo: ${{ env.PREVIEW_REPO }} |
| 97 | + preview_token: ${{ secrets.PREVIEW_TOKEN }} |
0 commit comments