Skip to content

Commit 3d54a0d

Browse files
authored
Update major version tag upon releasing (#222)
Part of #211.
1 parent 49eb4c2 commit 3d54a0d

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

.github/workflows/release.main.kts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
#!/usr/bin/env kotlin
22
@file:Repository("https://repo1.maven.org/maven2/")
33
@file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.0.0")
4+
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
45

56
@file:Repository("https://bindings.krzeminski.it")
67
@file:DependsOn("actions:checkout:v4")
78
@file:DependsOn("gradle:actions__setup-gradle:v4")
9+
@file:OptIn(ExperimentalKotlinLogicStep::class)
810

911
import io.github.typesafegithub.workflows.actions.actions.Checkout
1012
import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle
13+
import io.github.typesafegithub.workflows.annotations.ExperimentalKotlinLogicStep
1114
import io.github.typesafegithub.workflows.domain.RunnerType
1215
import io.github.typesafegithub.workflows.domain.triggers.WorkflowDispatch
1316
import io.github.typesafegithub.workflows.dsl.expressions.expr
1417
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
1522

1623
workflow(
1724
name = "Release",
@@ -74,13 +81,39 @@ workflow(
7481
val versionExpr = expr { "github.event.inputs.version" }
7582

7683
run(
77-
name = "Create and push tag",
84+
name = "Create and push a patch version tag",
7885
command = """
7986
git tag -a "$versionExpr" -m "Release version $versionExpr"
8087
git push origin "$versionExpr"
8188
""".trimIndent()
8289
)
8390

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+
84117
run(
85118
name = "Delete temp branch",
86119
command = "git push origin --delete $tempBranchName"

.github/workflows/release.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,19 @@ jobs:
5858
name: 'Push commit'
5959
run: 'git push --set-upstream origin temp-branch-for-release'
6060
- id: 'step-7'
61-
name: 'Create and push tag'
61+
name: 'Create and push a patch version tag'
6262
run: |-
6363
git tag -a "${{ github.event.inputs.version }}" -m "Release version ${{ github.event.inputs.version }}"
6464
git push origin "${{ github.event.inputs.version }}"
6565
- id: 'step-8'
66+
env:
67+
GHWKT_GITHUB_CONTEXT_JSON: '${{ toJSON(github) }}'
68+
run: 'GHWKT_RUN_STEP=''release:step-8'' ''.github/workflows/release.main.kts'''
69+
- id: 'step-9'
70+
name: 'Create or update a major version tag'
71+
run: |-
72+
git tag "${{ steps.step-8.outputs.majorVersion }}" -f
73+
git push origin "${{ steps.step-8.outputs.majorVersion }}" -f
74+
- id: 'step-10'
6675
name: 'Delete temp branch'
6776
run: 'git push origin --delete temp-branch-for-release'

0 commit comments

Comments
 (0)