Skip to content

Instantly share code, notes, and snippets.

@k2tzumi
Last active Jan 16, 2022
Embed
What would you like to do?
PHPUnitのコードカバレッジレポートをPRコメントするGitHub Actionsワークフロー
name: code coverage
on:
pull_request:
types: [opened, reopened, synchronize]
paths: '**.php'
jobs:
cleanup-runs:
runs-on: ubuntu-latest
steps:
- uses: rokroskar/workflow-run-cleanup-action@v0.2.2
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
code-coverage-job:
name: code coverage job.
runs-on: ubuntu-latest
services:
mysql:
image: mysql:${{ secrets.MYSQL_VERSION }}
options: --health-cmd "mysqladmin ping -h localhost" --health-interval 20s --health-timeout 10s --health-retries 10
env:
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: ${{ secrets.MYSQL_DATABASE }}
ports:
- "3306:3306"
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 128
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ secrets.PHP_VERSION }}
coverage: none
- name: Change dot env
run: |
REPOSITORY_NAME=$(jq -r '.repository.name' $GITHUB_EVENT_PATH)
sed -i -e "s/^DB_HOST_READ=docker-mysql/DB_HOST_READ=127.0.0.1/g" .env
sed -i -e "s/^DB_HOST_WRITE=docker-mysql/DB_HOST_WRITE=127.0.0.1/g" .env
sed -i -e "s/^JWT_STORAGE_PATH=storage\/jwt\//JWT_STORAGE_PATH=\/home\/runner\/work\/${REPOSITORY_NAME}\/${REPOSITORY_NAME}\/storage\/jwt\//g" .env
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer modules
uses: actions/cache@v2
env:
cache-name: cache-composer-modules
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: composer install
run: |
composer install -n --prefer-dist
- name: create database for phpunittest
run: |
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan mysql:createdb
- name: phpunit test
run: |
phpdbg -qrr vendor/bin/phpunit -d memory_limit=-1 --coverage-text --coverage-html storage/logs/coverage-report tests/ | \
sed -E "s/"$'\E'"\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g" | \
grep "Code Coverage Report:" -A6 > storage/logs/coverage-summary.log
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-1
- name: Upload coverage html
id: upload-s3
run: |
PR_NUM=$(jq -r '.number' $GITHUB_EVENT_PATH)
REPOSITORY_NAME=$(jq -r '.repository.name' $GITHUB_EVENT_PATH)
aws s3 sync storage/logs/coverage-report/ s3://${{ secrets.AWS_S3_BUCKET }}/coverage-report/$REPOSITORY_NAME/$PR_NUM/ --delete
aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --paths '/coverage-report/$REPOSITORY_NAME/$PR_NUM/*'
echo "* [レポート全体](https://${{ secrets.AWS_S3_WEB_HOST }}/coverage-report/$REPOSITORY_NAME/$PR_NUM/index.html)" | tee -a storage/logs/coverage-report-urls
HEAD_SHA=$(jq -r '.pull_request.head.sha' $GITHUB_EVENT_PATH)
BASE_SHA=$(jq -r '.pull_request.base.sha' $GITHUB_EVENT_PATH)
git diff --name-only --diff-filter=ACMR ${BASE_SHA}..${HEAD_SHA} -- 'app/**/*.php' | \
grep -v "tests/" | \
cut -d"/" -f4- | \
xargs -I{} echo "* [{}](https://${{ secrets.AWS_S3_WEB_HOST }}/coverage-report/$REPOSITORY_NAME/$PR_NUM/{}.html)" | \
tee -a storage/logs/coverage-report-urls
- name: Read coverage summary
id: coverage-summary
uses: juliangruber/read-file-action@v1.0.0
with:
path: storage/logs/coverage-summary.log
- name: Read coverage report urls
id: coverage-report-urls
uses: juliangruber/read-file-action@v1.0.0
with:
path: storage/logs/coverage-report-urls
- name: Covarage summary comment
uses: marocchino/sticky-pull-request-comment@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: coverage-summary
message: |
${{ steps.coverage-summary.outputs.content }}
${{ steps.coverage-report-urls.outputs.content }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment