Skip to content

Commit

Permalink
ci: add sonar code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ariwk committed Oct 29, 2024
1 parent cb5e470 commit fe3b704
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
60 changes: 35 additions & 25 deletions .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: maven-build
on:
push:
branches: ['**/**']
pull_request:
branches: [main]
types: [opened, synchronize, reopened, ready_for_review]
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -11,24 +14,33 @@ jobs:
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
- 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: Prepare Cache
- 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: ${{ runner.os }}-mvn-${{ hashFiles('**/pom.xml') }}
- name: Generate settings.xml
key: ${{ steps.cache_key.outputs.cache_key }}
- name: 🔘 Generate settings.xml for Maven
uses: whelk-io/maven-settings-xml-action@9dc09b23833fa9aa7f27b63db287951856f3433d # v22
with:
servers: >
Expand Down Expand Up @@ -58,21 +70,19 @@ jobs:
}
}
]
- name: Print settings.xml
- name: 🔘 Print settings.xml
run: cat /home/runner/.m2/settings.xml
- name: Build with Maven
run: mvn --batch-mode clean package
- name: Store 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_OUTPUT
- 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 to Maven Central
# Deploy release to Maven Central
deploy-maven-central:
needs: build
runs-on: ubuntu-latest
Expand All @@ -82,24 +92,24 @@ jobs:
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
- 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
- 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: Publish to Maven Central
- name: 📦 Deploy artifacts to Maven Central
run: mvn --batch-mode -Dmaven.test.skip=true deploy -P gpg-sign -P nexus-staging

# deploy to GitHub Packages
# Deploy release to GitHub Packages
deploy-github-packages:
needs: build
runs-on: ubuntu-latest
Expand All @@ -110,21 +120,21 @@ jobs:
env:
GITHUB_TOKEN: ${{ github.token }}
steps:
- name: Set up JDK and Maven
- name: 🧱 Set up JDK and Maven
uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b # v4
with:
distribution: adopt
java-version: 17
- name: Cache
id: cache
- 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: Publish to GitHub Packages
- 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
run: cd ${{github.workspace}} && gh release upload v${{ needs.build.outputs.project_version }} target/*-${{ needs.build.outputs.project_version }}.jar
shell: bash
- name: 📦 Upload assets to GitHub Release
run: |-
gh release upload v${{ needs.build.outputs.project_version }} target/*-${{ needs.build.outputs.project_version }}.jar
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5
with:
cache: pip # caching pip dependencies
python-version: 3.x
- run: pip install commitizen
- name: Check commit messages
run: cz check --rev-range origin/${GITHUB_BASE_REF}..
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ Before you submit your Pull Request (PR) consider the following guidelines:
is necessary because release notes are automatically generated from these messages.

```shell
git commit -a -S
git commit -a --gpg-sign
```
Note: The optional commit `-a` command line option will automatically "add" and "rm" edited files.

Note: The command line option `-S` generates a signed commit, which is required to make a contribution (See [Developer Certificate of Origin](./LICENSES/DCO.txt))
Note: The command line option `-S/--gpg-sign` generates a signed commit, which is required to make a contribution (See [Developer Certificate of Origin](./LICENSES/DCO.txt))

* Push your branch to GitHub:

Expand Down
20 changes: 17 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
<name>SBB Polarion Team</name>
<email>[email protected]</email>
<organization>SBB AG</organization>
<organizationUrl>http://www.sbb.ch</organizationUrl>
<organizationUrl>https://www.sbb.ch</organizationUrl>
</developer>
</developers>

<scm>
<connection>scm:git:git://github.com/SchweizerischeBundesbahnen/ch.sbb.export-package-generator.git</connection>
<developerConnection>scm:git:ssh://github.com/SchweizerischeBundesbahnen/ch.sbb.export-package-generator.git</developerConnection>
<url>http://github.com/SchweizerischeBundesbahnen/ch.sbb.export-package-generator/tree/main</url>
<url>https://github.com/SchweizerischeBundesbahnen/ch.sbb.export-package-generator/tree/main</url>
</scm>

<issueManagement>
Expand All @@ -56,7 +56,21 @@
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
<maven-surefire-plugin.version>3.5.1</maven-surefire-plugin.version>

<!-- Sonar and JaCoCo -->
<jacoco.version>0.8.12</jacoco.version>
<sonar.projectKey>SchweizerischeBundesbahnen_${project.artifactId}</sonar.projectKey>
<sonar.organization>schweizerischebundesbahnen</sonar.organization>
<sonar.projectName>${project.artifactId}</sonar.projectName>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.language>java</sonar.language>
<sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
<sonar.verbose>true</sonar.verbose>
<sonar.qualitygate.wait>true</sonar.qualitygate.wait>

<nexus-staging-maven-plugin.version>1.7.0</nexus-staging-maven-plugin.version>
<nexus-staging-maven-plugin.autoReleaseAfterClose>true</nexus-staging-maven-plugin.autoReleaseAfterClose>
</properties>

<dependencies>
Expand Down Expand Up @@ -109,7 +123,7 @@
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
<autoReleaseAfterClose>${nexus-staging-maven-plugin.autoReleaseAfterClose}</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
Expand Down

0 comments on commit fe3b704

Please sign in to comment.