Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build and workflow files #8

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: On pull request

on: [ pull_request ]

concurrency: on-pull-request

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'corretto'
cache: 'gradle'

- name: Build
run: ./gradlew devSnapshot printDevSnapshotReleaseNote
env:
GPG_SIGNING_KEY: ${{ secrets.GPG_SIGNING_KEY }}
GPG_SIGNING_PASSWORD: ${{ secrets.GPG_SIGNING_PASSWORD }}
SONATYPE_USERNAME: ${{ secrets.OSSRH_USERNAME }}
SONATYPE_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
GITHUB_HEAD_REF: ${{ github.head_ref }}
41 changes: 18 additions & 23 deletions .github/workflows/push-to-master.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
name: On push to master

on:
release:
types: [published]
push:
branches:
- master
paths-ignore:
- README.md

concurrency: on-push-to-master

jobs:
build:

runs-on: ubuntu-latest

steps:

- name: Checkout full repository history
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up JDK 11
uses: actions/setup-java@v2
- name: Setup JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'adopt'
server-id: github

- name: Enqueue concurrent workflows runs
uses: softprops/turnstyle@v1
with:
abort-after-seconds: 1800
poll-interval-seconds: 30
same-branch-only: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
distribution: 'corretto'
cache: 'gradle'

- name: Build, upload release version to Maven Central and create git release tag with Gradle
run: ./gradlew final closeAndReleaseRepository printFinalReleaseNode
- name: Build
run: ./gradlew final closeAndReleaseStagingRepository printFinalReleaseNote
env:
GPG_SIGNING_KEY: ${{ secrets.GPG_SIGNING_KEY }}
GPG_SIGNING_PASSWORD: ${{ secrets.GPG_SIGNING_PASSWORD }}
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
SONATYPE_USERNAME: ${{ secrets.OSSRH_USERNAME }}
SONATYPE_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
GITHUB_HEAD_REF: ${{ github.head_ref }}
GRGIT_USER: ${{ secrets.GRGIT_USER }}
GRGIT_PASS: ${{ secrets.GRGIT_PASS }}
155 changes: 105 additions & 50 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
java
signing
kotlin("jvm") version "1.6.10"
id("io.codearte.nexus-staging") version "0.22.0"
id("nebula.release") version "15.2.0"
id("maven-publish")
signing
`maven-publish`
id("com.netflix.nebula.release") version "17.2.2"
id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
}

repositories {
Expand All @@ -33,22 +33,15 @@ dependencies {
testImplementation("org.slf4j:slf4j-simple:1.7.36")
}

configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_11
}

java {
withSourcesJar()
withJavadocJar()
}

// Kotlin settings
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions.jvmTarget = "11"

val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions.jvmTarget = "11"
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
kotlinOptions.apiVersion = "1.7"
kotlinOptions.languageVersion = "1.7"
}

// Unit tests settings
tasks.withType<Test> {
enableAssertions = true
useJUnitPlatform()
Expand All @@ -59,6 +52,11 @@ tasks.withType<Test> {
}
}

java {
withSourcesJar()
withJavadocJar()
}


val settingsProvider = SettingsProvider()

Expand All @@ -70,33 +68,51 @@ tasks {
}
}

// Publish artifacts to Maven Central before pushing new git tag to repo
named("release").get().apply {
dependsOn(named("publish").get())
afterEvaluate {
// Publish artifacts to Maven Central before pushing new git tag to repo
named("release").get().apply {
dependsOn(named("publishToSonatype").get())
}

named("closeAndReleaseStagingRepository").get().apply {
dependsOn(named("final").get())
}
}
}


tasks.withType<Sign> {
doFirst {
settingsProvider.validateGPGSecrets()
}
dependsOn(tasks.getByName("build"))
}

tasks.withType<PublishToMavenRepository> {
doFirst {
settingsProvider.validateOssrhCredentials()
settingsProvider.validateSonatypeCredentials()
}
}

tasks.register("printFinalReleaseNote") {
doLast {
printFinalReleaseNote(
groupId = PublicationSettings.GROUP_ID,
artifactId = PublicationSettings.ARTIFACT_ID,
sanitizedVersion = project.sanitizeVersion()
)
}
dependsOn(tasks.getByName("final"))
}

tasks.register("printFinalReleaseNode") {
tasks.register("printDevSnapshotReleaseNote") {
doLast {
printFinalReleaseNode(
printDevSnapshotReleaseNote(
groupId = PublicationSettings.GROUP_ID,
artifactId = PublicationSettings.ARTIFACT_ID,
sanitizedVersion = project.sanitizeVersion()
)
}
dependsOn(tasks.getByName("devSnapshot"))
}

publishing {
Expand Down Expand Up @@ -139,33 +155,53 @@ publishing {
}
}
}
repositories {
maven {
credentials {
username = settingsProvider.ossrhUsername
password = settingsProvider.ossrhPassword
}
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
}
}
}

signing {
useInMemoryPgpKeys(settingsProvider.gpgSigningKey, settingsProvider.gpgSigningPassword)
sign(publishing.publications["mavenJava"])
}

nexusStaging {
packageGroup = PublicationSettings.STAGING_PACKAGE_GROUP
username = settingsProvider.ossrhUsername
password = settingsProvider.ossrhPassword
nexusPublishing {
repositories {
sonatype {
useStaging.set(!project.isSnapshotVersion())
packageGroup.set(PublicationSettings.GROUP_ID)
username.set(settingsProvider.sonatypeUsername)
password.set(settingsProvider.sonatypePassword)
// nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
// snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
}
}
}

// We want to change SNAPSHOT versions format from:
// <major>.<minor>.<patch>-dev.#+<branchname>.<hash> (local branch)
// <major>.<minor>.<patch>-dev.#+<hash> (github pull request)
// to:
// <major>.<minor>.<patch>-dev+<branchname>-SNAPSHOT
fun Project.sanitizeVersion(): String {
return version.toString()
val version = version.toString()
return if (project.isSnapshotVersion()) {
val githubHeadRef = settingsProvider.githubHeadRef
if (githubHeadRef != null) {
// github pull request
version
.replace(Regex("-dev\\.\\d+\\+[a-f0-9]+$"), "-dev+$githubHeadRef-SNAPSHOT")
} else {
// local branch
version
.replace(Regex("-dev\\.\\d+\\+"), "-dev+")
.replace(Regex("\\.[a-f0-9]+$"), "-SNAPSHOT")
}
} else {
version
}
}

fun printFinalReleaseNode(groupId: String, artifactId: String, sanitizedVersion: String) {
fun Project.isSnapshotVersion() = version.toString().contains("-dev.")

fun printFinalReleaseNote(groupId: String, artifactId: String, sanitizedVersion: String) {
println()
println("========================================================")
println()
Expand All @@ -187,6 +223,25 @@ fun printFinalReleaseNode(groupId: String, artifactId: String, sanitizedVersion:
println()
}

fun printDevSnapshotReleaseNote(groupId: String, artifactId: String, sanitizedVersion: String) {
println()
println("========================================================")
println()
println("New developer SNAPSHOT artifact version were published:")
println(" groupId: $groupId")
println(" artifactId: $artifactId")
println(" version: $sanitizedVersion")
println()
println("Discover on Maven Central:")
println(" https://oss.sonatype.org/content/repositories/snapshots/${groupId.replace('.', '/')}/$artifactId/")
println()
println("Edit or delete artifacts on OSS Nexus Repository Manager:")
println(" https://oss.sonatype.org/#nexus-search;gav~$groupId~~~~")
println()
println("========================================================")
println()
}

class SettingsProvider {

val gpgSigningKey: String?
Expand All @@ -195,11 +250,11 @@ class SettingsProvider {
val gpgSigningPassword: String?
get() = System.getenv(GPG_SIGNING_PASSWORD_PROPERTY)

val ossrhUsername: String?
get() = System.getenv(OSSRH_USERNAME_PROPERTY)
val sonatypeUsername: String?
get() = System.getenv(SONATYPE_USERNAME_PROPERTY)

val ossrhPassword: String?
get() = System.getenv(OSSRH_PASSWORD_PROPERTY)
val sonatypePassword: String?
get() = System.getenv(SONATYPE_PASSWORD_PROPERTY)

val githubHeadRef: String?
get() = System.getenv(GITHUB_HEAD_REF_PROPERTY)
Expand All @@ -209,16 +264,16 @@ class SettingsProvider {
lazyMessage = { "Both $GPG_SIGNING_KEY_PROPERTY and $GPG_SIGNING_PASSWORD_PROPERTY environment variables must not be empty" }
)

fun validateOssrhCredentials() = require(
value = !ossrhUsername.isNullOrBlank() && !ossrhPassword.isNullOrBlank(),
lazyMessage = { "Both $OSSRH_USERNAME_PROPERTY and $OSSRH_PASSWORD_PROPERTY environment variables must not be empty" }
fun validateSonatypeCredentials() = require(
value = !sonatypeUsername.isNullOrBlank() && !sonatypePassword.isNullOrBlank(),
lazyMessage = { "Both $SONATYPE_USERNAME_PROPERTY and $SONATYPE_PASSWORD_PROPERTY environment variables must not be empty" }
)

companion object {
private companion object {
private const val GPG_SIGNING_KEY_PROPERTY = "GPG_SIGNING_KEY"
private const val GPG_SIGNING_PASSWORD_PROPERTY = "GPG_SIGNING_PASSWORD"
private const val OSSRH_USERNAME_PROPERTY = "OSSRH_USERNAME"
private const val OSSRH_PASSWORD_PROPERTY = "OSSRH_PASSWORD"
private const val SONATYPE_USERNAME_PROPERTY = "SONATYPE_USERNAME"
private const val SONATYPE_PASSWORD_PROPERTY = "SONATYPE_PASSWORD"
private const val GITHUB_HEAD_REF_PROPERTY = "GITHUB_HEAD_REF"
}
}
Expand All @@ -234,7 +289,7 @@ object PublicationSettings {

const val DEVELOPER_ID = "vgv"
const val DEVELOPER_NAME = "Vasily Vasilkov"
const val DEVELOPER_EMAIL = "vgv@ecwid.com"
const val DEVELOPER_EMAIL = "vasily.vasilkov@lightspeedhq.com"

const val LICENSE_NAME = "The Apache License, Version 2.0"
const val LICENSE_URL = "https://www.apache.org/licenses/LICENSE-2.0.txt"
Expand Down