Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional tools for plugin archives: php-scoper, rector #134

Merged
merged 50 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
94be548
feat: declare workflow outputs from job outputs
Biont Feb 28, 2024
e100109
Update to actions/upload-artifact@v4
Biont Feb 28, 2024
5a6722b
Split into three jobs.
Biont Mar 19, 2024
fe7f453
Set up job dependencies
Biont Mar 19, 2024
852ecfd
Fix upload directory
Biont Mar 19, 2024
a6ed282
Test for build tool configs
Biont Mar 19, 2024
77b17d8
Improve artifact naming
Biont Mar 19, 2024
134d063
Allow composer plugins prior to installation
Biont Mar 19, 2024
9a55296
Allow composer plugins prior to installation
Biont Mar 19, 2024
b69e951
Allow composer plugins prior to installation
Biont Mar 19, 2024
d16d060
Set up php for wp-cli
Biont Mar 19, 2024
102504a
remove some clutter from deps artifact already
Biont Mar 19, 2024
1078279
improve artifact naming
Biont Mar 19, 2024
5c8b9fa
Move required data for final archive step
Biont Mar 19, 2024
520353b
Add missing path config for final artifact upload
Biont Mar 19, 2024
c615353
Move env vars where they belong
Biont Mar 19, 2024
741d772
Merge branch 'main' into feature/multi-step-plugin-archive
Biont Mar 19, 2024
607a5e2
Move PLUGIN_FOLDER_NAME as well
Biont Mar 20, 2024
9206bd8
Introduce php-scoper
Biont Mar 20, 2024
7ecaeba
Introduce php-scoper via shivammathur/setup-php
Biont Mar 20, 2024
2451d0e
Update .github/workflows/build-plugin-archive.yml
Biont Mar 21, 2024
8839d57
Update .github/workflows/build-plugin-archive.yml
Biont Mar 21, 2024
251fea6
Update .github/workflows/build-plugin-archive.yml
Biont Mar 21, 2024
058508e
Introduce PHP_VERSION_BUILD
Biont Mar 21, 2024
c59e12e
Remove somewhat redundant check for package.json files
Biont Mar 21, 2024
1b99b6e
Merge branch 'feature/multi-step-plugin-archive' into feature/prefixe…
Biont Mar 21, 2024
5019427
Merge upstream changes and fix required php version
Biont Mar 21, 2024
946f4e4
temporarily require main branch of php-scoper
Biont Apr 30, 2024
81bc444
Force '__composer_autoload_files' to be unique
Biont Jun 18, 2024
a78738a
Use github.sha for now
Biont Jun 18, 2024
b20f5ad
One more try
Biont Jun 18, 2024
d19fcbb
Merge branch 'refs/heads/main' into feature/prefixed-archives
Biont Jun 20, 2024
4ca3067
Fix usage of artifact name
Biont Jun 24, 2024
df4b49b
Debug a little
Biont Jun 24, 2024
717326e
Remove working_dir configs since actions/download-artifact@v4 apparen…
Biont Jun 24, 2024
67b8ac0
Fix job output
Biont Jun 24, 2024
9ee5dc5
Declare job output of final archive creation
Biont Jul 3, 2024
1791b79
Fix variable name
Biont Jul 3, 2024
2975185
Add Rector in archive creation (#135)
AlexP11223 Sep 3, 2024
5c9ca82
Update .github/workflows/build-plugin-archive.yml
Biont Sep 3, 2024
47b51bc
Update .github/workflows/build-plugin-archive.yml
Biont Sep 3, 2024
240dbc2
Update .github/workflows/build-plugin-archive.yml
Biont Sep 3, 2024
3a9d6a6
Merge branch 'main' into feature/prefixed-archives
Biont Sep 3, 2024
bbddb40
Include hidden files in upload
AlexP11223 Sep 3, 2024
4b7a858
Merge branch 'main' into feature/prefixed-archives
Biont Sep 4, 2024
f3ba405
Update .github/workflows/build-plugin-archive.yml
Biont Sep 4, 2024
a9a9db5
Update .github/workflows/build-plugin-archive.yml
Biont Sep 4, 2024
adb611a
Update .github/workflows/build-plugin-archive.yml
Biont Sep 4, 2024
3765dc4
Update .github/workflows/build-plugin-archive.yml
Biont Sep 4, 2024
095ab16
Update .github/workflows/build-plugin-archive.yml
Biont Sep 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 136 additions & 53 deletions .github/workflows/build-plugin-archive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ on:
required: false
type: string
COMPOSER_ARGS:
description: Set of arguments passed to Composer.
default: '--no-dev --no-scripts --prefer-dist --optimize-autoloader'
description: Set of arguments passed to Composer when gathering production dependencies.
default: '--no-dev --prefer-dist --optimize-autoloader'
required: false
type: string
PHP_VERSION:
description: PHP version to use during packaging.
description: PHP version to use when gathering production dependencies.
default: "8.0"
required: false
type: string
PHP_VERSION_BUILD:
description: PHP version to use when executing build tools.
default: "8.2"
tyrann0us marked this conversation as resolved.
Show resolved Hide resolved
required: false
type: string
ARCHIVE_NAME:
description: The name of the zip archive (falls back to the repository name).
default: ''
Expand Down Expand Up @@ -78,18 +83,12 @@ on:
value: ${{ jobs.create-plugin-archive.outputs.artifact }}

jobs:
create-plugin-archive:
checkout-dependencies:
name: Install production dependencies
timeout-minutes: 5
runs-on: ubuntu-latest
env:
NODE_OPTIONS: ${{ inputs.NODE_OPTIONS }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }}
NODE_CACHE_MODE: ''
ARCHIVE_NAME: ${{ inputs.ARCHIVE_NAME }}
PLUGIN_FOLDER_NAME: ${{ inputs.PLUGIN_FOLDER_NAME }}
ENV_VARS: ${{ secrets.ENV_VARS }}
GIT_SHA: ${{ github.sha }}
PLUGIN_VERSION: ${{ inputs.PLUGIN_VERSION }}
COMPOSER_AUTH: '${{ secrets.COMPOSER_AUTH_JSON }}'
# Disables symlinking of local path repositories.
# During development, symlinking is preferable.
Expand All @@ -101,14 +100,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Set up archive name
id: plugin-data
run: echo "archive-name=${ARCHIVE_NAME:-${{ github.event.repository.name }}}" >> $GITHUB_OUTPUT

- name: Set up plugin folder name
id: plugin-folder-name
run: echo "plugin-folder-name=${PLUGIN_FOLDER_NAME:-${ARCHIVE_NAME:-${{ github.event.repository.name }}}}" >> $GITHUB_OUTPUT

- name: Set up custom environment variables
if: ${{ env.ENV_VARS }}
uses: actions/github-script@v7
Expand All @@ -118,82 +109,174 @@ jobs:
.parse(process.env.ENV_VARS)
.forEach(envVar => core.exportVariable(envVar.name, envVar.value));

- name: Set up node cache mode
run: |
if [ "${{ inputs.PACKAGE_MANAGER }}" == 'npm' ] && { [ -f "${GITHUB_WORKSPACE}/package-lock.json" ] || [ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ]; }; then
echo "NODE_CACHE_MODE=npm" >> $GITHUB_ENV
elif [ "${{ inputs.PACKAGE_MANAGER }}" == 'yarn' ] && [ -f "${GITHUB_WORKSPACE}/yarn.lock" ]; then
echo "NODE_CACHE_MODE=yarn" >> $GITHUB_ENV
else
echo "No lock files found or unknown package manager"
fi

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.PHP_VERSION }}
tools: wp-cli

- name: Install Composer dependencies
# ⬑ The idea is to ensure we have non-production tools like Composer Asset Compiler and WordPress Translation Downloader available
- name: Install Composer dependencies without dev dependencies
uses: ramsey/composer-install@v3
with:
composer-options: --prefer-dist
composer-options: ${{ inputs.COMPOSER_ARGS }}

- name: Set artifact name
id: set-artifact-name
run: echo "artifact=interim-deps" >> $GITHUB_OUTPUT

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.set-artifact-name.outputs.artifact }}
path: |
./*
!./.git
!./.ddev
!./.github
run-build-tools:
Biont marked this conversation as resolved.
Show resolved Hide resolved
name: Process build steps
timeout-minutes: 5
runs-on: ubuntu-latest
needs: checkout-dependencies
env:
NODE_OPTIONS: ${{ inputs.NODE_OPTIONS }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }}
NODE_CACHE_MODE: ''
outputs:
artifact: ${{ steps.set-artifact-name.outputs.artifact }}
steps:
- name: Download Artifact
uses: actions/download-artifact@v4
with:
name: ${{ needs.checkout-dependencies.outputs.artifact }}
- name: Setup PHP with tools
Biont marked this conversation as resolved.
Show resolved Hide resolved
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.PHP_VERSION_BUILD }}
tools: humbug/php-scoper:dev-main
Biont marked this conversation as resolved.
Show resolved Hide resolved
- name: Check optional Composer build tools
id: composer-tools
run: |
hasDep(){
composer show -i -N -D --strict "${1}" >/dev/null 2>&1
hasAssetConfig(){
test -f assets-compiler.json
local EXIT=$?
if [ ! $EXIT ]; then
echo "$EXIT"
exit 0
fi
jq '.extra | has("composer-asset-compiler")' < composer.json >/dev/null 2>&1
local EXIT=$?
echo "$EXIT"
}
hasTranslateConfig(){
jq '.extra | has("wp-translation-downloader")' < composer.json >/dev/null 2>&1
local EXIT=$?
echo "$EXIT"
}
hasScoperConfig(){
test -f scoper.inc.php
local EXIT=$?
echo "$EXIT"
}
echo "assets-compiler=$( hasDep inpsyde/composer-assets-compiler )" >> $GITHUB_OUTPUT
echo "translation-downloader=$( hasDep inpsyde/wp-translation-downloader )" >> $GITHUB_OUTPUT
echo "assets-compiler=$( hasAssetConfig )" >> $GITHUB_OUTPUT
echo "translation-downloader=$( hasTranslateConfig )" >> $GITHUB_OUTPUT
echo "php-scoper=$( hasScoperConfig )" >> $GITHUB_OUTPUT

- name: Check for package.json
id: check-for-package
- name: Set up node cache mode
run: |
if [ -f package.json ]; then
echo "has_package=true" >> $GITHUB_OUTPUT
if [ "${{ inputs.PACKAGE_MANAGER }}" == 'npm' ] && { [ -f "${GITHUB_WORKSPACE}/package-lock.json" ] || [ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ]; }; then
echo "NODE_CACHE_MODE=npm" >> $GITHUB_ENV
elif [ "${{ inputs.PACKAGE_MANAGER }}" == 'yarn' ] && [ -f "${GITHUB_WORKSPACE}/yarn.lock" ]; then
echo "NODE_CACHE_MODE=yarn" >> $GITHUB_ENV
else
echo "has_package=false" >> $GITHUB_OUTPUT
echo "No lock files found or unknown package manager"
fi

- name: Set up node
if: steps.check-for-package.outputs.has_package == 'true'
if: steps.composer-tools.outputs.assets-compiler == '0'
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.NODE_VERSION }}
registry-url: ${{ inputs.NPM_REGISTRY_DOMAIN }}
cache: ${{ env.NODE_CACHE_MODE }}

- name: Run Composer Asset Compiler
if: steps.composer-tools.outputs.assets-compiler == '0' && steps.check-for-package.outputs.has_package == 'true'
run: composer compile-assets ${{ inputs.COMPILE_ASSETS_ARGS }}
- name: Install and run Composer Asset Compiler
if: steps.composer-tools.outputs.assets-compiler == '0'
run: |
composer global config --no-plugins --no-interaction allow-plugins.inpsyde/composer-assets-compiler true
composer global require inpsyde/composer-assets-compiler
composer compile-assets ${{ inputs.COMPILE_ASSETS_ARGS }}

- name: Run WordPress Translation Downloader
- name: Install and run WordPress Translation Downloader
if: steps.composer-tools.outputs.translation-downloader == '0'
run: composer wp-translation-downloader:download
run: |
composer global config --no-plugins --no-interaction allow-plugins.composer/installers true
composer global config --no-plugins --no-interaction allow-plugins.inpsyde/wp-translation-downloader true
composer global require inpsyde/wp-translation-downloader
composer wp-translation-downloader:download

- name: Install Composer dependencies without dev dependencies
# ⬑ Now we want to get rid of dev dependencies and most probably skip composer scripts
uses: ramsey/composer-install@v3
- name: Run PHP Scoper
Biont marked this conversation as resolved.
Show resolved Hide resolved
if: steps.composer-tools.outputs.php-scoper == '0'
run: |
php-scoper add-prefix --force --output-dir build
Biont marked this conversation as resolved.
Show resolved Hide resolved
composer --working-dir=build dump-autoload -o
sed -i "s/'__composer_autoload_files'/\'__composer_autoload_files_${{ github.sha }}'/g" "build/vendor/composer/autoload_real.php"
tyrann0us marked this conversation as resolved.
Show resolved Hide resolved
- name: Move unchanged code to build directory
Biont marked this conversation as resolved.
Show resolved Hide resolved
if: steps.composer-tools.outputs.php-scoper != '0'
run: mv * build 2>/dev/null
tyrann0us marked this conversation as resolved.
Show resolved Hide resolved

- name: Set artifact name
id: set-artifact-name
run: echo "artifact=interim-built" >> $GITHUB_OUTPUT
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
composer-options: ${{ inputs.COMPOSER_ARGS }}
name: ${{ steps.set-artifact-name.outputs.artifact }}
path: |
build/
!build/**/node_modules
create-plugin-archive:
Biont marked this conversation as resolved.
Show resolved Hide resolved
name: Create build archive
timeout-minutes: 5
runs-on: ubuntu-latest
needs: run-build-tools
env:
ARCHIVE_NAME: ${{ inputs.ARCHIVE_NAME }}
PLUGIN_FOLDER_NAME: ${{ inputs.PLUGIN_FOLDER_NAME }}
GIT_SHA: ${{ github.sha }}
PLUGIN_VERSION: ${{ inputs.PLUGIN_VERSION }}
steps:
- name: Download Artifact
uses: actions/download-artifact@v4
with:
name: ${{ needs.run-build-tools.outputs.artifact }}

- name: Set up plugin folder name
id: plugin-folder-name
run: echo "plugin-folder-name=${PLUGIN_FOLDER_NAME:-${ARCHIVE_NAME:-${{ github.event.repository.name }}}}" >> $GITHUB_OUTPUT

- name: Set up archive name
id: plugin-data
run: echo "archive-name=${ARCHIVE_NAME:-${{ github.event.repository.name }}}" >> $GITHUB_OUTPUT

- name: Add commit hash to plugin header
run: 'sed -Ei "s/SHA: .*/SHA: ${GIT_SHA}/g" ${{ inputs.PLUGIN_MAIN_FILE }}'

- name: Set plugin version header
run: 'sed -Ei "s/Version: .*/Version: ${PLUGIN_VERSION}/g" ${{ inputs.PLUGIN_MAIN_FILE }}'

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.PHP_VERSION_BUILD }}
tools: wp-cli

- name: Install dist-archive command
run: wp package install wp-cli/dist-archive-command

- name: Execute custom code before archive creation
run: |
${{ inputs.PRE_SCRIPT }}
${{ inputs.PRE_SCRIPT }}

- name: Run WP-CLI command
run: wp dist-archive . ./archive.zip --plugin-dirname=${{ steps.plugin-folder-name.outputs.plugin-folder-name }}
Expand All @@ -210,4 +293,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: ${{ steps.set-artifact-name.outputs.artifact }}
path: dist/*
path: ./dist/*
3 changes: 2 additions & 1 deletion docs/archive-creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ jobs:
| `NPM_REGISTRY_DOMAIN` | `"https://npm.pkg.github.com/"` | Domain of the private npm registry |
| `PACKAGE_MANAGER` | `"yarn"` | Package manager with which the dependencies should be installed (`npm` or `yarn`) |
| `COMPOSER_ARGS` | `'--no-dev --no-scripts --prefer-dist --optimize-autoloader'` | Set of arguments passed to Composer |
| `PHP_VERSION` | `"8.0"` | PHP version to use during packaging |
| `PHP_VERSION` | `"8.0"` | PHP version to use when gathering production dependencies |
| `PHP_VERSION_BUILD` | `"8.0"` | PHP version to use when executing build tools |
| `ARCHIVE_NAME` | `""` | The name of the zip archive (falls back to the repository name) |
| `PLUGIN_MAIN_FILE` | `"index.php"` | The name of the main plugin file |
| `PLUGIN_FOLDER_NAME` | `""` | The name of the plugin folder (falls back to the archive name, if set, or the repository name) |
Expand Down