Skip to content

Enhance playground PR previews #11

Enhance playground PR previews

Enhance playground PR previews #11

# For now (!) the name here is important and should not be changed!
#
# The plugin-proxy.php does a check against exact this workflow name.
name: Playground Preview
# Inspired by, but not based on https://github.com/WordPress/gutenberg/blob/b89fb7b6eaf619bde0269e2a7fbf6245822f6cbf/.github/workflows/build-plugin-zip.yml#L153
on:
# What and Why pull_request_target?
# https://github.com/GatherPress/gatherpress/pull/666#issuecomment-2143088443
pull_request_target:
types: [opened, synchronize]
paths:
- '.github/workflows/playground-preview.yml'
- 'build/**'
- 'includes/**'
- 'languages/**'
- 'src/**'
- '*.php'
# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true
jobs:
check-changes:
name: Check for changes in /src/ directory
runs-on: ubuntu-latest
outputs:
src_changed: ${{ steps.src_changed.outputs.src_changed }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Check if /src/ directory has changed
id: src_changed
run: |
# Check if there are changes in the /src/ directory between the base and head refs
if git diff --name-only ${{ github.base_ref }} ${{ github.head_ref }} | grep -q '^src/'; then
echo "src_changed=true" >> $GITHUB_ENV
echo "::set-output name=src_changed::true"
else
echo "src_changed=false" >> $GITHUB_ENV
echo "::set-output name=src_changed::false"
fi
zip:
name: Build GatherPress plugin & upload as zipped artifact
runs-on: ubuntu-latest
needs: check-changes
if: needs.check-changes.outputs.src_changed == 'true' # Only run this job if there are changes in /src/
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: latest
coverage: none
tools: wp-cli
- name: Install latest version of dist-archive-command
run: wp package install wp-cli/dist-archive-command:dev-main
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: npm
- name: Install dependencies
run: npm ci
- name: Build plugin
# - [Incorrect version number used when creating zip archive · Issue #92 · wp-cli/dist-archive-command](https://github.com/wp-cli/dist-archive-command/issues/92)
run: |
npm run build
wp dist-archive . ./${{ github.event.repository.name }}.zip
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ github.event.repository.name }}-pr
path: ./${{ github.event.repository.name }}.zip
comment:
name: Comment with playground link
needs: [zip, check-changes] # Ensure this runs after the zip and check-changes jobs
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Add Preview Links comment
id: comment-on-pr
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const createPreviewLinks = require('.github/scripts/create-preview-links');
createPreviewLinks(github, context);