Skip to content

Commit a08ec94

Browse files
authored
Merge pull request #1017 from microsoftgraph/feat/automate-ossrh-releases
Automate OSSRH releases to maven central
2 parents 1b1546e + 0c17abd commit a08ec94

8 files changed

Lines changed: 139 additions & 136 deletions

File tree

.github/workflows/gradle-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
settings.gradle
5252
gradle.properties
5353
**/gradle/**
54-
Scripts/**
54+
scripts/**
5555
5656
build-java-8:
5757
runs-on: ubuntu-latest

.github/workflows/preview-and-release.yml

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ on:
1414
workflow_dispatch:
1515

1616
env:
17-
PREVIEW_TASK: publishSnapshotPublicationToSonatypeSnapshotRepository
18-
PUBLISH_TASK: publishMavenCentralReleasePublicationToSonatypeRepository
17+
PREVIEW_TASK: publishToSonatype
18+
PUBLISH_TASK: publishToSonatype closeAndReleaseSonatypeStagingRepository
19+
JAVA_VERSION: 21
20+
JAVA_DISTRIBUTION: 'temurin'
1921

2022
permissions:
2123
contents: write
@@ -26,6 +28,7 @@ jobs:
2628
environment:
2729
name: maven_central_snapshot
2830
runs-on: ubuntu-latest
31+
needs: validate-package-contents
2932
steps:
3033
- uses: actions/checkout@v4
3134
- name: Detect secrets
@@ -35,17 +38,17 @@ jobs:
3538
- name: Set up JDK
3639
uses: actions/setup-java@v4
3740
with:
38-
java-version: 21
39-
distribution: 'temurin'
41+
java-version: ${{ env.JAVA_VERSION }}
42+
distribution: ${{ env.JAVA_DISTRIBUTION }}
4043
cache: gradle
4144
- name: Download File
42-
run: .\Scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
45+
run: .\scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
4346
shell: pwsh
4447
env:
4548
ENCODED_VALUE: ${{ secrets.LOCAL_PROPERTIES }}
4649
OUTPUT_PATH: '.\local.properties'
4750
- name: Download File
48-
run: .\Scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
51+
run: .\scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
4952
shell: pwsh
5053
env:
5154
ENCODED_VALUE: ${{ secrets.SECRING_GPG }}
@@ -60,34 +63,35 @@ jobs:
6063
environment:
6164
name: maven_central_release
6265
runs-on: ubuntu-latest
66+
needs: validate-package-contents
6367
steps:
6468
- uses: actions/checkout@v4
6569
- name: Set up JDK
6670
uses: actions/setup-java@v4
6771
with:
68-
java-version: 21
69-
distribution: 'temurin'
72+
java-version: ${{ env.JAVA_VERSION }}
73+
distribution: ${{ env.JAVA_DISTRIBUTION }}
7074
cache: gradle
7175
- name: Detect secrets
7276
run: |
7377
pip install detect-secrets
7478
git ls-files -z | xargs -0 detect-secrets-hook --baseline .secrets.baseline
7579
- name: Download File
76-
run: .\Scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
80+
run: .\scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
7781
shell: pwsh
7882
env:
7983
ENCODED_VALUE: ${{ secrets.LOCAL_PROPERTIES }}
8084
OUTPUT_PATH: '.\local.properties'
8185
- name: Download File
82-
run: .\Scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
86+
run: .\scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
8387
shell: pwsh
8488
env:
8589
ENCODED_VALUE: ${{ secrets.SECRING_GPG }}
8690
OUTPUT_PATH: '.\secring.gpg'
8791
- name: Grant execute permission for gradlew
8892
run: chmod +x gradlew
8993
- name: Publish
90-
run: ./gradlew $PUBLISH_TASK
94+
run: ./gradlew $PUBLISH_TASK -PmavenCentralSnapshotArtifactSuffix=""
9195
- name: Upload Build Artifact
9296
uses: actions/upload-artifact@v4
9397
with:
@@ -102,11 +106,54 @@ jobs:
102106
settings.gradle
103107
gradle.properties
104108
**/gradle/**
105-
Scripts/**
109+
scripts/**
106110
- name: GitHub Release
107111
uses: softprops/action-gh-release@v2
108112
with:
109113
token: ${{ secrets.GITHUB_TOKEN }}
110114
fail_on_unmatched_files: true
111115
files: |
112116
build/**/*.jar
117+
118+
validate-package-contents:
119+
runs-on: ubuntu-latest
120+
environment: ${{ contains(github.ref, 'refs/tags/v') && 'maven_central_release' || 'maven_central_snapshot' }}
121+
defaults:
122+
run:
123+
working-directory: ./
124+
steps:
125+
- uses: actions/checkout@v4
126+
- name: Setup JDK
127+
uses: actions/setup-java@v4
128+
with:
129+
java-version: ${{ env.JAVA_VERSION }}
130+
distribution: ${{ env.JAVA_DISTRIBUTION}}
131+
cache: gradle
132+
- name: Download file
133+
run: .\scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
134+
shell: pwsh
135+
env:
136+
ENCODED_VALUE: ${{ secrets.LOCAL_PROPERTIES }}
137+
OUTPUT_PATH: 'local.properties'
138+
- name: Download file
139+
run: .\scripts\decodeAndWrite.ps1 -encodedValue $env:ENCODED_VALUE -outputPath $env:OUTPUT_PATH
140+
shell: pwsh
141+
env:
142+
ENCODED_VALUE: ${{ secrets.SECRING_GPG }}
143+
OUTPUT_PATH: '.\secring.gpg'
144+
- name: Publish to local Maven cache for validation
145+
run: ./gradlew --no-daemon publishToMavenLocal
146+
- name: Get current SNAPSHOT version
147+
shell: pwsh
148+
run: |
149+
$contents = Get-Content gradle.properties -Raw
150+
$major = $contents | Select-String -Pattern 'mavenMajorVersion\s+= ([0-9]+)' | ForEach-Object { $_.Matches.Groups[1].Value }
151+
$minor = $contents | Select-String -Pattern 'mavenMinorVersion\s+= ([0-9]+)' | ForEach-Object { $_.Matches.Groups[1].Value }
152+
$patch = $contents | Select-String -Pattern 'mavenPatchVersion\s+= ([0-9]+)' | ForEach-Object { $_.Matches.Groups[1].Value }
153+
$version = "$major.$minor.$patch-SNAPSHOT"
154+
echo "Current version is $version"
155+
echo "PACKAGE_VERSION=$version" >> $Env:GITHUB_ENV
156+
- name: Inspect contents of local Maven cache
157+
shell: pwsh
158+
run: |
159+
.\scripts\validatePackageContents.ps1 -ArtifactId msgraph-beta-sdk-java -Version $env:PACKAGE_VERSION

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ hs_err_pid*
3333
/target/
3434
local.properties
3535
.idea
36+
*.gpg

build.gradle

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ plugins {
1313
id 'eclipse'
1414
id 'maven-publish'
1515
id 'signing'
16+
id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
1617
}
1718

1819
java {
@@ -37,7 +38,6 @@ sourceSets {
3738
repositories {
3839
// You can declare any Maven/Ivy/file repository here.
3940
mavenCentral()
40-
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
4141
}
4242

4343
apply from: "gradle/dependencies.gradle"
@@ -54,10 +54,6 @@ def pomConfig = {
5454

5555
tasks.withType(Javadoc).all { enabled = false }
5656

57-
//Publishing tasks-
58-
//Maven Central Snapshot: publishSnapshotPublicationToMavenRepository
59-
//Maven Central Release: publishMavenCentralReleasePublicationToMaven2Repository
60-
6157
tasks.jar {
6258
manifest {
6359
attributes('Automatic-Module-Name': project.property('mavenGroupId'))
@@ -69,82 +65,42 @@ publishing {
6965

7066
publications {
7167

72-
maven(MavenPublication) {
73-
groupId project.property('mavenGroupId')
74-
artifactId project.property('mavenArtifactId')
75-
version "${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}${mavenArtifactSuffix}"
76-
from components.java
77-
pom.withXml {
78-
def root = asNode()
79-
root.appendNode('name', 'Microsoft Graph Beta SDK for Java')
80-
root.appendNode('url', 'https://github.com/microsoftgraph/msgraph-beta-sdk-java')
81-
root.children().last() + pomConfig
82-
def pomFile = file("${project.buildDir}/libs/microsoft-graph-beta.pom")
83-
writeTo(pomFile)
84-
}
85-
86-
}
87-
Snapshot(MavenPublication) {
88-
customizePom(pom)
89-
groupId project.property('mavenGroupId')
90-
artifactId project.property('mavenArtifactId')
91-
version "${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}${mavenCentralSnapshotArtifactSuffix}"
92-
from components.java
93-
pom.withXml {
94-
def pomFile = file("${project.buildDir}/generated-pom.xml")
95-
writeTo(pomFile)
96-
}
97-
}
98-
99-
mavenCentralRelease(MavenPublication) {
68+
maven(MavenPublication) {
10069
customizePom(pom)
10170
groupId project.property('mavenGroupId')
10271
artifactId project.property('mavenArtifactId')
103-
version "${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}"
72+
version getVersionName()
10473
from components.java
10574
pom.withXml {
10675
def pomFile = file("${project.buildDir}/generated-pom.xml")
10776
writeTo(pomFile)
10877
}
10978
}
11079
}
111-
repositories {
112-
maven {
113-
url = 'https://oss.sonatype.org/content/repositories/snapshots'
114-
name = 'sonatypeSnapshot'
115-
116-
credentials {
117-
if (project.rootProject.file('local.properties').exists()) {
118-
Properties properties = new Properties()
119-
properties.load(project.rootProject.file('local.properties').newDataInputStream())
120-
username = properties.getProperty('sonatypeUsername')
121-
password = properties.getProperty('sonatypePassword')
122-
}
123-
}
124-
}
125-
126-
maven {
127-
url = 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
128-
name = 'sonatype'
129-
130-
credentials {
131-
if (project.rootProject.file('local.properties').exists()) {
132-
Properties properties = new Properties()
133-
properties.load(project.rootProject.file('local.properties').newDataInputStream())
134-
username = properties.getProperty('sonatypeUsername')
135-
password = properties.getProperty('sonatypePassword')
136-
}
80+
}
81+
82+
nexusPublishing {
83+
repositories {
84+
sonatype {
85+
if (project.rootProject.file('local.properties').exists()) {
86+
Properties properties = new Properties()
87+
properties.load(project.rootProject.file('local.properties').newDataInputStream())
88+
username = properties.getProperty('sonatypeUsername')
89+
password = properties.getProperty('sonatypePassword')
13790
}
13891
}
13992
}
14093
}
14194

95+
group = project.property('mavenGroupId')
96+
version = "${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}${mavenCentralSnapshotArtifactSuffix}"
97+
14298
signing {
143-
sign publishing.publications.mavenCentralRelease
99+
sign publishing.publications.maven
144100
}
145101
tasks.withType(Sign)*.enabled = mavenCentralPublishingEnabled.toBoolean()
146102

147-
def fixAscNames = { name ->
103+
def fixAscNames = { name ->
148104
if(name.contains('pom')) {
149105
"${project.property('mavenArtifactId')}-${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}.pom.asc"
150106
} else {
@@ -163,7 +119,7 @@ def getVersionCode() {
163119
}
164120

165121
def getVersionName() {
166-
return "${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}${mavenArtifactSuffix}"
122+
return "${mavenMajorVersion}.${mavenMinorVersion}.${mavenPatchVersion}${mavenCentralSnapshotArtifactSuffix}"
167123
}
168124

169125
artifacts {
@@ -225,7 +181,7 @@ gradle.taskGraph.whenReady { taskGraph ->
225181
}
226182

227183
model {
228-
tasks.generatePomFileForMavenCentralReleasePublication {
184+
tasks.generatePomFileForMavenPublication {
229185
destination = file("${project.buildDir}/generated-pom.xml")
230186
}
231187
}

gradle.properties

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -39,62 +39,3 @@ mavenArtifactSuffix =
3939
#enable mavenCentralPublishingEnabled to publish to maven central
4040
mavenCentralSnapshotArtifactSuffix = -SNAPSHOT
4141
mavenCentralPublishingEnabled=true
42-
43-
44-
45-
46-
47-
48-
49-
50-
51-
52-
53-
54-
55-
56-
57-
58-
59-
60-
61-
62-
63-
64-
65-
66-
67-
68-
69-
70-
71-
72-
73-
74-
75-
76-
77-
78-
79-
80-
81-
82-
83-
84-
85-
86-
87-
88-
89-
90-
91-
92-
93-
94-
95-
96-
97-
98-
99-
100-

0 commit comments

Comments
 (0)