Skip to content

Commit

Permalink
Add a function to generate relaseName
Browse files Browse the repository at this point in the history
  • Loading branch information
ozonophore committed May 30, 2024
1 parent 5e71fca commit f3d5982
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions ft/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,55 @@ tasks.register("uninstallHelm") {
throw GradleException("Helm namespace is not set")
}
val result = exec {
commandLine("helm", "uninstall", helmRelease, "--namespace", helmNamespace)
commandLine("helm", "uninstall", sanitizeHelmReleaseName(helmRelease), "--namespace", helmNamespace)
}
if (result.exitValue != 0) {
throw GradleException("Helm uninstall failed with exit code ${result.exitValue}")
}
}
}

/**
* Sanitizes a Helm release name to ensure it adheres to Helm's naming conventions.
*
* Helm release names must:
* - Be no longer than 53 characters.
* - Contain only lowercase letters (`a-z`), digits (`0-9`), and hyphens (`-`).
* - Start with a letter.
* - End with a letter or digit.
*
* @param name The original release name to be sanitized.
* @return A sanitized release name that conforms to Helm's naming conventions.
*/
fun sanitizeHelmReleaseName(name: String?): String {
if (name == null) {
throw GradleException("Helm release name is not set")
}

var sanitized = name.toLowerCase().replace(Regex("[^a-z0-9-]"), "-")

if (!sanitized.first().isLetter()) {
sanitized = "a$sanitized"
}

if (!sanitized.last().isLetterOrDigit()) {
sanitized = sanitized.dropLastWhile { !it.isLetterOrDigit() }
if (sanitized.isEmpty()) {
sanitized = "default-release"
}
}

if (sanitized.length > 53) {
sanitized = sanitized.take(53)
}

if (!sanitized.last().isLetterOrDigit()) {
sanitized = sanitized.dropLastWhile { !it.isLetterOrDigit() }
}

return sanitized
}

tasks.register<Exec>("deployHelm") {

if (helmRelease == null) {
Expand All @@ -150,7 +191,7 @@ tasks.register<Exec>("deployHelm") {

setWorkingDir("./deploy")
commandLine("helm", "upgrade", "--wait"
, "--install", helmRelease, "chart"
, "--install", sanitizeHelmReleaseName(helmRelease), "chart"
, "--namespace", helmNamespace
, "--set", "bitbucket.license=$bitbucketLicense"
, "--set", "vcs-facade.image.tag=${project.version}"
Expand Down

0 comments on commit f3d5982

Please sign in to comment.