-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrelease.main.kts
More file actions
executable file
·192 lines (174 loc) · 6.48 KB
/
release.main.kts
File metadata and controls
executable file
·192 lines (174 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env kotlin
@file:Repository("https://repo1.maven.org/maven2/")
@file:DependsOn("io.github.typesafegithub:github-workflows-kt:3.7.0")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.10.0")
@file:Repository("https://bindings.krzeminski.it")
@file:DependsOn("actions:checkout:v6")
@file:DependsOn("gradle:actions__setup-gradle:v6")
@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.JobBuilder
import io.github.typesafegithub.workflows.dsl.expressions.expr
import io.github.typesafegithub.workflows.dsl.workflow
import io.github.typesafegithub.workflows.yaml.CheckoutActionVersionSource
import io.github.typesafegithub.workflows.yaml.DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
workflow(
name = "Release",
on = listOf(
WorkflowDispatch(
inputs = mapOf(
"version" to WorkflowDispatch.Input(
type = WorkflowDispatch.Type.String,
required = true,
description = "Used for the tag and the version name. E.g. v1.2.3.",
)
),
),
),
consistencyCheckJobConfig = DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy(
checkoutActionVersion = CheckoutActionVersionSource.InferFromClasspath(),
),
sourceFile = __FILE__,
) {
job(
id = "release",
runsOn = RunnerType.UbuntuLatest,
) {
val tempBranchName = "temp-branch-for-release"
buildAndCommitDist(branchName = tempBranchName)
val versionExpr = expr { "github.event.inputs.version" }
run(
name = "Create and push a patch version tag",
workingDirectory = "github-actions-typing",
command = """
git tag -a "$versionExpr" -m "Release version $versionExpr"
git push origin "$versionExpr"
""".trimIndent()
)
val MAJOR_VERSION_OUTPUT_NAME = "majorVersion"
val extractMajorVersion = run(
name = "Extract major version",
workingDirectory = "github-actions-typing",
) {
// 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 branch",
workingDirectory = "github-actions-typing",
command = """
git branch -D "$majorVersionExpr" || true
git checkout -b "$majorVersionExpr"
git push origin "$majorVersionExpr" -f
""".trimIndent()
)
run(
name = "Delete temp branch",
workingDirectory = "github-actions-typing",
command = "git push origin --delete $tempBranchName"
)
}
}
workflow(
name = "Make branch runnable",
on = listOf(
WorkflowDispatch(),
),
consistencyCheckJobConfig = DEFAULT_CONSISTENCY_CHECK_JOB_CONFIG.copy(
checkoutActionVersion = CheckoutActionVersionSource.InferFromClasspath(),
),
sourceFile = __FILE__,
targetFileName = "make-branch-runnable.yaml",
) {
job(
id = "make-branch-runnable",
runsOn = RunnerType.UbuntuLatest,
) {
buildAndCommitDist()
}
}
private fun JobBuilder<*>.buildAndCommitDist(branchName: String? = null) {
uses(
name = "Checkout github-actions-typing",
action = Checkout(
path = "github-actions-typing"
)
)
uses(
name = "Checkout github-actions-typing-catalog",
action = Checkout(
repository = "typesafegithub/github-actions-typing-catalog",
path = "github-actions-typing-catalog"
)
)
uses(action = ActionsSetupGradle())
run(
workingDirectory = "github-actions-typing",
command = "./gradlew build"
)
run(
name = "Regenerate the contents of dist directory",
workingDirectory = "github-actions-typing",
command = """
set -euxo pipefail
rm -rf dist
unzip -qq build/distributions/github-actions-typing.zip -d dist
""".trimIndent()
)
run(
name = "Configure git",
workingDirectory = "github-actions-typing",
command = """
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
""".trimIndent()
)
if (branchName != null) {
run(
name = "Commit changes to temp branch",
workingDirectory = "github-actions-typing",
command = """
git checkout -b $branchName
git add .
git commit -m "Update dist"
""".trimIndent()
)
run(
name = "Push commit",
workingDirectory = "github-actions-typing",
command = "git push --set-upstream origin $branchName",
)
} else {
run(
name = "Commit changes",
workingDirectory = "github-actions-typing",
command = """
git add .
git commit -m "Update dist"
""".trimIndent()
)
run(
name = "Push commit",
workingDirectory = "github-actions-typing",
command = "git push",
)
}
}