|
| 1 | +name: Codecoverage |
| 2 | + |
| 3 | +# Runs codeception unit and wpunit tests, merges the code coverage, commits the html report to |
| 4 | +# GitHub Pages, generates a README badge with the coverage percentage. |
| 5 | +# |
| 6 | +# Requires a gh-pages branch already created. |
| 7 | +# |
| 8 | +# @author BrianHenryIE |
| 9 | + |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: |
| 13 | + - master |
| 14 | + |
| 15 | +jobs: |
| 16 | + |
| 17 | + ci: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + services: |
| 21 | + mysql: |
| 22 | + image: mysql:8.0 |
| 23 | + env: |
| 24 | + MYSQL_ROOT_PASSWORD: password |
| 25 | + MYSQL_DATABASE: test |
| 26 | + ports: |
| 27 | + - 3306 |
| 28 | + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Git checkout |
| 32 | + uses: actions/checkout@v2 |
| 33 | + |
| 34 | + # Fails here when pages branch does not exist. |
| 35 | + |
| 36 | + - uses: actions/checkout@v2 |
| 37 | + with: |
| 38 | + ref: gh-pages |
| 39 | + path: tests/_output/html |
| 40 | + |
| 41 | +# - name: Make envfile from Secrets |
| 42 | +# uses: SpicyPizza/create-envfile@v1 |
| 43 | +# with: |
| 44 | +# envkey_SECRET_KEY: ${{ secrets.SECRET_KEY }} |
| 45 | +# file_name: .env.secret |
| 46 | + |
| 47 | +# - name: Create Composer auth.json from Secrets |
| 48 | +# run: php -r "file_put_contents( 'auth.json', json_encode( [ 'http-basic' => [ 'blog.brianhenry.ie' => [ 'username' => '"${{ secrets.COMPOSER_AUTH_SECRET }}"', 'password' => 'satispress' ] ] ] ) );" |
| 49 | + |
| 50 | + - name: Read .env.testing |
| 51 | + uses: c-py/action-dotenv-to-setenv@v2 |
| 52 | + with: |
| 53 | + env-file: .env.testing |
| 54 | + |
| 55 | + - name: Configure MySQL |
| 56 | + run: | |
| 57 | + mysql -h 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -u root -ppassword -e "CREATE USER '"$TEST_DB_USER"'@'%' IDENTIFIED WITH mysql_native_password BY '"$TEST_DB_PASSWORD"';"; |
| 58 | + mysql -h 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -u root -ppassword -e "CREATE DATABASE "$TEST_SITE_DB_NAME"; USE "$TEST_SITE_DB_NAME"; GRANT ALL PRIVILEGES ON "$TEST_SITE_DB_NAME".* TO '"$TEST_DB_USER"'@'%';"; |
| 59 | + mysql -h 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -u root -ppassword -e "CREATE DATABASE "$TEST_DB_NAME"; USE "$TEST_DB_NAME"; GRANT ALL PRIVILEGES ON "$TEST_DB_NAME".* TO '"$TEST_DB_USER"'@'%';"; |
| 60 | +
|
| 61 | + - name: Update configs for GA |
| 62 | + run: | |
| 63 | + find . -depth \( -name '.env.testing' \) -exec sed -i "s/=\"127.0.0.1\"/=\"127.0.0.1:${{ job.services.mysql.ports['3306'] }}\"/g" {} + |
| 64 | + find . -depth \( -name 'composer.json' \) -exec sed -i "s/127.0.0.1/127.0.0.1:${{ job.services.mysql.ports['3306'] }}/g" {} + |
| 65 | + find . -depth \( -name '.env.testing' \) -exec sed -i "s/localhost:8080\/$PLUGIN_SLUG/localhost:8080/g" {} + |
| 66 | +
|
| 67 | + - name: Run composer install |
| 68 | + uses: php-actions/composer@v1 |
| 69 | + |
| 70 | + - name: Run unit tests |
| 71 | + run: vendor/bin/codecept run unit --coverage unit.cov || true; |
| 72 | + |
| 73 | + - name: Run wpunit tests |
| 74 | + run: vendor/bin/codecept run wpunit --coverage wpunit.cov || true; |
| 75 | + |
| 76 | + - name: Clear previous code coverage |
| 77 | + run: | |
| 78 | + cd tests/_output/html |
| 79 | + rm -rf * |
| 80 | + cd ../../.. |
| 81 | +
|
| 82 | + - name: Merge code coverage |
| 83 | + run: vendor/bin/phpcov merge --clover tests/_output/clover.xml --html tests/_output/html tests/_output --text; |
| 84 | + |
| 85 | + - name: Edit phpcov html output to work with gh-pages |
| 86 | + run: | |
| 87 | + cd tests/_output/html |
| 88 | + mv .css css; find . -depth -name '*.html' -exec sed -i "s/.css\//css\//" {} + |
| 89 | + mv .icons icons; find . -depth -name '*.html' -exec sed -i "s/.icons\//icons\//" {} + |
| 90 | + mv .js js; find . -depth -name '*.html' -exec sed -i "s/.js\//js\//" {} + |
| 91 | + git add * |
| 92 | + cd ../../.. |
| 93 | +
|
| 94 | + - name: Discard MySQL port changes in .env before commiting other changes |
| 95 | + run: | |
| 96 | + git checkout -- .env.testing |
| 97 | +
|
| 98 | + - name: Commit code coverage to gh-pages |
| 99 | + uses: stefanzweifel/git-auto-commit-action@v4.1.1 |
| 100 | + with: |
| 101 | + repository: tests/_output/html |
| 102 | + branch: gh-pages |
| 103 | + commit_message: "Commit code coverage to gh-pages" |
| 104 | + commit_options: |
| 105 | + env: |
| 106 | + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 107 | + |
| 108 | + - name: Update README badge |
| 109 | + run: vendor/bin/php-coverage-badger tests/_output/clover.xml .github/coverage.svg PHPUnit |
| 110 | + |
| 111 | + - name: Commit code coverage badge |
| 112 | + uses: stefanzweifel/git-auto-commit-action@v4.1.1 |
| 113 | + with: |
| 114 | + file_pattern: .github/coverage.svg |
| 115 | + commit_message: "Commit code coverage badge" |
0 commit comments