ci: add sonar code coverage #124
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
name: maven-build | |
on: | |
push: | |
branches: ['**/**'] | |
pull_request: | |
branches: [main] | |
types: [opened, synchronize, reopened, ready_for_review] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
env: | |
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_USERNAME: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_USERNAME }} | |
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_TOKEN: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_TOKEN }} | |
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PASSPHRASE: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PASSPHRASE }} | |
GITHUB_TOKEN: ${{ github.token }} | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
steps: | |
- name: 📄 Checkout the repository | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | |
with: | |
fetch-depth: 0 | |
- name: Set up JDK and Maven | |
uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b # v4 | |
with: | |
distribution: adopt | |
java-version: 17 | |
gpg-private-key: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PRIVATE_KEY }} | |
- name: 📝 Get the project version | |
id: project_version | |
run: echo "project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT | |
- name: 📝 Store cache key | |
id: cache_key | |
run: echo "cache_key=${{ runner.os }}-mvn-${{ hashFiles('**/pom.xml') }}-${{ github.sha }}" >> $GITHUB_OUTPUT | |
- name: 💾 Prepare cache using cache key | |
id: prepare-cache | |
uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a # v4 | |
with: | |
path: | | |
/home/runner/.m2 | |
/home/runner/work | |
key: ${{ steps.cache_key.outputs.cache_key }} | |
- name: 🔘 Generate settings.xml for Maven | |
uses: whelk-io/maven-settings-xml-action@9dc09b23833fa9aa7f27b63db287951856f3433d # v22 | |
with: | |
servers: > | |
[ | |
{ | |
"id": "github", | |
"username": "${env.GITHUB_ACTOR}", | |
"password": "${env.GITHUB_TOKEN}" | |
}, | |
{ | |
"id": "ossrh", | |
"username": "${env.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_USERNAME}", | |
"password": "${env.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_TOKEN}" | |
}, | |
{ | |
"id": "gpg.passphrase", | |
"passphrase": "${env.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PASSPHRASE}" | |
} | |
] | |
profiles: > | |
[ | |
{ | |
"id": "deploy-github-packages", | |
"properties": | |
{ | |
"altDeploymentRepository": "github::default::https://maven.pkg.github.com/${{ github.repository }}" | |
} | |
} | |
] | |
- name: 🔘 Print settings.xml | |
run: cat /home/runner/.m2/settings.xml | |
- name: 📦 Build with Maven for Pushes | |
if: github.event_name == 'push' | |
run: mvn --batch-mode clean package sonar:sonar -Dsonar.branch.name=${{ github.head_ref }} | |
- name: 📦 Build with Maven for PRs | |
if: github.event_name == 'pull_request' | |
run: mvn --batch-mode clean package sonar:sonar -Dsonar.pullrequest.base=${{ github.base_ref }} -Dsonar.pullrequest.branch=${{ github.head_ref }} -Dsonar.pullrequest.key=${{ github.event.pull_request.number }} | |
outputs: | |
project_version: ${{ steps.project_version.outputs.project_version }} | |
cache_key: ${{ steps.cache_key.outputs.cache_key }} | |
# Deploy release to Maven Central | |
deploy-maven-central: | |
needs: build | |
runs-on: ubuntu-latest | |
if: ${{ !endsWith(needs.build.outputs.project_version, '-SNAPSHOT') && github.ref == 'refs/heads/main' }} | |
env: | |
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_USERNAME: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_USERNAME }} | |
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_TOKEN: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_TOKEN }} | |
COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PASSPHRASE: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PASSPHRASE }} | |
steps: | |
- name: 🧱 Set up JDK and Maven | |
uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b # v4 | |
with: | |
distribution: adopt | |
java-version: 17 | |
gpg-private-key: ${{ secrets.COM_SONATYPE_CENTRAL_POLARION_OPENSOURCE_GPG_PRIVATE_KEY }} | |
- name: 💾 Restore cache using cache key | |
id: restore-cache | |
uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a # v4 | |
with: | |
path: | | |
/home/runner/.m2 | |
/home/runner/work | |
key: ${{ needs.build.outputs.cache_key }} | |
- name: 📦 Deploy artifacts to Maven Central | |
run: mvn --batch-mode -Dmaven.test.skip=true deploy -P gpg-sign -P nexus-staging | |
# Deploy release to GitHub Packages | |
deploy-github-packages: | |
needs: build | |
runs-on: ubuntu-latest | |
if: ${{ !endsWith(needs.build.outputs.project_version, '-SNAPSHOT') && github.ref == 'refs/heads/main' }} | |
permissions: | |
contents: write | |
packages: write | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
steps: | |
- name: 🧱 Set up JDK and Maven | |
uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b # v4 | |
with: | |
distribution: adopt | |
java-version: 17 | |
- name: 💾 Restore cache using cache key | |
id: restore-cache | |
uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a # v4 | |
with: | |
path: | | |
/home/runner/.m2 | |
/home/runner/work | |
key: ${{ needs.build.outputs.cache_key }} | |
- name: 📦 Deploy artifacts to GitHub Packages | |
run: mvn --batch-mode -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dmaven.source.skip=true deploy -P deploy-github-packages | |
- name: 📦 Upload assets to GitHub Release | |
run: |- | |
gh release upload v${{ needs.build.outputs.project_version }} target/*-${{ needs.build.outputs.project_version }}.jar |