Skip to content

Merge pull request #868 from newfold-labs/dependabot/npm_and_yarn/dev… #1937

Merge pull request #868 from newfold-labs/dependabot/npm_and_yarn/dev…

Merge pull request #868 from newfold-labs/dependabot/npm_and_yarn/dev… #1937

name: Build Plugin
on:
push:
branches:
- 'main'
- 'develop'
pull_request:
types: [ opened, synchronize, reopened, ready_for_review ]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true
# Disable permissions for all available scopes by default.
# Any needed permissions should be configured at the job level.
permissions: {}
jobs:
build:
name: On Push
runs-on: ubuntu-latest
permissions:
contents: read
if: ${{ github.repository == 'newfold-labs/wp-plugin-bluehost' }}
steps:
- name: Checkout
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
with:
persist-credentials: false
- name: Validate version number
if: ${{ (github.repository == 'newfold-labs/wp-plugin-bluehost') }}
env:
PLUGIN_NAME: ${{ vars.PLUGIN_NAME }}
run: |
pluginHeaderVersion=$(grep "Version:" "$PLUGIN_NAME.php" | grep -Eo "[0-9\.]*")
pluginConstantVersion=$(grep "'BLUEHOST_PLUGIN_VERSION'" "$PLUGIN_NAME.php" | grep -Eo "[0-9\.]*")
pluginPackageVersion=$(grep '"version"' package.json | grep -Eo "[0-9\.]*")
echo "Plugin header version: $pluginHeaderVersion"
echo "Plugin constant version: $pluginConstantVersion"
echo "Plugin package version: $pluginPackageVersion"
[[ "$pluginPackageVersion" == "$pluginHeaderVersion" ]] || exit 1
[[ "$pluginPackageVersion" == "$pluginConstantVersion" ]] || exit 1
- name: Setup PHP
uses: shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1 # v2.36.0
with:
php-version: '8.1'
coverage: none
tools: composer, cs2pr
- name: Setup workflow context
id: workflow
working-directory: ${{ runner.temp }}
env:
REPO: ${{ github.repository }}
PLUGIN_NAME: ${{ vars.PLUGIN_NAME }}
run: |
mkdir dist
echo "DIST=${PWD}/dist" >> "$GITHUB_OUTPUT"
echo "PACKAGE=$PLUGIN_NAME" >> "$GITHUB_OUTPUT"
- name: Use Node.js 20.x
uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
with:
node-version: 20.x
cache: 'npm'
- name: Get Composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"
- name: Cache Composer vendor directory
uses: actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb # v5.0.1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
- name: Show versions
run: |
php --version
composer --version
node --version
npm --version
- name: Validate composer.json and composer.lock
if: ${{ github.repository == 'newfold-labs/wp-plugin-bluehost' }}
run: composer validate
- name: Install PHP Dependencies
run: composer install --no-progress --no-dev --optimize-autoloader --prefer-dist
- name: NPM Install
run: npm install --legacy-peer-deps
- name: Build JavaScript
run: npm run build
- name: Prepare files
env:
DIST: ${{ steps.workflow.outputs.DIST }}
run: rsync -r --include-from=.distinclude --exclude-from=.distignore . "$DIST"
- name: List Files
working-directory: ${{ steps.workflow.outputs.DIST }}
run: find .
- name: Upload artifact
id: artifact
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
with:
name: ${{ steps.workflow.outputs.PACKAGE }}
path: ${{ steps.workflow.outputs.DIST }}
include-hidden-files: true
- name: Comment on PR with download link
if: github.event_name == 'pull_request' && github.actor != 'dependabot[bot]'
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
continue-on-error: true
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;
const commentMeta = '<!-- plugin-artifact-download -->';
const artifactUrl = '${{ steps.artifact.outputs.artifact-url }}';
const commentBody = `
${commentMeta}
## Latest Plugin Build
⬇️ [_Download build artifact:_ \`${artifactUrl}\`](${artifactUrl})
🗂️ _Built from:_ \`${context.payload.pull_request.head.ref}\`
📦 _Commit:_ \`${context.payload.pull_request.head.sha.slice(0, 7)}\`
📅 _Date:_ \`${new Date().toISOString()}\`
🚀 Please download, test, and report any issues before merging. 🎉 This build includes the latest changes from this PR and was automatically generated by the GitHub Actions workflow. Any future commits will update this build.
⏳ _Note: Artifacts expire after ~90 days_
_A WordPress Playground preview link will be added shortly by a separate workflow._
`;
const { data: comments } = await github.rest.issues.listComments({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
});
const existingComment = comments.find(c => c.body.includes(commentMeta));
if (existingComment) {
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody,
});
} else {
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody,
});
}