Skip to content

Commit ec2f583

Browse files
author
bfintal@gmail.com
committed
added building per PR
1 parent 5ae075e commit ec2f583

4 files changed

Lines changed: 106 additions & 4 deletions

File tree

.github/workflows/plugin-build.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Plugin Build
2+
3+
on:
4+
push:
5+
branches: [ master, develop ]
6+
pull_request:
7+
branches: [ master, develop ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Check out
15+
uses: actions/checkout@v2
16+
17+
- name: Set the version suffix for the output
18+
run: echo VERSION_SUFFIX=${GITHUB_REF_NAME//\//-} >> $GITHUB_ENV
19+
20+
- name: Install dependencies and build
21+
run: |
22+
npm ci --legacy-peer-deps
23+
npm run build --suffix=${{ env.VERSION_SUFFIX }}
24+
25+
- name: Rename build zip for PR
26+
run: |
27+
# Find the zip file in dist directory
28+
ZIP_FILE=$(find dist -name "cimo-*.zip" -type f | head -1)
29+
if [ -n "$ZIP_FILE" ]; then
30+
mv "$ZIP_FILE" cimo-${{ env.VERSION_SUFFIX }}.zip
31+
echo "Renamed $ZIP_FILE to cimo-${{ env.VERSION_SUFFIX }}.zip"
32+
else
33+
echo "Build zip file not found in dist directory"
34+
echo "Contents of dist directory:"
35+
ls -la dist/
36+
exit 1
37+
fi
38+
39+
- name: Upload PR build zip artifact
40+
if: ${{ github.event_name == 'pull_request' }}
41+
uses: gavv/pull-request-artifacts@v1.0.0
42+
with:
43+
commit: ${{ github.event.pull_request.head.sha }}
44+
repo-token: ${{ secrets.GITHUB_TOKEN }}
45+
artifacts: cimo-${{ env.VERSION_SUFFIX }}.zip
46+
artifacts-branch: artifacts

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"scripts": {
1111
"prebuild": "npm run lint",
12-
"build": "node scripts/update-build-type.js free && npm run sync-version && wp-scripts build && npm run package",
12+
"build": "node scripts/update-build-type.js free && npm run sync-version ${npm_config_suffix} && wp-scripts build && npm run package ${npm_config_suffix}",
1313
"start": "node scripts/update-build-type.js free && wp-scripts start",
1414
"build:premium": "cd pro__premium_only && npm run build",
1515
"start:premium": "cd pro__premium_only && npm run start",
@@ -19,7 +19,7 @@
1919
"lint:css:fix": "wp-scripts lint-style --fix",
2020
"lint": "npm run lint:js && npm run lint:css",
2121
"format": "wp-scripts format",
22-
"package": "node scripts/package.js",
22+
"package": "node scripts/package.js ${npm_config_suffix}",
2323
"sync-version": "node scripts/sync-version.js"
2424
},
2525
"browserslist": [

scripts/package.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
/* eslint-disable no-console */
12
const fs = require( 'fs' )
23
const path = require( 'path' )
34
const archiver = require( 'archiver' )
45

6+
// Allow PR builds to add a version suffix.
7+
let folderSuffix = ''
8+
if ( process.argv.length === 3 ) {
9+
folderSuffix = process.argv[ process.argv.length - 1 ]
10+
}
11+
512
// Configuration
613
const PLUGIN_NAME = 'cimo'
714
const BUILD_DIR = 'build-plugin'
@@ -198,6 +205,34 @@ function addSecurityFiles( dir ) {
198205
}
199206
}
200207

208+
function updatePluginHeaderVersion( buildDir, suffix ) {
209+
if ( ! suffix ) {
210+
return
211+
}
212+
213+
const pluginFileName = 'cimo.php'
214+
const pluginFilePath = path.join( buildDir, pluginFileName )
215+
216+
if ( ! fs.existsSync( pluginFilePath ) ) {
217+
return
218+
}
219+
220+
let content = fs.readFileSync( pluginFilePath, 'utf8' )
221+
// Append folder suffix to version in plugin header
222+
content = content.replace(
223+
/^(\s*\*\s*Version:\s*)([^\r\n]+)/m,
224+
( match, prefix, version ) => {
225+
// Only append if suffix is not already present
226+
if ( ! version.includes( suffix ) ) {
227+
return prefix + version + '-' + suffix
228+
}
229+
return match
230+
}
231+
)
232+
fs.writeFileSync( pluginFilePath, content )
233+
console.log( `📝 Updated version in ${ pluginFileName } to include suffix: ${ suffix }` )
234+
}
235+
201236
// Main packaging function
202237
async function packagePlugin() {
203238
// eslint-disable-next-line no-console
@@ -240,6 +275,9 @@ async function packagePlugin() {
240275
console.log( '🔒 Adding security index.php files...' )
241276
addSecurityFiles( BUILD_DIR )
242277

278+
console.log( '📝 Updating plugin header version...' )
279+
updatePluginHeaderVersion( BUILD_DIR, folderSuffix )
280+
243281
// Create zip file
244282
// eslint-disable-next-line no-console
245283
console.log( '📦 Creating zip package...' )
@@ -269,7 +307,7 @@ async function packagePlugin() {
269307
} )
270308

271309
archive.pipe( output )
272-
archive.directory( BUILD_DIR, PLUGIN_NAME )
310+
archive.directory( BUILD_DIR, PLUGIN_NAME + ( folderSuffix ? `-${ folderSuffix }` : '' ) )
273311
await archive.finalize()
274312
}
275313

scripts/sync-version.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const fs = require( 'fs' )
22
const https = require( 'https' )
33

4+
// Allow PR builds to add a version suffix.
5+
let versionSuffix = ''
6+
if ( process.argv.length === 3 ) {
7+
versionSuffix = process.argv[ process.argv.length - 1 ]
8+
}
9+
410
// Function to fetch available WordPress versions
511
async function getAvailableWordPressVersions() {
612
return new Promise( resolve => {
@@ -82,7 +88,19 @@ async function syncVersions() {
8288
throw new Error( 'Could not find Version in cimo.php' )
8389
}
8490

85-
const pluginVersion = versionMatch[ 1 ].trim()
91+
const basePluginVersion = versionMatch[ 1 ].trim()
92+
const pluginVersion = versionSuffix ? `${ basePluginVersion }-${ versionSuffix }` : basePluginVersion
93+
94+
// Update interactions.php with version suffix if provided
95+
if ( versionSuffix ) {
96+
const updatedPluginContent = cimoPhp.replace(
97+
/^(\s*\*\s*Version:\s*)([^\r\n]+)/m,
98+
`$1${ pluginVersion }`
99+
)
100+
fs.writeFileSync( 'cimo.php', updatedPluginContent )
101+
// eslint-disable-next-line no-console
102+
console.log( `✅ ${ 'cimo.php' } version updated to ${ pluginVersion }` )
103+
}
86104

87105
// Read current package.json
88106
const packageJson = JSON.parse( fs.readFileSync( 'package.json', 'utf8' ) )

0 commit comments

Comments
 (0)