Skip to content

Commit f075b8a

Browse files
committed
refactor(ci)!: consolidate package configuration
1 parent d79a488 commit f075b8a

6 files changed

Lines changed: 122 additions & 23 deletions

File tree

‎.github/workflows/__test-workflow-continuous-integration.yml‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,57 @@ on:
33
workflow_call: null
44
permissions: {}
55
jobs:
6+
package-configuration:
7+
name: Assert - Package configuration
8+
runs-on: ubuntu-latest
9+
permissions:
10+
contents: read
11+
steps:
12+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
13+
with:
14+
persist-credentials: false
15+
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
16+
with:
17+
script: |
18+
const assert = require('node:assert/strict');
19+
const fs = require('node:fs');
20+
const workflow = fs.readFileSync('.github/workflows/continuous-integration.yml', 'utf8');
21+
const step = workflow.split(' - id: package-config\n')[1]?.split('\n - ')[0];
22+
assert.ok(step, 'Package configuration step must exist');
23+
const script = step.split(' script: |\n')[1]?.replace(/^ {12}/gm, '');
24+
assert.ok(script, 'Package configuration script must exist');
25+
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
26+
const configure = new AsyncFunction('core', 'process', 'require', script);
27+
async function parse(input, container = '') {
28+
const outputs = {};
29+
await configure({
30+
setOutput: (name, value) => { outputs[name] = typeof value === 'string' ? value : JSON.stringify(value); }
31+
}, { env: { PACKAGE_INPUT: input, CONTAINER_INPUT: container } }, require);
32+
return outputs;
33+
}
34+
for (const input of ['', 'false', ' false ']) {
35+
const outputs = await parse(input);
36+
assert.equal(outputs.enabled, 'false', input);
37+
assert.equal(outputs.tests, '');
38+
}
39+
for (const input of ['true', '{}', ' true ']) {
40+
const outputs = await parse(input);
41+
assert.equal(outputs.enabled, 'true', input);
42+
assert.equal(outputs.tests, '');
43+
}
44+
const commands = 'echo "$PACKAGE_NAME"\necho "consumer check"\n';
45+
const custom = await parse(JSON.stringify({ tests: commands }));
46+
assert.equal(custom.enabled, 'true');
47+
assert.equal(custom.tests, commands, 'Preserve multiline commands and shell quoting');
48+
for (const input of ['no', 'null', '[]', '0', '"true"', '{']) {
49+
await assert.rejects(() => parse(input), /package must be true, false, or a JSON object/, input);
50+
}
51+
await assert.rejects(() => parse('{"unknown":true}'), /Unknown package options/);
52+
for (const tests of [true, 1, [], {}, null]) {
53+
await assert.rejects(() => parse(JSON.stringify({ tests })), /must be.*string/);
54+
}
55+
core.info('Package configuration checks passed.');
56+
657
act:
758
name: Act - Build and test Composer project
859
uses: $/.github/workflows/continuous-integration.yml
@@ -27,3 +78,7 @@ jobs:
2778
artifact-ids: ${{ needs.act.outputs.build-artifact-id }}
2879
path: dist
2980
- run: test "$(cat dist/test.txt)" = "build test"
81+
- name: Assert - Package creation is opt-in
82+
env:
83+
PACKAGE_ARTIFACT_ID: ${{ needs.act.outputs.package-artifact-id }}
84+
run: test -z "$PACKAGE_ARTIFACT_ID"

‎.github/workflows/continuous-integration.md‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,15 @@ jobs:
7878
# Optional build output directory relative to working-directory. Uploaded after building and restored at the same path before tests.
7979
build-artifact: ""
8080

81-
# Validate a public Composer source distribution from the Git repository root. Disabled for applications that do not publish a Composer package.
82-
package: false
83-
84-
# Optional commands in an isolated Composer consumer after archive and autoload checks. vendor/bin is on PATH; PACKAGE_NAME, PACKAGE_INSTALL_PATH and PACKAGE_ARCHIVE_PATH identify the package.
85-
package-tests: ""
81+
# Validate a public Composer source distribution from the Git repository root.
82+
# Pass "false" (the default) to disable, "true" for archive and autoload checks,
83+
# or a JSON object with `tests` for additional commands in an isolated Composer consumer.
84+
# Quote booleans because this input has type string. Example: `{"tests":"my-cli --help"}`.
85+
# vendor/bin is on PATH; PACKAGE_NAME, PACKAGE_INSTALL_PATH and PACKAGE_ARCHIVE_PATH identify the package.
86+
# Unknown options and non-string tests are rejected before packaging.
87+
#
88+
# Default: `false`
89+
package: "false"
8690

8791
# Review dependency changes on pull requests.
8892
# Default: `true`
@@ -111,8 +115,12 @@ jobs:
111115
| **`test-cache`** | Optional test cache directory relative to working-directory. Cache generated fixtures or test results, never vendor. | **false** | **string** | - |
112116
| **`build`** | Optional Composer build script. Empty skips builds. | **false** | **string** | - |
113117
| **`build-artifact`** | Optional build output directory relative to working-directory. Uploaded after building and restored at the same path before tests. | **false** | **string** | - |
114-
| **`package`** | Validate a public Composer source distribution from the Git repository root. Disabled for applications that do not publish a Composer package. | **false** | **boolean** | `false` |
115-
| **`package-tests`** | Optional commands in an isolated Composer consumer after archive and autoload checks. vendor/bin is on PATH; PACKAGE_NAME, PACKAGE_INSTALL_PATH and PACKAGE_ARCHIVE_PATH identify the package. | **false** | **string** | - |
118+
| **`package`** | Validate a public Composer source distribution from the Git repository root. | **false** | **string** | `false` |
119+
| | Pass "false" (the default) to disable, "true" for archive and autoload checks, | | | |
120+
| | or a JSON object with `tests` for additional commands in an isolated Composer consumer. | | | |
121+
| | Quote booleans because this input has type string. Example: `{"tests":"my-cli --help"}`. | | | |
122+
| | vendor/bin is on PATH; PACKAGE_NAME, PACKAGE_INSTALL_PATH and PACKAGE_ARCHIVE_PATH identify the package. | | | |
123+
| | Unknown options and non-string tests are rejected before packaging. | | | |
116124
| **`dependency-review`** | Review dependency changes on pull requests. | **false** | **boolean** | `true` |
117125

118126
<!-- inputs:end -->

‎.github/workflows/continuous-integration.yml‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ on:
5555
type: string
5656
default: ""
5757
package:
58-
description: Validate a public Composer source distribution from the Git repository root. Disabled for applications that do not publish a Composer package.
59-
type: boolean
60-
default: false
61-
package-tests:
62-
description: Optional commands in an isolated Composer consumer after archive and autoload checks. vendor/bin is on PATH; PACKAGE_NAME, PACKAGE_INSTALL_PATH and PACKAGE_ARCHIVE_PATH identify the package.
58+
description: |
59+
Validate a public Composer source distribution from the Git repository root.
60+
Pass "false" (the default) to disable, "true" for archive and autoload checks,
61+
or a JSON object with `tests` for additional commands in an isolated Composer consumer.
62+
Quote booleans because this input has type string. Example: `{"tests":"my-cli --help"}`.
63+
vendor/bin is on PATH; PACKAGE_NAME, PACKAGE_INSTALL_PATH and PACKAGE_ARCHIVE_PATH identify the package.
64+
Unknown options and non-string tests are rejected before packaging.
6365
type: string
64-
default: ""
66+
default: "false"
6567
dependency-review:
6668
description: Review dependency changes on pull requests.
6769
type: boolean
@@ -81,15 +83,38 @@ jobs:
8183
outputs:
8284
versions: ${{ steps.config.outputs.versions }}
8385
primary-version: ${{ steps.config.outputs.primary-version }}
86+
package-enabled: ${{ steps.package-config.outputs.enabled }}
87+
package-tests: ${{ steps.package-config.outputs.tests }}
8488
steps:
89+
- id: package-config
90+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
91+
env:
92+
PACKAGE_INPUT: ${{ inputs.package }}
93+
with:
94+
script: |
95+
let config;
96+
try {
97+
config = JSON.parse(process.env.PACKAGE_INPUT.trim() || 'false');
98+
} catch {
99+
throw new Error('package must be true, false, or a JSON object.');
100+
}
101+
if (typeof config !== 'boolean' && (!config || typeof config !== 'object' || Array.isArray(config))) {
102+
throw new Error('package must be true, false, or a JSON object.');
103+
}
104+
const enabled = config !== false;
105+
if (typeof config === 'boolean') config = {};
106+
const unknown = Object.keys(config).filter(key => key !== 'tests');
107+
if (unknown.length) throw new Error(`Unknown package options: ${unknown.join(', ')}.`);
108+
const { tests = '' } = config;
109+
if (typeof tests !== 'string') throw new Error('package.tests must be a string.');
110+
core.setOutput('enabled', enabled);
111+
core.setOutput('tests', tests);
85112
- id: config
86113
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
87114
env:
88115
VERSIONS: ${{ inputs.php-versions }}
89116
COVERAGE: ${{ inputs.coverage }}
90117
TEST_RUNNER: ${{ inputs.test-runner }}
91-
PACKAGE: ${{ inputs.package }}
92-
PACKAGE_TESTS: ${{ inputs.package-tests }}
93118
BUILD: ${{ inputs.build }}
94119
BUILD_ARTIFACT: ${{ inputs.build-artifact }}
95120
TEST_CACHE: ${{ inputs.test-cache }}
@@ -101,7 +126,6 @@ jobs:
101126
}
102127
if (!['', 'github', 'codecov'].includes(process.env.COVERAGE)) throw new Error('coverage must be github, codecov or empty.');
103128
if (!['phpunit', 'composer'].includes(process.env.TEST_RUNNER)) throw new Error('test-runner must be phpunit or composer.');
104-
if (process.env.PACKAGE_TESTS.trim() && process.env.PACKAGE !== 'true') throw new Error('package-tests requires package: true.');
105129
if (process.env.BUILD_ARTIFACT.trim() && !process.env.BUILD.trim()) throw new Error('build-artifact requires a build script.');
106130
for (const key of ['BUILD_ARTIFACT', 'TEST_CACHE']) {
107131
const value = process.env[key];
@@ -210,7 +234,7 @@ jobs:
210234
runner: ${{ inputs.test-runner }}
211235
coverage: ${{ matrix.php-version == needs.configuration.outputs.primary-version && inputs.coverage || '' }}
212236
package:
213-
if: inputs.package
237+
if: needs.configuration.outputs.package-enabled == 'true'
214238
needs: configuration
215239
runs-on: ubuntu-latest
216240
permissions:
@@ -236,4 +260,4 @@ jobs:
236260
- uses: $/actions/package/test
237261
with:
238262
archive-path: ${{ steps.archive.outputs.archive-path }}
239-
commands: ${{ inputs.package-tests }}
263+
commands: ${{ needs.configuration.outputs.package-tests }}

‎.github/workflows/release.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
contents: read
6363
id-token: write
6464
with:
65-
package: true
65+
package: "true"
6666
publish:
6767
needs: [plan, validate]
6868
uses: hoverkraft-tech/ci-github-php/.github/workflows/release.yml@<commit-sha>

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
# contents: read
5454
# id-token: write
5555
# with:
56-
# package: true
56+
# package: "true"
5757
# publish:
5858
# needs: [plan, validate]
5959
# uses: hoverkraft-tech/ci-github-php/.github/workflows/release.yml@<commit-sha>

‎docs/usage/continuous-integration.md‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ publishing and project-specific documentation transformations remain in the call
109109

110110
## Package checks
111111

112-
Set `package: true` for a public Composer package at the Git repository root. The
112+
Set `package: "true"` for a public Composer package at the Git repository root. The
113113
package job archives the checked-out commit using `git archive`, respecting
114114
`.gitattributes` `export-ignore` and `export-subst`. Uncommitted files and generated
115115
build output are not included.
@@ -119,8 +119,20 @@ autoload files, and binaries, installs the archive in an isolated consumer with
119119
production dependencies, checks platform requirements, and loads the autoloader.
120120
Installation hooks and plugins remain disabled in that consumer.
121121

122-
Use `package-tests` only for behavior specific to your package, such as rendering
123-
a template or invoking a CLI. Commands execute in the consumer, with `vendor/bin`
122+
Packaging is disabled by default. Omit `package` or pass `"false"` to skip it.
123+
The input is a string, so boolean values must be quoted. A JSON object enables
124+
packaging with specific options; unknown options and non-string `tests` fail
125+
configuration validation.
126+
127+
Use `tests` only for behavior specific to your package, such as rendering a
128+
template or invoking a CLI:
129+
130+
```yaml
131+
with:
132+
package: '{"tests":"my-cli --help"}'
133+
```
134+
135+
Commands execute in the consumer, with `vendor/bin`
124136
on `PATH` and `PACKAGE_NAME`, `PACKAGE_INSTALL_PATH`, and `PACKAGE_ARCHIVE_PATH`
125137
available. The temporary consumer is removed after success or failure. Modifying
126138
the source archive fails validation.

0 commit comments

Comments
 (0)