Skip to content

Commit

Permalink
Merge pull request #1033 from WordPress/update/plugin-bundling
Browse files Browse the repository at this point in the history
Update plugin(s) build setup for distribution
  • Loading branch information
mukeshpanchal27 committed Mar 19, 2024
2 parents 7ba61f4 + 0d5451d commit eaac49c
Show file tree
Hide file tree
Showing 11 changed files with 1,340 additions and 610 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/tests export-ignore
/plugin-tests export-ignore
/plugins export-ignore
/build-cs export-ignore

/*.DS_store export-ignore
/.DS_store? export-ignore
Expand All @@ -26,6 +27,7 @@ phpcs.xml.dist export-ignore
/phpcs.ruleset.xml export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/webpack.config.js export-ignore

/CONTRIBUTING.md export-ignore
/SECURITY.md export-ignore
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/deploy-dotorg.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
name: Deploy to WordPress.org

on:
Expand All @@ -7,17 +8,19 @@ on:

jobs:
release:
name: New Release
name: Release Performance Lab Plugin
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v4

- name: WordPress plugin deploy
uses: 10up/action-wordpress-plugin-deploy@stable
env:
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
SLUG: performance-lab

- name: Upload release assets
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
Expand Down
222 changes: 162 additions & 60 deletions .github/workflows/deploy-standalone-plugins.yml
Original file line number Diff line number Diff line change
@@ -1,99 +1,201 @@
name: Deploy standalone plugins to WordPress.org
---
name: Deploy Standalone Plugin(s) to WordPress.org

on:
release:
types: [published]
types:
- published
workflow_dispatch:
inputs:
slug:
plugin:
type: string
description: 'The slug of the plugin to deploy'
required: true
dry-run:
type: boolean
description: 'Debug mode (run without publishing).'
default: false

permissions: {}

jobs:
release:
name: Prepare Deployment
pre-run:
name: Pre-run
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
matrix: ${{ steps.set-matrix.outputs.plugins }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Node.js (.nvmrc)
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: npm
- name: Install npm dependencies
run: npm ci
- name: Get directory
id: get-plugin-directory
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
echo "directory=$(node ./bin/plugin/cli.js get-plugin-dir --slug=${{ inputs.slug }})" >> $GITHUB_OUTPUT
- name: Get plugin version
id: get-version
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
echo "version=$(node ./bin/plugin/cli.js get-plugin-version --slug=${{ inputs.slug }})" >> $GITHUB_OUTPUT
- name: Checkout
uses: actions/checkout@v4

- name: Set matrix
id: set-matrix
run: |
PLUGINS=$(jq -r '.plugins' plugins.json)
if ${{ github.event_name == 'workflow_dispatch' }}; then
result=$(echo "${{ steps.get-version.outputs.version }}" | awk '/^(\*|[0-9]+(\.[0-9]+){0,2}(-[a-zA-Z0-9.]+)?)$/ {print "Matched"}')
if [[ -n "$result" ]]; then
# Set the manual input values in JSON format for use in the matrix.
echo "matrix={\"include\":[{\"slug\":\"${{ inputs.slug }}\",\"version\":\"${{ steps.get-version.outputs.version }}\",\"directory\":\"${{ steps.get-plugin-directory.outputs.directory }}\",\"dry-run\":\"${{ inputs.dry-run }}\"}]}" >> $GITHUB_OUTPUT
SLUG=${{ inputs.plugin }}
if echo $PLUGINS | jq -e '.[] | select(. == "'$SLUG'")' > /dev/null; then
PLUGINS="[ \"$SLUG\" ]"
else
echo "The ${{ inputs.slug }} module slug is missing in the file plugins.json."
echo "::error::The plugin $SLUG is not in the list of plugins to deploy."
exit 1
fi
else
# Load the JSON file and parse from "{name: {slug, version}, ...}" to "include: [{ name, slug, version }, ...]"
# for use in the matrix.
# For plugins, the "version" parameter is not included here; it will dynamically get it in its own job.
# The "dry-run" parameter is included here to set the deployment mode.
# When running the manual (workflow_dispatch) workflow, this value will be set from manual input type.
echo "matrix=$(jq -c '{include: ([.modules | to_entries[] | {name:.key, slug: .value.slug, version: .value.version, directory: "build", "dry-run": false}] + ([.plugins[] | {name:. , slug:. , directory: "plugins", "dry-run": false}]))}' plugins.json)" >> $GITHUB_OUTPUT
fi
# Set the matrix.
echo "::notice::Deploying the following plugins: $(echo ${PLUGINS[@]})"
echo "plugins=$(echo $PLUGINS | jq -c .)" >> $GITHUB_OUTPUT
deploy:
name: Deploy Plugin
needs: release
name: "Deploy Plugin: ${{ matrix.plugin }}"
needs: pre-run
runs-on: ubuntu-latest
permissions:
actions: write
deployments: write
strategy:
matrix: ${{ fromJSON(needs.release.outputs.matrix) }}
matrix:
plugin: ${{ fromJSON(needs.pre-run.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js (.nvmrc)
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: npm

- name: Install npm dependencies
run: npm ci
- name: Building standalone plugins
if: ${{ matrix.directory != 'plugins' }}
run: npm run build-plugins
- name: Set version
id: set_version

- name: Build plugin
run: npm run build:plugin:${{ matrix.plugin }}

- name: Set plugin version
working-directory: ./build
run: |
echo "PLUGIN_VERSION=$(jq -r '."${{ matrix.plugin }}"' manifest.json)" >> $GITHUB_ENV
- name: Check if deployment is needed
id: check-deployment
run: |
if [ -z "${{ matrix.version }}" ]; then
echo "version=$(node ./bin/plugin/cli.js get-plugin-version --slug=${{ matrix.slug }})" >> $GITHUB_OUTPUT
else
echo "version=${{ matrix.version }})" >> $GITHUB_OUTPUT
PLUGIN_VERSION_WPORG=$(curl -sSL https://api.wordpress.org/plugins/info/1.0/${{ matrix.plugin }}.json --retry 3 --retry-delay 5 --retry-all-errors --fail | jq -r '.version')
echo "::debug::The ${{ matrix.plugin }} plugin is currently at version $PLUGIN_VERSION_WPORG on WordPress.org."
# Bail if the plugin version is not found.
if [ -z "$PLUGIN_VERSION_WPORG" ]; then
echo "::error::Could not retrieve ${{ matrix.plugin }} information from WordPress.org. The plugin may not exist or the API may be down."
exit 1
fi
- name: Deploy Standalone Plugin - ${{ matrix.slug }}
# Bail if the plugin is already up to date.
if [ "$PLUGIN_VERSION_WPORG" = "$PLUGIN_VERSION" ]; then
echo "::notice::The ${{ matrix.plugin }} plugin is already up to date on WordPress.org. Halting deployment."
exit 0
fi
echo "deploy=true" >> $GITHUB_OUTPUT
- name: Create zip file
if: steps.check-deployment.outputs.deploy == 'true'
run: |
mkdir -p ./build/dist
cd ./build/${{ matrix.plugin }}
zip -r ../dist/${{ matrix.plugin }}.zip .
- name: Generate checksum
if: steps.check-deployment.outputs.deploy == 'true'
working-directory: ./build/dist
run: |
mkdir -p $RUNNER_TEMP/plugin-checksums
find . -type f -print0 | sort -z | xargs -r0 shasum -a 256 -b | sed 's# \*\./# *#' > $RUNNER_TEMP/plugin-checksums/checksums.txt
shasum -a 256 -U -c $RUNNER_TEMP/plugin-checksums/checksums.txt
cat $RUNNER_TEMP/plugin-checksums/checksums.txt | while read sum file; do echo "$sum $file" > ${file#\*}.sha256; done
- name: Upload artifact
if: steps.check-deployment.outputs.deploy == 'true'
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.plugin }}
path: ./build/dist

- name: Start deployment
if: steps.check-deployment.outputs.deploy == 'true'
uses: bobheadxi/deployments@v1
id: wporg-deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: "wp.org plugin: ${{ matrix.plugin }}"

- name: Deploy Plugin - ${{ matrix.plugin }}
if: steps.check-deployment.outputs.deploy == 'true'
uses: 10up/action-wordpress-plugin-deploy@stable
with:
dry-run: ${{ matrix.dry-run }}
dry-run: ${{ github.event_name == 'workflow_dispatch' && inputs.dry-run || false }}
env:
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
SLUG: ${{ matrix.plugin }}
VERSION: ${{ env.PLUGIN_VERSION }}
SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
SLUG: ${{ matrix.slug }}
VERSION: ${{ steps.set_version.outputs.version }}
BUILD_DIR: ./${{ matrix.directory }}/${{ matrix.slug }}
ASSETS_DIR: ./${{ matrix.directory }}/${{ matrix.slug }}/.wordpress-org
SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
BUILD_DIR: ./build/${{ matrix.plugin }}
ASSETS_DIR: ./plugins/${{ matrix.plugin }}/.wordpress-org

- name: Finish deployment
if: ${{ steps.wporg-deployment.outputs.deployment_id && always() }}
uses: bobheadxi/deployments@v1
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.wporg-deployment.outputs.deployment_id }}
env: "wp.org plugin: ${{ matrix.plugin }}"
env_url: "https://wordpress.org/plugins/${{ matrix.plugin }}/"

release-assets:
name: Add release assets
needs: [ pre-run, deploy ]
runs-on: ubuntu-latest
permissions:
actions: read
contents: write
if: github.event_name == 'release'
strategy:
matrix:
plugin: ${{ fromJSON(needs.pre-run.outputs.matrix) }}
steps:
- name: Check artifact existence
id: artifact-existence
uses: actions/github-script@v7
with:
script: |
const getArtifact = await github.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts{?name}', {
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
name: "${{ matrix.plugin }}"
});
if (getArtifact.status !== 200) {
throw new Error(`Invalid response from GitHub API: ${getArtifact.status}`);
}
core.setOutput('exists', getArtifact.data.artifacts.length > 0);
core.debug(`Artifact for ${{ matrix.plugin }} exists: ${core.getInput('exists')} ? "true" : "false"`);
- name: Download artifact
if: steps.artifact-existence.outputs.exists == 'true'
uses: actions/download-artifact@v4
with:
name: ${{ matrix.plugin }}
path: ./build/dist

- name: Upload release assets
if: steps.artifact-existence.outputs.exists == 'true'
uses: softprops/action-gh-release@v1
with:
files: |
./build/dist/${{ matrix.plugin }}.zip
./build/dist/${{ matrix.plugin }}.zip.sha256
33 changes: 0 additions & 33 deletions bin/plugin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,10 @@ const {
handler: translationsHandler,
options: translationsOptions,
} = require( './commands/translations' );
const {
handler: buildPluginsHandler,
options: buildPluginsOptions,
} = require( './commands/build-plugins' );
const {
handler: testPluginsHandler,
options: testPluginsOptions,
} = require( './commands/test-plugins' );
const {
handler: getPluginVersionHandler,
options: getPluginVersionOptions,
} = require( './commands/get-plugin-version' );
const {
handler: getPluginDirHandler,
options: getPluginDirOptions,
} = require( './commands/get-plugin-dir' );
const {
handler: enabledModulesHandler,
options: enabledModulesOptions,
Expand Down Expand Up @@ -85,32 +73,11 @@ withOptions( program.command( 'module-translations' ), translationsOptions )
)
.action( catchException( translationsHandler ) );

withOptions(
program.command( 'build-standalone-plugins' ),
buildPluginsOptions
)
.alias( 'build-plugins' )
.description( 'Build standalone plugins' )
.action( catchException( buildPluginsHandler ) );

withOptions( program.command( 'test-standalone-plugins' ), testPluginsOptions )
.alias( 'test-plugins' )
.description( 'Test standalone plugins' )
.action( catchException( testPluginsHandler ) );

withOptions(
program.command( 'get-standalone-plugin-version' ),
getPluginVersionOptions
)
.alias( 'get-plugin-version' )
.description( 'Get standalone plugin version' )
.action( catchException( getPluginVersionHandler ) );

withOptions( program.command( 'get-plugin-dir' ), getPluginDirOptions )
.alias( 'get-plugin-directory' )
.description( 'Get plugin directory' )
.action( catchException( getPluginDirHandler ) );

withOptions(
program.command( 'default-enabled-modules' ),
enabledModulesOptions
Expand Down

0 comments on commit eaac49c

Please sign in to comment.