Skip to content

Commit

Permalink
[publish] publishing signed artifacts with version computed from curr…
Browse files Browse the repository at this point in the history
…ent branch/tag
  • Loading branch information
FrogDevelopper committed May 16, 2024
1 parent bfc961e commit 1b754c0
Show file tree
Hide file tree
Showing 26 changed files with 408 additions and 52 deletions.
19 changes: 15 additions & 4 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Java CI with Gradle

env:
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CI: true

on:
Expand All @@ -17,9 +15,12 @@ jobs:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v4
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
Expand All @@ -40,4 +41,14 @@ jobs:
path: '**/build/test-results/**/*.xml'

- name: Containerize
run: ./gradlew jib -Djib.to.auth.username=${{ secrets.DOCKER_USR }} -Djib.to.auth.password=${{ secrets.DOCKER_PSW }} -Djib.console='plain'
run: ./gradlew jib -Djib.console='plain'
env:
DOCKER_USR: ${{ secrets.DOCKER_USR }}
DOCKER_PSW: ${{ secrets.DOCKER_PSW }}

- name: Publish package
run: ./gradlew publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GNUPG_PRIVATE_KEY }}
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.GNUPG_PASSPHRASE }}
59 changes: 23 additions & 36 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
name: Release
name: Publish package to the Maven Central Repository

env:
CI: true

on:
workflow_dispatch:
inputs:
version:
description: 'Version'
required: true
release:
types: [ created ]

jobs:
tagging:
name: Release of [${{ github.event.inputs.version }}]
publish_release:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: release/${{ github.event.inputs.version }}
fetch-depth: 0

- name: Config Git
run: |
git config user.name ${{ github.actor }}
git config user.email "${{ github.actor }}@users.noreply.github.com"

- name: Merge on master
run: |
git checkout master
git merge --no-ff release/${{ github.event.inputs.version }}
git push
- name: Tag on master
run: |
git tag --annotate --message 'Release ${{ github.event.inputs.version }}' ${{ github.event.inputs.version }}
git push origin ${{ github.event.inputs.version }}
- name: Merge on develop
run: |
git checkout develop
git merge --no-ff release/${{ github.event.inputs.version }}
git push
- name: Delete released branch
run: git push origin :release/${{ github.event.inputs.version }}
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3.1.0

- name: Publish package
run: ./gradlew publish
env:
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
OSSRH_TOKEN: ${{ secrets.OSSRH_TOKEN }}
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.GNUPG_PRIVATE_KEY }}
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.GNUPG_PASSPHRASE }}
47 changes: 45 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
plugins {
java
id("org.ajoberstar.grgit") version "5.2.2"
}

version = "1.0.0-SNAPSHOT"
group = "com.frogdevelopment"
group = "com.frogdevelopment.consul-populate"

repositories {
mavenCentral()
Expand All @@ -13,3 +13,46 @@ java {
sourceCompatibility = JavaVersion.toVersion("21")
targetCompatibility = JavaVersion.toVersion("21")
}

afterEvaluate {
computeProjectVersion()
}

fun computeProjectVersion() {
val branchName = grgit.branch.current().name

println("Current branch: $branchName")

val computedVersion = when (branchName) {
"HEAD" -> handleHead()
"develop" -> handleDevelop()
else -> handleBranch(branchName)
}

allprojects {
group = rootProject.group
version = computedVersion
}

println("Computed version: $version")
}

fun handleHead(): String {
val githubRefName = System.getenv("GITHUB_REF_NAME")
if (githubRefName == null || githubRefName.isEmpty()) {
throw GradleException("One does not simply build from HEAD. Checkout to matching local branch !!")
}
return githubRefName
}

fun handleDevelop(): String {
return "develop-SNAPSHOT"
}

fun handleBranch(branchName: String): String {
val matchBranchResult = """^(?<type>\w+)/(?<details>.+)?$""".toRegex().find(branchName)
val branchType = matchBranchResult!!.groups["type"]?.value!!
val branchDetails = matchBranchResult.groups["details"]?.value!!

return "$branchType-$branchDetails-SNAPSHOT"
}
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`kotlin-dsl`
}

repositories {
gradlePluginPortal()
}
1 change: 1 addition & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "buildSrc"
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
plugins {
java
`maven-publish`
signing
}

java {
withJavadocJar()
withSourcesJar()
}

publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])

versionMapping {
usage("java-api") {
fromResolutionOf("runtimeClasspath")
}
usage("java-runtime") {
fromResolutionResult()
}
}

pom {
description = project.description
url = "https://github.com/FrogDevelopment/consul-populate/wiki"
inceptionYear = "2024"
issueManagement {
system = "GitHub"
url = "https://github.com/FrogDevelopment/consul-populate/issues"
}
developers {
developer {
id = "FrogDevelopper"
name = "Le Gall Benoît"
email = "legall.benoit@gmail.com"
url = "https://github.com/FrogDevelopper"
timezone = "Europe/Paris"
}
}
licenses {
license {
name = "The Apache License, Version 2.0"
url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
scm {
connection = "scm:git:git://github.com/FrogDevelopment/consul-populate.git"
developerConnection = "scm:git:ssh://github.com:FrogDevelopment/consul-populate.git"
url = "https://github.com/FrogDevelopment/consul-populate/tree/master"
}
distributionManagement {
downloadUrl = "https://github.com/FrogDevelopment/consul-populate/releases"
}
}
}
}

repositories {
maven {
name = "sonatype"
url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = System.getenv("OSSRH_USERNAME")
password = System.getenv("OSSRH_TOKEN")
}
}

maven {
name = "github"
url = uri("https://maven.pkg.github.com/FrogDevelopment/consul-populate")
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
val releaseVersion = """^\d+\.\d+\.\d+$""".toRegex().matches(version.toString())
signing {
if (releaseVersion) {
val signingKey: String? by project
val signingPassword: String? by project
useInMemoryPgpKeys(signingKey, signingPassword)
sign(publishing.publications["mavenJava"])
}
}

tasks {
matching { it is PublishToMavenRepository && it.repository.name == "sonatype" }.all {
onlyIf { releaseVersion }
}
matching { it is PublishToMavenRepository && it.repository.name == "github" }.all {
onlyIf { !releaseVersion }
}
}
33 changes: 31 additions & 2 deletions consul-populate-cli/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins {
id("io.micronaut.minimal.application") version "4.3.8"
id("com.frogdevelopment.publish-conventions")
alias(libs.plugins.jib)
alias(libs.plugins.shadow)
}

micronaut {
Expand Down Expand Up @@ -32,19 +34,40 @@ dependencies {
testImplementation(libs.testcontainers.consul)
testImplementation(libs.vertx.consul)
testImplementation(libs.systemlambda)

}

application {
mainClass.set("com.frogdevelopment.consul.populate.ConsulPopulateCommand")
}

publishing {
publications {
named<MavenPublication>("mavenJava") {
shadow.component(this)

pom {
name = "Consul Populate - CLI"
description = "CLI executable for Consul Populate"
}
}
}
}

tasks {
jar {
enabled = false
}
shadowJar {
// minimize() waiting https://github.com/johnrengelman/shadow/pull/876
archiveClassifier.set("")
}


test {
// https://github.com/stefanbirkner/system-lambda/issues/27
systemProperty("java.security.manager", "allow")

// #https://junit-pioneer.org/docs/environment-variables/#warnings-for-reflective-access
// https://junit-pioneer.org/docs/environment-variables/#warnings-for-reflective-access
// jvmArgs = listOf("--add-opens java.base/java.util=ALL-UNNAMED","--add-opens java.base/java.lang=ALL-UNNAMED")

// environment.put("CONSUL_HOST", "qwert")
Expand Down Expand Up @@ -75,6 +98,12 @@ tasks {

to {
image = "frogdevelopment/consul-populate:${rootProject.version}"
if (System.getenv("CI") == "true") {
auth {
username = System.getenv("DOCKER_USR")
password = System.getenv("DOCKER_PSW")
}
}
}

container {
Expand Down
13 changes: 12 additions & 1 deletion consul-populate-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("io.micronaut.minimal.library") version "4.3.8"
`maven-publish`
id("com.frogdevelopment.publish-conventions")
}

micronaut {
Expand Down Expand Up @@ -34,3 +34,14 @@ dependencies {
testImplementation(libs.testcontainers.junit)
testImplementation(libs.testcontainers.consul)
}

publishing {
publications {
named<MavenPublication>("mavenJava") {
pom {
name = "Consul Populate - Core"
description = "Core library for Consul Populate"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@

import io.micronaut.core.annotation.NonNull;

/**
* Interface to extend with the specific import type implementation
*
* @author Le Gall Benoît
* @since 1.0.0
*/
public interface DataImporter {

/**
* Do the import from the data type
*
* @return Map of key-value to be imported into Consul's KV
*/
@NonNull
Map<String, String> execute();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package com.frogdevelopment.consul.populate;

/**
* Entry point to run the data import
*
* @author Le Gall Benoît
* @since 1.0.0
*/
public interface PopulateService {

/**
* Main method that is going to do the defined import into Consul's KV
*/
void populate();
}
Loading

0 comments on commit 1b754c0

Please sign in to comment.