-
Notifications
You must be signed in to change notification settings - Fork 2
191 lines (164 loc) · 8.79 KB
/
codecoverage-main.yml
File metadata and controls
191 lines (164 loc) · 8.79 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
name: Codecoverage-Main
# Runs PHPUnit unit and Codeception wp-browser wpunit tests, merges the code coverage, commits the html report to
# GitHub Pages, generates a README badge with the coverage percentage.
on:
push:
branches:
- main
pull_request:
types: [ opened, reopened, ready_for_review, synchronize ]
branches:
- main
workflow_dispatch:
jobs:
codecoverage:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:5.7 # Password auth did not work on 8.0 on PHP 7.3, it did seem to work for PHP 7.4+
env: # These are the same username and password as the wp-env container defaults
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: tests-wordpress
ports: # This mapping matches the .wp-env.json configuration
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
strategy:
matrix: # Supported PHP versions
php: [ '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
steps:
- name: Checkout
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
fetch-depth: 0 # attempting to get all branch names.
# TODO: a DEPLOY_KEY is needed to enable gh-pages for the first time (the branch can be created and pushed but
# it will not be reachable as a website).
# Consider @see https://github.com/peaceiris/actions-gh-pages
# - name: Check does gh-pages branch need to be created
# run: |
# git branch -l;
# if [[ $(git branch -l gh-pages) == "" ]]; then
# gh_pages_branch_needed=true;
# echo "gh-pages branch is needed";
# else
# gh_pages_branch_needed=false
# echo "gh-pages branch already exists";
# fi
# echo "GH_PAGES_BRANCH_NEEDED=$gh_pages_branch_needed" >> $GITHUB_ENV;
# mkdir gh-pages
#
# - name: Maybe create gh-pages branch
# if: ${{ env.GH_PAGES_BRANCH_NEEDED }}
# uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
# with:
# github_token: ${{ secrets.GITHUB_TOKEN }}
# publish_dir: ./gh-pages
# force_orphan: true
# allow_empty_commit: true
# commit_message: "🤖 Creating gh-pages branch"
- name: Checkout GitHub Pages branch for code coverage report
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
ref: gh-pages
path: gh-pages
- name: Install PHP
uses: shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1 # v2.36.0
with:
php-version: ${{ matrix.php }}
coverage: xdebug
tools: composer, jaschilz/php-coverage-badger
extensions: zip
- name: Read .env.testing
uses: c-py/action-dotenv-to-setenv@925b5d99a3f1e4bd7b4e9928be4e2491e29891d9 # v5
with:
env-file: .env.testing
- name: Run composer install
continue-on-error: true
run: composer install -v
- name: Allow writing to wp-content
if: ${{ hashFiles('wp-content') != '' }} # TODO: This may be incorrect since it's a directory.
run: sudo chmod -R a+w wp-content
# On main, there will be a previous coverage report. On PRs we create a new directory using the commit SHA.
- name: Clear previous code coverage
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/main') }}
run: |
rm -rf gh-pages/phpunit || true;
mkdir gh-pages/phpunit || true;
- name: Run unit tests
if: ${{ hashFiles('tests/phpunit/bootstrap.php') != '' }} # Only run unit tests if they are present.
run: XDEBUG_MODE=coverage vendor/bin/phpunit --bootstrap tests/phpunit/bootstrap.php --coverage-php tests/_output/unit.cov --debug
- name: Run wpunit tests
run: XDEBUG_MODE=coverage vendor/bin/codecept run wpunit --coverage tests/_output/wpunit.cov --debug
# For PRs, we'll generate the coverage report on each pushed commit
- name: Merge code coverage for PR
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
run: |
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
vendor/bin/phpcov merge --clover gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/clover.xml --php gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov --html gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/html/ tests/_output/;
# On main, we want it output to a different path
- name: Merge code coverage for main
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/main') }}
run: |
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
vendor/bin/phpcov merge --clover gh-pages/phpunit/clover.xml --php gh-pages/phpunit/phpunit.cov --html gh-pages/phpunit/html/ tests/_output/;
# This makes the coverage percentage available in `{{ steps.coverage-percentage.outputs.coverage-rounded }}`.
- name: Check test coverage
if: ${{ matrix.php == '7.3' }}
uses: johanvanhelden/gha-clover-test-coverage-check@2543c79a701f179bd63aa14c16c6938c509b2cec # v1
id: coverage-percentage
with:
percentage: 25
exit: false
filename: clover.xml
rounded-precision: "0"
# See: https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/
- name: Add `.nojekyll` file so code coverage report successfully deploys to gh-pages
if: ${{ (matrix.php == '7.3') }}
working-directory: gh-pages
run: |
touch .nojekyll
git add -- .nojekyll *
- name: Update README coverage badge
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/main') }} # only commit on main, on the PHP version we're using in production.
run: php-coverage-badger clover.xml gh-pages/phpunit/coverage.svg
- name: Generate PR coverage badge
if: ${{ (matrix.php == '7.3') && github.event_name == 'pull_request' }}
run: php-coverage-badger clover.xml gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/coverage.svg
- name: Commit code coverage to gh-pages
if: ${{ matrix.php == '7.3' }}
uses: stefanzweifel/git-auto-commit-action@28e16e81777b558cc906c8750092100bbb34c5e3 # v7.0.0
with:
repository: gh-pages
branch: gh-pages
commit_message: ${{ format('🤖 Save code coverage report to gh-pages {0}%', steps.coverage-percentage.outputs.coverage-rounded) }}
commit_options: ""
env:
GITHUB_TOKEN: "${{ github.token }}"
- name: Add coverage badge to PR comment
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
run: |
echo "[](https://newfold-labs.github.io/wp-module-performance/${{ github.event.pull_request.head.sha }}/phpunit/html/)" >> coverage-comment.md
echo "" >> coverage-comment.md
echo "" >> coverage-comment.md
- name: Add coverage report link to PR comment
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
run: |
echo "${{ format('[project coverage report {0}%](https://newfold-labs.github.io/wp-module-performance/{1}/phpunit/html/) @ {2}', steps.coverage-percentage.outputs.coverage-rounded, github.event.pull_request.head.sha, github.event.pull_request.head.sha) }}" >> coverage-comment.md
echo "" >> coverage-comment.md
echo "" >> coverage-comment.md
- name: Add phpcov uncovered lines report to PR comment
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
continue-on-error: true # phpcov can fail if there are no uncovered lines
run: |
BRANCHED_COMMIT=$(git rev-list origin..HEAD | tail -n 1);
echo "BRANCHED_COMMIT=$BRANCHED_COMMIT"
git diff $BRANCHED_COMMIT...${{ github.event.pull_request.head.sha }} > branch.diff;
cat branch.diff;
OUTPUT=${vendor/bin/phpcov patch-coverage --path-prefix $(pwd) ./gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov branch.diff || true}
echo $OUTPUT;
echo "$OUTPUT" >> coverage-comment.md
- name: Add coverage PR comment
uses: mshick/add-pr-comment@b8f338c590a895d50bcbfa6c5859251edc8952fc # v2.8.2
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
with:
message-id: coverage-report
message-path: coverage-comment.md