Skip to content

Commit

Permalink
Update major version tag upon releasing (#222)
Browse files Browse the repository at this point in the history
Part of #211.
  • Loading branch information
krzema12 authored Oct 13, 2024
1 parent 49eb4c2 commit 3d54a0d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
35 changes: 34 additions & 1 deletion .github/workflows/release.main.kts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 3d54a0d

Please sign in to comment.