Repository navigation
154 lines (142 loc) · 6.25 KB
/
Copy pathci-test-php.yml
File metadata and controls
154 lines (142 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
name: CI / Test PHP
on:
workflow_call:
inputs:
runs-on:
description: "Runner for every job, as JSON: a label such as \"ubuntu-latest\" or a label list such as [\"self-hosted\"]. rtCamp private repos pass [\"self-hosted\"]."
type: string
default: '"ubuntu-latest"'
php-version:
description: "PHP version. Installed on the host (used directly in standalone mode) and passed to wp-env as WP_ENV_PHP_VERSION so the container runs the same version."
type: string
default: "8.3"
node-version:
description: "Node.js version, used only when use-wp-env is true. Leave empty to read .nvmrc from working-dir, then the repo root, falling back to 22."
type: string
default: ""
wp-version:
description: >-
WordPress core for wp-env: 'latest' (newest stable release), 'trunk' (development branch),
or a specific WordPress version (e.g. '6.8' for the latest 6.8.x or '6.8.2' for a specific
patch version). Empty uses the consumer's .wp-env.json. Ignored when use-wp-env is false.
type: string
default: ""
use-wp-env:
description: "Start @wordpress/env before tests for WordPress integration runs."
type: boolean
default: true
test-command:
description: "Shell command that runs PHPUnit. When empty, defaults to 'npm run test:php' if use-wp-env is true, otherwise 'vendor/bin/phpunit'."
type: string
default: ""
composer-flags:
description: "Flags passed to composer install. Leave empty for '--prefer-dist --no-scripts'; pass your own flags to run Composer scripts."
type: string
default: ""
working-dir:
description: "Working directory for monorepos."
type: string
default: "."
permissions:
contents: read
jobs:
test-php:
name: PHPUnit
runs-on: ${{ fromJSON(inputs.runs-on) }}
# Pin the wp-env container's PHP via env: (not $GITHUB_ENV, which is newline-injectable)
# at job scope, so start/run/stop resolve the same environment. WP_ENV_CORE needs a
# lookup, so the "Resolve WordPress core" step validates it before writing $GITHUB_ENV.
env:
WP_ENV_PHP_VERSION: ${{ inputs.use-wp-env && inputs.php-version || '' }}
defaults:
run:
working-directory: ${{ inputs.working-dir }}
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Set up PHP
uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0
with:
php-version: ${{ inputs.php-version }}
coverage: none
tools: composer
# Raises the GitHub API rate limit when composer install resolves a VCS repo.
github-token: ${{ github.token }}
# Node version: the input, else .nvmrc (working-dir, then repo root), else 22.
- name: Resolve Node.js version
id: node
if: ${{ inputs.use-wp-env }}
shell: bash
env:
NODE_VERSION: ${{ inputs.node-version }}
WORKING_DIR: ${{ inputs.working-dir }}
run: |
set -euo pipefail
# setup-node reads the file path from the repo root.
if [ -n "$NODE_VERSION" ]; then
echo "version=$NODE_VERSION" >> "$GITHUB_OUTPUT"
elif [ -s "$GITHUB_WORKSPACE/$WORKING_DIR/.nvmrc" ]; then
echo "file=$WORKING_DIR/.nvmrc" >> "$GITHUB_OUTPUT"
elif [ -s "$GITHUB_WORKSPACE/.nvmrc" ]; then
echo "file=.nvmrc" >> "$GITHUB_OUTPUT"
else
echo "version=22" >> "$GITHUB_OUTPUT"
fi
- name: Set up Node.js
if: ${{ inputs.use-wp-env }}
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: ${{ steps.node.outputs.version }}
node-version-file: ${{ steps.node.outputs.file }}
cache: npm
# setup-node is a JS action: without this it looks for the lockfile at the repo root.
cache-dependency-path: ${{ inputs.working-dir != '.' && format('{0}/package-lock.json', inputs.working-dir) || '' }}
- name: Install Composer dependencies
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0
with:
working-directory: ${{ inputs.working-dir }}
composer-options: ${{ inputs.composer-flags || '--prefer-dist --no-scripts' }}
- name: Install npm dependencies
if: ${{ inputs.use-wp-env }}
run: npm ci
- name: Resolve WordPress core
if: ${{ inputs.use-wp-env && inputs.wp-version != '' }}
shell: bash
env:
WP_VERSION: ${{ inputs.wp-version }}
run: |
set -euo pipefail
case "$WP_VERSION" in
latest) core="https://wordpress.org/latest.zip" ;;
trunk) core="WordPress/WordPress#master" ;;
*)
# x.y means its newest x.y.z; the WordPress/WordPress mirror tags x.y.0 releases as x.y.
if [[ "$WP_VERSION" =~ ^[0-9]+\.[0-9]+$ ]]; then
ref=$(git ls-remote --tags --refs --sort=version:refname https://github.com/WordPress/WordPress.git "$WP_VERSION" "$WP_VERSION.*" \
| tail -n1 | sed 's|.*refs/tags/||')
elif [[ "$WP_VERSION" =~ ^[0-9]+\.[0-9]+\.0$ ]]; then
ref=${WP_VERSION%.0}
else
ref=$WP_VERSION
fi
if [[ ! "$ref" =~ ^[A-Za-z0-9._/-]+$ ]]; then
echo "::error::wp-version '$WP_VERSION' matches no WordPress release."
exit 1
fi
core="WordPress/WordPress#$ref"
;;
esac
echo "WP_ENV_CORE=$core" | tee -a "$GITHUB_ENV"
- name: Start wp-env
if: ${{ inputs.use-wp-env }}
run: npm run wp-env start
- name: Run PHPUnit
shell: bash
env:
TEST_COMMAND: ${{ inputs.test-command != '' && inputs.test-command || (inputs.use-wp-env && 'npm run test:php' || 'vendor/bin/phpunit') }}
run: bash -eo pipefail -c "$TEST_COMMAND"
- name: Stop wp-env
if: ${{ inputs.use-wp-env && always() }}
run: npm run wp-env stop