Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jestのコードカバレッジレポートをPRコメントするGitHub Actionsワークフロー
name: test with code coverage
on:
pull_request:
types: [opened, reopened, synchronize]
paths:
- '**.vue'
- '**.js'
jobs:
cleanup-runs:
runs-on: ubuntu-latest
steps:
- uses: rokroskar/workflow-run-cleanup-action@v0.2.2
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
test-with-code-coverage-job:
name: code coverage job.
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 128
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ secrets.NODE_VERSION }}
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Dependencies
run: npm ci
- name: jest test
run: |
mkdir -p coverage
npm test -- --collect-coverage --verbose | \
tee | \
sed -E "s/"$'\E'"\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g" | \
grep "Coverage summary" -A4 | \
tail -n 4 > coverage/coverage-summary.log
status=("${PIPESTATUS[@]}")
if [ ${status[0]} -ne 0 ]; then
echo "jest faild."
exit 1;
fi
- 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 coverage/lcov-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 coverage/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} -- '**/*.js' '**/*.vue' | \
grep -v "__tests__/" | \
xargs -I{} echo "* [{}](https://${{ secrets.AWS_S3_WEB_HOST }}/coverage-report/$REPOSITORY_NAME/$PR_NUM/{}.html)" | \
tee -a coverage/coverage-report-urls
- name: Read coverage summary
id: coverage-summary
uses: juliangruber/read-file-action@v1.0.0
with:
path: coverage/coverage-summary.log
- name: Read coverage report urls
id: coverage-report-urls
uses: juliangruber/read-file-action@v1.0.0
with:
path: coverage/coverage-report-urls
- name: Covarage summary comment
uses: marocchino/sticky-pull-request-comment@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: coverage-summary
message: |
## Coverage summary
${{ steps.coverage-summary.outputs.content }}
## Coverage report
${{ steps.coverage-report-urls.outputs.content }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment