Skip to content

Commit

Permalink
Merge pull request #250 from alaw-viator/feature/extend-gitlab-publis…
Browse files Browse the repository at this point in the history
…hing-for-self-managed

Extended GitLab Publishing for self managed instances #249
  • Loading branch information
samhill303 authored Jul 16, 2024
2 parents 3c0f17e + 473ae28 commit 1a01510
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
23 changes: 13 additions & 10 deletions kmmbridge/src/main/kotlin/BuildFileHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
import co.touchlab.faktory.internal.githubPublishTokenOrNull
import co.touchlab.faktory.internal.githubPublishUser
import co.touchlab.faktory.internal.githubRepoOrNull
import co.touchlab.faktory.internal.gitlabPublishTokenOrNull
import co.touchlab.faktory.internal.gitlabPublishUser
import co.touchlab.faktory.internal.gitlabRepoOrNull
import co.touchlab.faktory.internal.gitLabPublishTokenOrNull
import co.touchlab.faktory.internal.gitLabPublishUser
import co.touchlab.faktory.internal.gitLabRepoOrNull
import co.touchlab.faktory.internal.gitLabDomain
import co.touchlab.faktory.publishingExtension
import org.gradle.api.Project
import org.gradle.api.credentials.HttpHeaderCredentials
Expand Down Expand Up @@ -51,8 +52,9 @@ fun Project.addGithubPackagesRepository() {

/**
* Helper function to support GitLab Packages publishing.
* Pass in a valid GitLab token name with GITLAB_PUBLISH_USER. Defaults user to "Job-Token".
* Pass in a valid GitLab token with GITLAB_PUBLISH_TOKEN. Defaults token to CI_JOB_TOKEN.
* Pass in a valid GitLab token type with GITLAB_PUBLISH_USER. Options include; "Private-Token", "Deploy-Token" & "Job-Token" (default).
* Pass in a valid GitLab token for the specified type with GITLAB_PUBLISH_TOKEN. Defaults to CI_JOB_TOKEN environment variable.
* Pass in a custom GitLab domain. Useful for self-managed instances. Defaults to "gitlab.com".
*
* Generally, just add the following in the Gradle build file.
*
Expand All @@ -62,12 +64,13 @@ fun Project.addGithubPackagesRepository() {
fun Project.addGitlabPackagesRepository() {
publishingExtension.apply {
try {
val gitLabPublishUser = project.gitlabPublishUser ?: "Job-Token"
val gitLabPublishToken = project.gitlabPublishTokenOrNull ?: System.getenv("CI_JOB_TOKEN") ?: return
val gitLabRepo = project.gitlabRepoOrNull ?: return
val gitLabPublishUser = project.gitLabPublishUser ?: "Job-Token"
val gitLabPublishToken = project.gitLabPublishTokenOrNull ?: System.getenv("CI_JOB_TOKEN") ?: return
val gitLabRepo = project.gitLabRepoOrNull ?: return
val gitLabDomain = project.gitLabDomain ?: "gitlab.com"
repositories.maven {
name = "GitLabPackages"
url = uri("https://gitlab.com/api/v4/projects/$gitLabRepo/packages/maven")
url = uri("https://$gitLabDomain/api/v4/projects/$gitLabRepo/packages/maven")
credentials(HttpHeaderCredentials::class.java) {
name = gitLabPublishUser
value = gitLabPublishToken
Expand All @@ -77,7 +80,7 @@ fun Project.addGitlabPackagesRepository() {
}
}
} catch (e: Exception) {
logger.warn("Could not configure GitLabPackagesRepository!")
logger.warn("Could not configure GitLabPackagesRepository! - $e")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ import co.touchlab.faktory.findStringProperty
import org.gradle.api.Project
import java.net.URLEncoder

internal val Project.gitlabPublishTokenOrNull: String?
internal val Project.gitLabPublishTokenOrNull: String?
get() = project.property("GITLAB_PUBLISH_TOKEN") as String?

internal val Project.gitlabPublishUser: String?
internal val Project.gitLabPublishUser: String?
get() = project.findStringProperty("GITLAB_PUBLISH_USER")

internal val Project.gitlabRepoOrNull: String?
internal val Project.gitLabRepoOrNull: String?
get() {
val repo = project.findStringProperty("GITHUB_REPO") ?: return null
// The GitLab API accepts repo id or url-encoded path
val repoId = repo.toIntOrNull()
return repoId?.toString() ?: URLEncoder.encode(repo, "UTF-8")
}

internal val Project.gitLabDomain: String?
get() = project.findStringProperty("GITLAB_DOMAIN")

0 comments on commit 1a01510

Please sign in to comment.