|
1 | 1 | #!/usr/bin/env kotlin |
2 | 2 | @file:Repository("https://repo1.maven.org/maven2/") |
3 | 3 | @file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.0.0") |
| 4 | +@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3") |
4 | 5 |
|
5 | 6 | @file:Repository("https://bindings.krzeminski.it") |
6 | 7 | @file:DependsOn("actions:checkout:v4") |
7 | 8 | @file:DependsOn("gradle:actions__setup-gradle:v4") |
| 9 | +@file:OptIn(ExperimentalKotlinLogicStep::class) |
8 | 10 |
|
9 | 11 | import io.github.typesafegithub.workflows.actions.actions.Checkout |
10 | 12 | import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle |
| 13 | +import io.github.typesafegithub.workflows.annotations.ExperimentalKotlinLogicStep |
11 | 14 | import io.github.typesafegithub.workflows.domain.RunnerType |
12 | 15 | import io.github.typesafegithub.workflows.domain.triggers.WorkflowDispatch |
13 | 16 | import io.github.typesafegithub.workflows.dsl.expressions.expr |
14 | 17 | import io.github.typesafegithub.workflows.dsl.workflow |
| 18 | +import kotlinx.serialization.json.Json |
| 19 | +import kotlinx.serialization.json.contentOrNull |
| 20 | +import kotlinx.serialization.json.jsonObject |
| 21 | +import kotlinx.serialization.json.jsonPrimitive |
15 | 22 |
|
16 | 23 | workflow( |
17 | 24 | name = "Release", |
@@ -74,13 +81,39 @@ workflow( |
74 | 81 | val versionExpr = expr { "github.event.inputs.version" } |
75 | 82 |
|
76 | 83 | run( |
77 | | - name = "Create and push tag", |
| 84 | + name = "Create and push a patch version tag", |
78 | 85 | command = """ |
79 | 86 | git tag -a "$versionExpr" -m "Release version $versionExpr" |
80 | 87 | git push origin "$versionExpr" |
81 | 88 | """.trimIndent() |
82 | 89 | ) |
83 | 90 |
|
| 91 | + val MAJOR_VERSION_OUTPUT_NAME = "majorVersion" |
| 92 | + |
| 93 | + val extractMajorVersion = run { |
| 94 | + // There should be a way to access the inputs using the DSL. |
| 95 | + // TODO: https://github.com/typesafegithub/github-workflows-kt/issues/1685 |
| 96 | + val githubContextJson = System.getenv("GHWKT_GITHUB_CONTEXT_JSON")!! |
| 97 | + val version: String = Json.parseToJsonElement(githubContextJson) |
| 98 | + .jsonObject["event"] |
| 99 | + ?.jsonObject?.get("inputs") |
| 100 | + ?.jsonObject?.get("version") |
| 101 | + ?.jsonPrimitive?.contentOrNull |
| 102 | + ?: error("Version couldn't be extracted from input") |
| 103 | + val majorVersion = version.substringBefore(".") |
| 104 | + outputs[MAJOR_VERSION_OUTPUT_NAME] = majorVersion |
| 105 | + } |
| 106 | + |
| 107 | + val majorVersionExpr = expr { "steps.${extractMajorVersion.id}.outputs.$MAJOR_VERSION_OUTPUT_NAME" } |
| 108 | + |
| 109 | + run( |
| 110 | + name = "Create or update a major version tag", |
| 111 | + command = """ |
| 112 | + git tag "$majorVersionExpr" -f |
| 113 | + git push origin "$majorVersionExpr" -f |
| 114 | + """.trimIndent() |
| 115 | + ) |
| 116 | + |
84 | 117 | run( |
85 | 118 | name = "Delete temp branch", |
86 | 119 | command = "git push origin --delete $tempBranchName" |
|
0 commit comments