From 3d54a0d5b5937c6dbc8d49e5e5df54d858542fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Krzemi=C5=84ski?= <3110813+krzema12@users.noreply.github.com> Date: Sun, 13 Oct 2024 23:12:56 +0200 Subject: [PATCH] Update major version tag upon releasing (#222) Part of #211. --- .github/workflows/release.main.kts | 35 +++++++++++++++++++++++++++++- .github/workflows/release.yaml | 11 +++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.main.kts b/.github/workflows/release.main.kts index de2e8f8..2ab4c95 100755 --- a/.github/workflows/release.main.kts +++ b/.github/workflows/release.main.kts @@ -1,17 +1,24 @@ #!/usr/bin/env kotlin @file:Repository("https://repo1.maven.org/maven2/") @file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.0.0") +@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3") @file:Repository("https://bindings.krzeminski.it") @file:DependsOn("actions:checkout:v4") @file:DependsOn("gradle:actions__setup-gradle:v4") +@file:OptIn(ExperimentalKotlinLogicStep::class) import io.github.typesafegithub.workflows.actions.actions.Checkout import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle +import io.github.typesafegithub.workflows.annotations.ExperimentalKotlinLogicStep import io.github.typesafegithub.workflows.domain.RunnerType import io.github.typesafegithub.workflows.domain.triggers.WorkflowDispatch import io.github.typesafegithub.workflows.dsl.expressions.expr import io.github.typesafegithub.workflows.dsl.workflow +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive workflow( name = "Release", @@ -74,13 +81,39 @@ workflow( val versionExpr = expr { "github.event.inputs.version" } run( - name = "Create and push tag", + name = "Create and push a patch version tag", command = """ git tag -a "$versionExpr" -m "Release version $versionExpr" git push origin "$versionExpr" """.trimIndent() ) + val MAJOR_VERSION_OUTPUT_NAME = "majorVersion" + + val extractMajorVersion = run { + // There should be a way to access the inputs using the DSL. + // TODO: https://github.com/typesafegithub/github-workflows-kt/issues/1685 + val githubContextJson = System.getenv("GHWKT_GITHUB_CONTEXT_JSON")!! + val version: String = Json.parseToJsonElement(githubContextJson) + .jsonObject["event"] + ?.jsonObject?.get("inputs") + ?.jsonObject?.get("version") + ?.jsonPrimitive?.contentOrNull + ?: error("Version couldn't be extracted from input") + val majorVersion = version.substringBefore(".") + outputs[MAJOR_VERSION_OUTPUT_NAME] = majorVersion + } + + val majorVersionExpr = expr { "steps.${extractMajorVersion.id}.outputs.$MAJOR_VERSION_OUTPUT_NAME" } + + run( + name = "Create or update a major version tag", + command = """ + git tag "$majorVersionExpr" -f + git push origin "$majorVersionExpr" -f + """.trimIndent() + ) + run( name = "Delete temp branch", command = "git push origin --delete $tempBranchName" diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index e918207..9afdb85 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -58,10 +58,19 @@ jobs: name: 'Push commit' run: 'git push --set-upstream origin temp-branch-for-release' - id: 'step-7' - name: 'Create and push tag' + name: 'Create and push a patch version tag' run: |- git tag -a "${{ github.event.inputs.version }}" -m "Release version ${{ github.event.inputs.version }}" git push origin "${{ github.event.inputs.version }}" - id: 'step-8' + env: + GHWKT_GITHUB_CONTEXT_JSON: '${{ toJSON(github) }}' + run: 'GHWKT_RUN_STEP=''release:step-8'' ''.github/workflows/release.main.kts''' + - id: 'step-9' + name: 'Create or update a major version tag' + run: |- + git tag "${{ steps.step-8.outputs.majorVersion }}" -f + git push origin "${{ steps.step-8.outputs.majorVersion }}" -f + - id: 'step-10' name: 'Delete temp branch' run: 'git push origin --delete temp-branch-for-release'