Skip to content

Commit e4cd280

Browse files
feat: add new build scripts and actions (#61)
Co-authored-by: Sebastian Thulin <[email protected]>
1 parent b3224fd commit e4cd280

File tree

2 files changed

+100
-137
lines changed

2 files changed

+100
-137
lines changed

.github/workflows/release.yml

Lines changed: 62 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,88 @@
1-
name: Bump version and create release
1+
name: Bump version, build and create release
22

33
on:
44
push:
5-
branches: [main]
5+
branches:
6+
- main
67

78
jobs:
89
release:
910
runs-on: ubuntu-latest
10-
11-
steps:
12-
13-
- name: Checkout
14-
uses: actions/checkout@v2
15-
with:
16-
fetch-depth: 0
17-
18-
- name: Create Release and bump version files
19-
uses: helsingborg-stad/[email protected]
20-
with:
21-
php-version: 8.2
22-
node-version: 20.6.0
23-
build-assets:
24-
needs: ['release']
25-
runs-on: ubuntu-latest
2611
permissions:
27-
contents: read
12+
contents: write
2813
packages: write
14+
2915
steps:
30-
- name: Get latest release version
31-
id: get-version
32-
run: |
33-
echo "LATEST_RELEASE=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r '.tag_name')" >> $GITHUB_ENV
3416
- name: Checkout
3517
uses: actions/checkout@v4
3618
with:
3719
fetch-depth: 0
38-
- name: Update version in package.json
39-
run: |
40-
# Read the latest release version from environment variable
41-
LATEST_VERSION="${{ env.LATEST_RELEASE }}"
4220

43-
# Update the "version" field in package.json using jq
44-
jq --arg version "$LATEST_VERSION" '.version = $version' package.json > temp.json && mv temp.json package.json
45-
- name: Setup node
46-
uses: actions/setup-node@v3
21+
- name: Bump version files (no release yet)
22+
uses: helsingborg-stad/release-wp-plugin-action@main
4723
with:
24+
php-version: 8.2
4825
node-version: 20.6.0
49-
- name: Inject access token in .npmrc
50-
run: |
51-
echo "registry=https://npm.pkg.github.com/helsingborg-stad" >> ~/.npmrc
52-
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> ~/.npmrc
53-
- name: Run CI
54-
if: ${{ hashFiles('package.json') != '' && hashFiles('package-lock.json') != '' }}
55-
run: npm ci --no-progress --no-audit
56-
- name: Run install
57-
if: ${{ hashFiles('package.json') != '' && hashFiles('package-lock.json') == '' }}
58-
run: npm install --no-progress --no-audit
59-
- name: Run build
60-
if: ${{ hashFiles('package-lock.json') != '' && hashFiles('gulp.js') == '' }}
61-
run: npx --yes browserslist@latest --update-db && npm run build
62-
- name: Run gulp
63-
if: ${{ hashFiles('package-lock.json') != '' && hashFiles('gulp.js') != '' }}
64-
run: gulp
65-
- name: Publish npm package to GHCR
66-
run: npm publish --registry=https://npm.pkg.github.com
67-
env:
68-
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69-
- name: Create assets zip
70-
uses: thedoctor0/[email protected]
71-
if: ${{ hashFiles('dist/') != '' }}
72-
with:
73-
type: zip
74-
filename: '../assets.zip'
75-
directory: dist
76-
exclusions: '*.git* /*node_modules/* */source/sass/* */source/js/*'
77-
- name: Upload Assets
78-
uses: helsingborg-stad/[email protected]
79-
if: ${{ hashFiles('assets.zip') != '' }}
80-
with:
81-
artifact: "assets.zip"
82-
gh_token: ${{ secrets.GITHUB_TOKEN }}
83-
release: ${{ env.LATEST_RELEASE }}
84-
repo: ${{ github.repository }}
85-
artifact_mime_type: application/zip
26+
skip-release: true # custom flag you added
27+
28+
- name: Pull latest main with bumped version
29+
run: |
30+
git fetch origin main
31+
git checkout main
32+
git pull origin main
33+
34+
- name: Extract version
35+
id: version
36+
run: |
37+
VERSION=$(jq -r '.version' package.json)
38+
echo "version=$VERSION" >> $GITHUB_OUTPUT
39+
echo "Detected version: $VERSION"
8640
41+
# --- PHP Build ---
8742
- name: Setup PHP
8843
uses: shivammathur/setup-php@v2
89-
if: ${{ hashFiles('composer.json') != '' }}
9044
with:
91-
tools: composer
92-
93-
- name: Build PHP
94-
if: ${{ hashFiles('composer.json') != '' }}
45+
php-version: "8.2"
46+
47+
- name: Configure npm for GitHub Packages
9548
run: |
96-
composer install --prefer-dist --no-progress --no-dev
97-
composer dump-autoload
49+
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > ~/.npmrc
50+
echo "@helsingborg-stad:registry=https://npm.pkg.github.com" >> ~/.npmrc
51+
52+
- name: Run PHP build script
53+
run: php build.php --no-composer --cleanup --release
9854

99-
- name: Process removables
55+
- name: Overwrite .gitignore for release branch
10056
run: |
101-
removables=('.git'
102-
'.gitignore'
103-
'.github'
104-
'.gitattributes'
105-
'build.php'
106-
'.npmrc'
107-
'composer.json'
108-
'composer.lock'
109-
'env-example'
110-
'webpack.config.js'
111-
'package-lock.json'
112-
'package.json'
113-
'phpunit.xml.dist'
114-
'README.md'
115-
'gulpfile.js'
116-
'./node_modules/'
117-
'./source/sass/'
118-
'./source/js/'
119-
'LICENSE'
120-
'babel.config.js'
121-
'yarn.lock'
122-
)
123-
for item in ${removables[@]}; do rm -rf $item; done;
57+
printf "/*\n!/dist\n!/dist/**\n!/source/php\n!/source/php/**\n" > .gitignore
12458
125-
- name: Create release zip
126-
uses: thedoctor0/[email protected]
127-
with:
128-
type: zip
129-
filename: 'release.zip'
130-
exclusions: '*.git* /*node_modules/* */source/sass/* */source/js/*'
131-
132-
- name: Upload release
133-
uses: helsingborg-stad/[email protected]
59+
# --- Release Branch Commit ---
60+
- name: Commit and push release branch
61+
run: |
62+
git config user.name "CI Release Bot"
63+
git config user.email "[email protected]"
64+
git checkout -B release
65+
git add -A
66+
git commit -m "Release build from $GITHUB_SHA" || echo "No changes to commit"
67+
git push origin release --force
68+
69+
# --- Tag and create GitHub Release on release branch ---
70+
- name: Create and push tag
71+
run: |
72+
TAG="${{ steps.version.outputs.version }}"
73+
# skip if tag already exists
74+
if git ls-remote --tags origin | grep -q "refs/tags/$TAG$"; then
75+
echo "Tag $TAG already exists, skipping tag creation."
76+
exit 0
77+
fi
78+
git tag "$TAG"
79+
git push origin "$TAG"
80+
81+
- name: Wait for tag propagation
82+
run: sleep 5
83+
84+
- name: Create GitHub Release
85+
uses: ncipollo/release-action@v1
13486
with:
135-
artifact: "release.zip"
136-
gh_token: ${{ secrets.GITHUB_TOKEN }}
137-
release: ${{ env.LATEST_RELEASE }}
138-
repo: ${{ github.repository }}
139-
artifact_mime_type: application/zip
87+
tag: ${{ steps.version.outputs.version }}
88+
generateReleaseNotes: true

build.php

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
}
77

88
/* Parameters:
9-
--no-composer Does not install vendors. Just create the autoloader.
10-
--cleanup Remove removeables.
11-
--allow-gulp Allow gulp to be used.
9+
--no-composer Does not install vendors via composer
10+
--cleanup Remove removeables
11+
--install-npm Installs npm package as per package.json name field
12+
--release Does not run composer install and does not remove .git
1213
*/
1314

1415
// Any command needed to run and build plugin assets when newly cheched out of repo.
@@ -17,57 +18,70 @@
1718
//Add composer build, if flag --no-composer is undefined.
1819
//Dump autloader.
1920
//Only if composer.json exists.
20-
if(file_exists('composer.json')) {
21-
if(is_array($argv) && !in_array('--no-composer', $argv)) {
22-
$buildCommands[] = 'composer install --prefer-dist --no-progress --no-dev';
21+
if (file_exists('composer.json')) {
22+
if (is_array($argv) && !in_array('--no-composer', $argv)) {
23+
$buildCommands[] = 'composer install --prefer-dist --no-progress --no-dev';
2324
}
25+
2426
$buildCommands[] = 'composer dump-autoload';
2527
}
2628

2729
//Run npm if package.json is found
28-
if(file_exists('package.json') && file_exists('package-lock.json')) {
29-
$buildCommands[] = 'npm ci --no-progress --no-audit';
30-
} elseif(file_exists('package.json') && !file_exists('package-lock.json')) {
31-
$buildCommands[] = 'npm install --no-progress --no-audit';
32-
}
33-
34-
//Run build if package-lock.json is found
35-
if(file_exists('package-lock.json') && !file_exists('gulp.js')) {
36-
$buildCommands[] = 'npx --yes browserslist@latest --update-db';
37-
$buildCommands[] = 'npm run build';
38-
} elseif(file_exists('package-lock.json') && file_exists('gulp.js') && is_array($argv) && in_array('--allow-gulp', $argv)) {
39-
$buildCommands[] = 'gulp';
30+
if (file_exists('package.json') && file_exists('package-lock.json')) {
31+
if (is_array($argv) && !in_array('--install-npm', $argv)) {
32+
$buildCommands[] = 'npm ci --no-progress --no-audit';
33+
$buildCommands[] = 'npm run build';
34+
} else {
35+
$npmPackage = json_decode(file_get_contents('package.json'));
36+
$buildCommands[] = "npm install $npmPackage->name";
37+
$buildCommands[] = "rm -rf ./dist";
38+
$buildCommands[] = "mv node_modules/$npmPackage->name/dist ./";
39+
}
40+
} elseif (file_exists('package.json') && !file_exists('package-lock.json')) {
41+
if (is_array($argv) && !in_array('--install-npm', $argv)) {
42+
$buildCommands[] = 'npm install --no-progress --no-audit';
43+
$buildCommands[] = 'npm run build';
44+
} else {
45+
$npmPackage = json_decode(file_get_contents('package.json'));
46+
$buildCommands[] = "npm install $npmPackage->name";
47+
$buildCommands[] = "rm -rf ./dist";
48+
$buildCommands[] = "mv node_modules/$npmPackage->name/dist ./";
49+
}
4050
}
4151

4252
// Files and directories not suitable for prod to be removed.
4353
$removables = [
44-
'.git',
4554
'.gitignore',
4655
'.github',
4756
'.gitattributes',
4857
'build.php',
58+
'build.js',
4959
'.npmrc',
50-
'composer.json',
60+
//'composer.json',
5161
'composer.lock',
5262
'env-example',
5363
'webpack.config.js',
5464
'package-lock.json',
5565
'package.json',
5666
'phpunit.xml.dist',
5767
'README.md',
58-
'gulpfile.js',
5968
'./node_modules/',
6069
'./source/sass/',
6170
'./source/js/',
6271
'LICENSE',
6372
'babel.config.js',
6473
'yarn.lock',
74+
'.devcontainer',
6575
'vite.config.mjs',
6676
'tsconfig.json',
6777
'mago.toml',
6878
'.vscode',
6979
];
7080

81+
if (is_array($argv) && !in_array('--release', $argv)) {
82+
$removables = array_merge($removables, ['.git']);
83+
}
84+
7185
$dirName = basename(dirname(__FILE__));
7286

7387
// Run all build commands.
@@ -78,14 +92,14 @@
7892
$timeStart = microtime(true);
7993
$exitCode = executeCommand($buildCommand);
8094
$buildTime = round(microtime(true) - $timeStart);
81-
print "---- Done build command '$buildCommand' for $dirName. Build time: $buildTime seconds. ----\n";
95+
print "---- Done build command '$buildCommand' for $dirName. Build time: $buildTime seconds. ----\n\n";
8296
if ($exitCode > 0) {
8397
exit($exitCode);
8498
}
8599
}
86100

87101
// Remove files and directories if '--cleanup' argument is supplied to save local developers from disasters.
88-
if(is_array($argv) && in_array('--cleanup', $argv)) {
102+
if (is_array($argv) && in_array('--cleanup', $argv)) {
89103
foreach ($removables as $removable) {
90104
if (file_exists($removable)) {
91105
print "Removing $removable from $dirName\n";
@@ -117,7 +131,7 @@ function executeCommand($command)
117131
$liveOutput = fread($proc, 4096);
118132
$completeOutput = $completeOutput . $liveOutput;
119133
print $liveOutput;
120-
@ flush();
134+
@flush();
121135
}
122136

123137
pclose($proc);

0 commit comments

Comments
 (0)