Publish Test Coverage to Codecov from Github Actions
Github Actions is one of ways to run Continues Integration. For Coverage report, we can use Codecov to publish the coverage result after running and generating test coverage.
For example, you have a Github Repository. You can open https://codecov.io/login and choose “Github”:
After you logged in, you can choose repository, or directly access https://codecov.io/gh/{your github user}/{your github repo}, for example, I use “samsonasik” as user, and “mezzio-authentication-with-authorization” as repository name:
https://codecov.io/gh/samsonasik/mezzio-authentication-with-authorization
On very first, we need to activate Webhook by open https://codecov.io/gh/gh/{your github user}/{your github repo}/settings, for example:
https://codecov.io/gh/samsonasik/mezzio-authentication-with-authorization/settings
Then, we click “Create new webhook” under Github Webhook:
After it done, we can copy “Repository Upload Token”:
by click “Copy” after then token, and back to Github, and save to Secrets section under Your Github Repository Settings with click “Add a new secret”, with eg: named: CODECOV_TOKEN, fill the value with your copied token, and click “Add secret” to save it to be like as follow:
The preparation is done. Now, time to add the github workflow, eg: “.github/workflows/ci-build.yml” at your repository, eg for php package/project and use phpunit, the workflow can be like the following:
name: "ci build" on: pull_request: push: branches: - "master" jobs: build: name: PHP ${{ matrix.php-versions }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: php-versions: ['7.2', '7.3', '7.4'] steps: - name: Setup PHP Action uses: shivammathur/setup-php@1.8.2 with: extensions: intl php-version: "${{ matrix.php-versions }}" coverage: pcov - name: Checkout uses: actions/checkout@v2 - name: "Validate composer.json and composer.lock" run: "composer validate" - name: "Install dependencies" run: "composer install --prefer-dist --no-progress --no-suggest && composer development-enable" - name: "Run test suite" run: "vendor/bin/phpunit --coverage-clover=coverage.xml" - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 with: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.xml flags: tests name: codecov-umbrella yml: ./codecov.yml fail_ci_if_error: true
That’s it. To trigger the Continues Integration, we can push a commit to the repository.
When its succeed (ci build green), we can then display coverage badge, eg at README.md like the following:
[](https://codecov.io/gh/{your github user}/{your github repo})
that will show:
leave a comment