Skip to content

Add Carsten as lead. #27

Add Carsten as lead.

Add Carsten as lead. #27

# 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:
- 'build/**'
- 'includes/**'
- '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:
needs-build:
name: Check if the pull request was just opened or if PR has changes in /src/ directory
runs-on: ubuntu-latest
outputs:
run_build: ${{ steps.set-run-build.outputs.run_build }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Check if zip artifact exists
id: check-artifact
run: |
# Define the expected artifact name based on the repository name and PR
ARTIFACT_NAME="${{ github.event.repository.name }}-pr"
# Use GitHub CLI to check if the artifact exists
ARTIFACT_EXISTS=$(gh api repos/${{ github.repository }}/actions/artifacts --jq ".artifacts[] | select(.name == \"$ARTIFACT_NAME\") | .id" || echo "")
if [ -n "$ARTIFACT_EXISTS" ]; then
# If the artifact exists, set artifact_exists to true
echo "artifact_exists=true" >> $GITHUB_ENV
else
# If the artifact does not exist, set artifact_exists to false
echo "artifact_exists=false" >> $GITHUB_ENV
fi
- name: Set run_build based on artifact existence or /src/ changes
id: set-run-build
run: |
# Check if the artifact does not exist
if [ "${{ steps.check-artifact.outputs.artifact_exists }}" == "false" ]; then
echo "run_build=true" >> $GITHUB_ENV
# Check if there are changes in the /src/ directory using commit SHAs
elif git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -q '^src/'; then
echo "run_build=true" >> $GITHUB_ENV
else
echo "run_build=false" >> $GITHUB_ENV
fi
zip:
name: Build GatherPress plugin & upload as zipped artifact
runs-on: ubuntu-latest
needs: needs-build
if: needs.needs-build.outputs.run_build == 'true' # Only run this job if no built zip-artifact exists or 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, needs-build] # Ensure this runs after the needs-build & zip jobs.
if: always() # Make sure this job runs even if dependent jobs were skipped.
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);