Skip to content

Commit

Permalink
Merge pull request #101 from gradle/ot/feature/publishing-automated
Browse files Browse the repository at this point in the history
Update publishing to automatically perform version changes
  • Loading branch information
octylFractal authored Aug 1, 2024
2 parents d910a19 + 66080bb commit d559a36
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
27 changes: 24 additions & 3 deletions .teamcity/src/main/kotlin/projects/unified/plugin/PluginProject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package projects.unified.plugin
import common.addGradleParam
import common.configuredGradle
import jetbrains.buildServer.configs.kotlin.*
import jetbrains.buildServer.configs.kotlin.buildSteps.script

class PluginProject(private val buildAndTest: BuildType) : Project({
id("Plugin")
Expand All @@ -12,7 +13,9 @@ class PluginProject(private val buildAndTest: BuildType) : Project({
buildType {
id("Deploy")
name = "Deploy"
description = "Deploy the Declarative Gradle unified prototype plugin"
description = "Deploy the Declarative Gradle unified prototype plugin. " +
"Uses current version minus -SNAPSHOT as the release version. " +
"The next development version will be the current version with an incremented patch version."
type = BuildTypeSettings.Type.DEPLOYMENT

vcs {
Expand All @@ -34,6 +37,10 @@ class PluginProject(private val buildAndTest: BuildType) : Project({

requirements {
contains("teamcity.agent.jvm.os.name", "Linux")

doesNotContain("teamcity.agent.name", "ec2")
// US region agents have name "EC2-XXX"
doesNotContain("teamcity.agent.name", "EC2")
}

dependencies {
Expand All @@ -45,8 +52,8 @@ class PluginProject(private val buildAndTest: BuildType) : Project({

steps {
step(configuredGradle {
name = "Check"
tasks = "check"
name = "Change to Release Version"
tasks = ":unified-plugin:changeSnapshotToReleaseVersion"
workingDir = "unified-prototype"
})
step(configuredGradle {
Expand All @@ -55,6 +62,20 @@ class PluginProject(private val buildAndTest: BuildType) : Project({
addGradleParam("-Dgradle.publish.skip.namespace.check=true")
workingDir = "unified-prototype"
})
step(configuredGradle {
name = "Change to next Snapshot Version"
tasks = ":unified-plugin:changeReleaseToNextSnapshotVersion"
workingDir = "unified-prototype"
})
script {
name = "Push version commits"
scriptContent = """
set -e
git config credential.helper 'store --file=.git/credentials'
echo 'https://bot-gradle:%github.bot-gradle.declarative-gradle.token%@github.com" > .git/credentials
git push origin main --tags
""".trimIndent()
}
}
}
})
9 changes: 9 additions & 0 deletions unified-prototype/unified-plugin/build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ plugins {
dependencies {
implementation("com.gradle.publish:plugin-publish-plugin:1.2.1")
}

gradlePlugin {
plugins {
create("build-logic.build-update-utils") {
id = "build-logic.build-update-utils"
implementationClass = "buildlogic.BuildUpdateUtilsPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package buildlogic

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.process.ExecOperations
import javax.inject.Inject

open class BuildUpdateUtilsPlugin @Inject constructor(
private val execOperations: ExecOperations
) : Plugin<Project> {

companion object {
private const val CI_UPDATE_TASKS = "CI Update Tasks"
}

override fun apply(target: Project) {
target.run {
val versionFile = rootProject.file("version.txt")

fun getVersionFromFile() = versionFile.readText().trim()

fun writeNewVersion(newVersion: String) = versionFile.writeText(newVersion)

tasks.register("changeSnapshotToReleaseVersion") {
description = "Change the version of the plugin to a release version"
group = CI_UPDATE_TASKS
doLast {
val version = getVersionFromFile()
val newVersion = version.removeSuffix("-SNAPSHOT")
if (version == newVersion) {
throw IllegalStateException("Version is not a snapshot version: $version")
}
writeNewVersion(newVersion)
execOperations.exec {
commandLine("git", "commit", "-m", "Release version $newVersion", versionFile.absolutePath)
}
execOperations.exec {
commandLine("git", "tag", "v${newVersion}", "-m", "Release version $newVersion")
}
}
}

tasks.register("changeReleaseToNextSnapshotVersion") {
description = "Change the version of the plugin to a snapshot version"
group = CI_UPDATE_TASKS
doLast {
val version = getVersionFromFile()
if (!version.matches(Regex("\\d+\\.\\d+\\.\\d+"))) {
throw IllegalStateException("Version is not a release version: $version")
}
val versionParts = version.split(".")
val newVersion = "${versionParts[0]}.${versionParts[1]}.${versionParts[2].toInt() + 1}-SNAPSHOT"
writeNewVersion(newVersion)
execOperations.exec {
commandLine(
"git",
"commit",
"-m",
"Switch to next snapshot version $newVersion",
versionFile.absolutePath
)
}
}
}
}
}
}
5 changes: 4 additions & 1 deletion unified-prototype/unified-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
plugins {
kotlin("jvm").version(libs.versions.kotlin).apply(false)
id("build-logic.build-update-utils")
}

version = file("version.txt").readText().trim()

subprojects {
group = "org.gradle.experimental"
version = "0.1.8-SNAPSHOT"
version = parent!!.version
}

val publishAllPlugins = tasks.register("publishAllPlugins") {
Expand Down
1 change: 1 addition & 0 deletions unified-prototype/unified-plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ include("plugin-swift")
include("plugin-cpp")
include("plugin-common")
include("internal-testing-utils")
include("build-update-utils")

rootProject.name = "unified-plugin"
1 change: 1 addition & 0 deletions unified-prototype/unified-plugin/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.8-SNAPSHOT

0 comments on commit d559a36

Please sign in to comment.