diff --git a/.github/workflows/sonatype.yml b/.github/workflows/sonatype.yml new file mode 100644 index 0000000..09a5a16 --- /dev/null +++ b/.github/workflows/sonatype.yml @@ -0,0 +1,27 @@ +name: Publish to Sonatype + +on: + release: + types: [released] + +jobs: + publish-release: + runs-on: ubuntu-latest + steps: + - name: Setup Java + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '17' + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Publish artifact to Sonatype + env: + SONATYPE_USER: ${{ secrets.SONATYPE_USER }} + SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} + GPG_KEY_ID: ${{ secrets.GPG_KEY_ID }} + GPG_SECRET_KEY: ${{ secrets.GPG_SECRET_KEY }} + GPG_PASSWORD: ${{ secrets.GPG_PASSWORD }} + run: ./gradlew -Psonatype -Pversion=${{ github.event.release.tag_name }} clean build -x test publish diff --git a/core/build.gradle.kts b/core/build.gradle.kts index dc9c4ef..21e0322 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -3,6 +3,7 @@ plugins { id("org.jetbrains.kotlin.android") id("org.jetbrains.kotlin.plugin.serialization") id("maven-publish") + id("signing") } val libVersionName by extra(version as String) @@ -21,6 +22,8 @@ val licenseUrl by extra("https://github.com/hotwired/hotwire-native-android/blob val developerId by extra("basecamp") val developerEmail by extra("androidteam@basecamp.com") +val isSonatypeRelease by extra(project.hasProperty("sonatype")) + android { namespace = "dev.hotwire.core" compileSdk = 34 @@ -112,9 +115,33 @@ dependencies { testImplementation("junit:junit:4.13.2") } +tasks { + // Only sign Sonatype release artifacts + withType().configureEach { + onlyIf { isSonatypeRelease } + } +} + +// Sign Sonatype published release artifacts +if (isSonatypeRelease) { + signing { + val keyId = System.getenv("GPG_KEY_ID") + val secretKey = System.getenv("GPG_SECRET_KEY") + val password = System.getenv("GPG_PASSWORD") + + useInMemoryPgpKeys(keyId, secretKey, password) + + setRequired({ gradle.taskGraph.hasTask("publish") }) + sign(publishing.publications) + } +} + // Publish to GitHub Packages via: // ./gradlew -Pversion= clean build publish // https://github.com/orgs/hotwired/packages?repo_name=hotwire-native-android +// Publish to Maven Central via: +// ./gradlew -Psonatype -Pversion= clean build publish +// https://search.maven.org/artifact/dev.hotwire/core publishing { publications { register("release") { @@ -152,13 +179,24 @@ publishing { } } repositories { - maven { - name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/hotwired/hotwire-native-android") + if (isSonatypeRelease) { + maven { + url = uri("https://s01.oss.sonatype.org/content/repositories/releases/") - credentials { - username = System.getenv("GITHUB_ACTOR") - password = System.getenv("GITHUB_TOKEN") + credentials { + username = System.getenv("SONATYPE_USER") + password = System.getenv("SONATYPE_PASSWORD") + } + } + } else { + maven { + name = "GitHubPackages" + url = uri("https://maven.pkg.github.com/hotwired/hotwire-native-android") + + credentials { + username = System.getenv("GITHUB_ACTOR") + password = System.getenv("GITHUB_TOKEN") + } } } } diff --git a/navigation-fragments/build.gradle.kts b/navigation-fragments/build.gradle.kts index 26d2a46..307d4ed 100644 --- a/navigation-fragments/build.gradle.kts +++ b/navigation-fragments/build.gradle.kts @@ -2,6 +2,7 @@ plugins { id("com.android.library") id("org.jetbrains.kotlin.android") id("maven-publish") + id("signing") } val libVersionName by extra(version as String) @@ -20,6 +21,8 @@ val licenseUrl by extra("https://github.com/hotwired/hotwire-native-android/blob val developerId by extra("basecamp") val developerEmail by extra("androidteam@basecamp.com") +val isSonatypeRelease by extra(project.hasProperty("sonatype")) + android { namespace = "dev.hotwire.navigation" compileSdk = 34 @@ -104,9 +107,33 @@ dependencies { testImplementation("junit:junit:4.13.2") } +tasks { + // Only sign Sonatype release artifacts + withType().configureEach { + onlyIf { isSonatypeRelease } + } +} + +// Sign Sonatype published release artifacts +if (isSonatypeRelease) { + signing { + val keyId = System.getenv("GPG_KEY_ID") + val secretKey = System.getenv("GPG_SECRET_KEY") + val password = System.getenv("GPG_PASSWORD") + + useInMemoryPgpKeys(keyId, secretKey, password) + + setRequired({ gradle.taskGraph.hasTask("publish") }) + sign(publishing.publications) + } +} + // Publish to GitHub Packages via: // ./gradlew -Pversion= clean build publish // https://github.com/orgs/hotwired/packages?repo_name=hotwire-native-android +// Publish to Maven Central via: +// ./gradlew -Psonatype -Pversion= clean build publish +// https://search.maven.org/artifact/dev.hotwire/navigation-fragments publishing { publications { register("release") { @@ -144,13 +171,24 @@ publishing { } } repositories { - maven { - name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/hotwired/hotwire-native-android") + if (isSonatypeRelease) { + maven { + url = uri("https://s01.oss.sonatype.org/content/repositories/releases/") - credentials { - username = System.getenv("GITHUB_ACTOR") - password = System.getenv("GITHUB_TOKEN") + credentials { + username = System.getenv("SONATYPE_USER") + password = System.getenv("SONATYPE_PASSWORD") + } + } + } else { + maven { + name = "GitHubPackages" + url = uri("https://maven.pkg.github.com/hotwired/hotwire-native-android") + + credentials { + username = System.getenv("GITHUB_ACTOR") + password = System.getenv("GITHUB_TOKEN") + } } } }