Skip to content

Commit 8224454

Browse files
committed
Add reusable GitHub Actions workflows for PHP linting and unit testing
1 parent 491a54b commit 8224454

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

.github/workflows/php.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This file defines a reusable GitHub Actions workflow for PHP projects.
2+
# It runs linting and unit tests across multiple PHP versions, only running tests if a test directory exists.
3+
4+
name: Jobs
5+
6+
on:
7+
workflow_call:
8+
inputs:
9+
php-version:
10+
required: false
11+
type: string
12+
default: '["8.2", "8.3", "8.4"]'
13+
14+
15+
jobs:
16+
17+
phplint:
18+
name: Lint
19+
strategy:
20+
matrix:
21+
php: ${{ fromJson(inputs.php-version) }}
22+
uses: icinga/github-actions/.github/workflows/phplint.yml@main
23+
with:
24+
php-version: ${{ matrix.php }}
25+
secrets: inherit
26+
27+
28+
test-exists:
29+
runs-on: ubuntu-latest
30+
name: test-exists
31+
outputs:
32+
has-tests: ${{ steps.check.outputs.exists }}
33+
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v4
37+
- name: Check directory
38+
id: check
39+
run: |
40+
if [ -d "test" ]; then
41+
echo "Test directory exists"
42+
echo "exists=true" >> $GITHUB_OUTPUT
43+
else
44+
echo "Test directory does not exist"
45+
echo "exists=false" >> $GITHUB_OUTPUT
46+
fi
47+
48+
phpunit:
49+
name: Unit Test
50+
needs: test-exists
51+
if: needs.test-exists.outputs.has-tests == 'true'
52+
strategy:
53+
matrix:
54+
php: ${{ fromJson(inputs.php-version) }}
55+
uses: icinga/github-actions/.github/workflows/phpstan.yml
56+
with:
57+
php-version: ${{ matrix.php }}
58+
secrets: inherit

.github/workflows/phplint.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Lint
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
php-version:
7+
type: string
8+
required: true
9+
10+
jobs:
11+
static-analysis:
12+
name: Static analysis for version ${{ inputs.php-version }}
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: '1'
20+
21+
- name: Set up PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ inputs.php-version }}
25+
tools: parallel-lint, phpcs
26+
27+
- name: Set up dependencies
28+
run: |
29+
git clone --depth 1 https://github.com/Icinga/icingaweb2.git vendor/icingaweb2
30+
git clone --depth 1 -b snapshot/nightly https://github.com/Icinga/icinga-php-library.git vendor/icinga-php-library
31+
git clone --depth 1 -b snapshot/nightly https://github.com/Icinga/icinga-php-thirdparty.git vendor/icinga-php-thirdparty
32+
33+
- name: Install composer
34+
if: hashFiles('composer.json') != ''
35+
run: |
36+
if [ -f composer.json ]; then
37+
composer install --no-progress --prefer-dist
38+
fi
39+
40+
- name: Lint
41+
run: parallel-lint --exclude vendor .
42+
43+
- name: Create phpcs.xml
44+
if: hashFiles('phpcs.xml') == ''
45+
run: |
46+
cat <<'EOF' >phpcs.xml
47+
<?xml version="1.0"?>
48+
<ruleset name="PSR12">
49+
<!-- Test all PHP files except those in vendor/ -->
50+
<file>./</file>
51+
<arg name="extensions" value="php"/>
52+
<exclude-pattern>vendor/*</exclude-pattern>
53+
54+
<arg name="report-width" value="auto"/>
55+
<arg name="report-full"/>
56+
<arg name="report-gitblame"/>
57+
<arg name="report-summary"/>
58+
<arg name="encoding" value="UTF-8"/>
59+
60+
<rule ref="PSR12"/>
61+
</ruleset>
62+
EOF
63+
64+
- name: Check Code Style
65+
run: |
66+
phpcs -wps --colors

.github/workflows/phpunit.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: PHP Unit Tests
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
php-version:
7+
type: string
8+
required: true
9+
10+
jobs:
11+
test:
12+
name: Unit tests with php ${{ inputs.php-version }}
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code base
17+
uses: actions/checkout@v4
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ inputs.php-version }}
23+
tools: phpunit
24+
25+
- name: Set up Icinga Web
26+
run: |
27+
git clone --depth 1 https://github.com/Icinga/icingaweb2.git _icingaweb2
28+
ln -s `pwd` _icingaweb2/modules/icingadb
29+
30+
- name: Set up Libraries
31+
run: |
32+
mkdir _libraries
33+
git clone --depth 1 -b snapshot/nightly https://github.com/Icinga/icinga-php-library.git _libraries/ipl
34+
git clone --depth 1 -b snapshot/nightly https://github.com/Icinga/icinga-php-thirdparty.git _libraries/vendor
35+
36+
- name: PHPUnit
37+
env:
38+
ICINGAWEB_LIBDIR: _libraries
39+
run: phpunit --bootstrap _icingaweb2/test/php/bootstrap.php

0 commit comments

Comments
 (0)