Skip to content

Commit fd0c1d1

Browse files
committed
Update GHA to latest actions steps
Simplify setup, reduce duplication
1 parent 43b17cb commit fd0c1d1

File tree

1 file changed

+68
-28
lines changed

1 file changed

+68
-28
lines changed

.github/workflows/maven.yml

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,94 @@ on:
99

1010
jobs:
1111
build:
12-
12+
name: Maven Build
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: Git Clone
16-
uses: actions/checkout@v4
16+
uses: actions/checkout@v6
1717
- name: Set up JDK 21
18-
uses: actions/setup-java@v4
18+
uses: actions/setup-java@v5
1919
with:
2020
distribution: 'temurin'
2121
java-version: 21
2222
# generate settings.xml with the correct values
2323
server-id: sonatype-central-portal # Value of the distributionManagement/repository/id field of the pom.xml
24-
server-username: MAVEN_USERNAME # env variable for username in deploy
25-
server-password: MAVEN_PASSWORD # env variable for token in deploy
24+
server-username: MAVEN_CENTRAL_PORTAL_USERNAME # env variable for username in deploy
25+
server-password: MAVEN_CENTRAL_PORTAL_PASSWORD # env variable for token in deploy
2626
- name: Adjust Git Config
2727
run: |
2828
git config --global user.email "action@github.com"
2929
git config --global user.name "GitHub Action"
30-
- name: Build and Analyse Reactor with Maven
31-
if: github.ref != 'refs/heads/master'
32-
run: ./mvnw -B clean install org.sonarsource.scanner.maven:sonar-maven-plugin:5.2.0.4988:sonar -Dsonar.projectKey=Netcentric_aem-classification -Dsonar.organization=netcentric -Dsonar.host.url=https://sonarcloud.io -Dsonar.scanner.skipJreProvisioning=true -Pcoverage-report
33-
env:
34-
# SonarCloud access token should be generated from https://sonarcloud.io/account/security/
35-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
36-
# Needed to get some information about the pull request, if any
37-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38-
- name: Build Site for Maven Plugin
39-
if: github.ref != 'refs/heads/master'
40-
run: ./mvnw -B clean site --file aem-classification-maven-plugin/pom.xml
41-
- name: Build, Analyse and Deploy Reactor with Maven
42-
if: github.ref == 'refs/heads/master'
43-
run: ./mvnw -B clean deploy org.sonarsource.scanner.maven:sonar-maven-plugin:5.2.0.4988:sonar -Dsonar.projectKey=Netcentric_aem-classification -Dsonar.organization=netcentric -Dsonar.host.url=https://sonarcloud.io -Dsonar.scanner.skipJreProvisioning=true -Pcoverage-report
44-
env:
45-
MAVEN_USERNAME: ${{ secrets.SONATYPE_CENTRAL_TOKEN_USER }}
46-
MAVEN_PASSWORD: ${{ secrets.SONATYPE_CENTRAL_TOKEN_PASSWORD }}
47-
# SonarCloud access token should be generated from https://sonarcloud.io/account/security/
48-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
49-
# Needed to get some information about the pull request, if any
50-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
# sets environment variables to be used in subsequent steps: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
31+
- name: Set environment variables
32+
shell: bash
33+
run: |
34+
if [ "${{github.ref}}" = "refs/heads/master" ] && [ "${{github.event_name}}" = "push" ] && [ "${{github.repository_owner}}" = "Netcentric" ]; then
35+
echo 'Running on main branch of the canonical repo'
36+
echo "MVN_ADDITIONAL_OPTS=-DdeployAtEnd=true" >> $GITHUB_ENV
37+
echo "MVN_GOAL=deploy" >> $GITHUB_ENV
38+
echo "MAVEN_CENTRAL_PORTAL_USERNAME=${{ secrets.SONATYPE_CENTRAL_TOKEN_USER }}" >> $GITHUB_ENV
39+
echo "MAVEN_CENTRAL_PORTAL_PASSWORD=${{ secrets.SONATYPE_CENTRAL_TOKEN_PASSWORD }}" >> $GITHUB_ENV
40+
else
41+
echo 'Running outside main branch/canonical repo'
42+
echo "MVN_ADDITIONAL_OPTS=" >> $GITHUB_ENV
43+
echo "MVN_GOAL=verify" >> $GITHUB_ENV
44+
fi
45+
- name: Build
46+
run: ./mvnw -B ${{ env.MVN_GOAL }} ${{ env.MVN_ADDITIONAL_OPTS }} -Pcoverage-report
47+
- name: Upload build result for subsequent SonarQube job
48+
# not supported on forks, https://portal.productboard.com/sonarsource/1-sonarqube-cloud/c/50-sonarcloud-analyzes-external-pull-request
49+
if: github.repository == 'Netcentric/aem-classification'
50+
uses: actions/upload-artifact@v4
51+
with:
52+
name: compiled-classes-and-coverage
53+
# compare with https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/languages/java/#java-analysis-and-bytecode
54+
path: |
55+
**/target/**/*.class
56+
**/target/site/jacoco*/*.xml
5157
- name: Build Site for Maven Plugin
5258
if: github.ref == 'refs/heads/master'
53-
run: ./mvnw -B clean site --file aem-classification-maven-plugin/pom.xml
59+
run: ./mvnw -B site --file aem-classification-maven-plugin/pom.xml
5460
- name: Upload Site for Maven Plugin
5561
if: github.ref == 'refs/heads/master'
56-
uses: actions/upload-pages-artifact@v3
62+
uses: actions/upload-pages-artifact@v4
5763
with:
5864
path: aem-classification-maven-plugin/target/site/
65+
# execute analysis in a separate job for better visualization and usage of matrix builds
66+
# https://docs.sonarsource.com/sonarcloud/advanced-setup/ci-based-analysis/sonarscanner-for-maven/#invoking-the-goal
67+
sonarqube:
68+
name: SonarQube Analysis
69+
runs-on: ubuntu-latest
70+
needs: build
71+
# not supported on forks, https://portal.productboard.com/sonarsource/1-sonarqube-cloud/c/50-sonarcloud-analyzes-external-pull-request
72+
if: github.repository == 'Netcentric/aem-classification'
73+
steps:
74+
- uses: actions/checkout@v4
75+
with:
76+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
77+
- name: Set up JDK 21
78+
uses: actions/setup-java@v5
79+
with:
80+
java-version: 21
81+
distribution: temurin
82+
cache: maven
83+
- name: Download compiled classes
84+
uses: actions/download-artifact@v4
85+
with:
86+
name: compiled-classes-and-coverage
87+
- name: Cache SonarQube packages
88+
uses: actions/cache@v4
89+
with:
90+
path: ~/.sonar/cache
91+
key: ${{ runner.os }}-sonar
92+
restore-keys: ${{ runner.os }}-sonar
93+
- name: Analyze with SonarQube
94+
env:
95+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
96+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
97+
run: ./mvnw -B org.sonarsource.scanner.maven:sonar-maven-plugin:5.5.0.6356:sonar -Dsonar.projectKey=Netcentric_aem-classification -Dsonar.organization=netcentric -Dsonar.host.url=https://sonarcloud.io -Dsonar.scanner.skipJreProvisioning=true
5998
deploy:
99+
name: Deploy to GH Pages
60100
if: github.ref == 'refs/heads/master'
61101
# Add a dependency to the build job
62102
needs: build

0 commit comments

Comments
 (0)