build-scripts/gradle/GiteaMavenRepo.gradle

90 lines
2.9 KiB
Groovy

/**
* Configure default publishing with sources+javadoc. Copy to project to customize, this mostly serves as an example.
*/
def configureDefaultPublishing(Project project) {
project.java {
withJavadocJar()
withSourcesJar()
}
project.publishing {
repositories {
addGiteaPublishingRepository(it, giteaUrl, giteaRepoNamespace)
}
publications {
maven(MavenPublication) {
from components.java
}
}
}
}
def addGiteaRepository(RepositoryHandler handler, String giteaApiUrl, String organization, String token = null) {
handler.maven {
name = "gitea-$organization"
url = "${giteaApiUrl}/api/packages/${organization}/maven"
setGiteaRepoAuth(it, token)
}
}
def addGiteaPublishingRepository(RepositoryHandler handler, String giteaApiUrl, String organization, String token = null) {
handler.maven {
name = "gitea-$organization"
url = "${giteaApiUrl}/api/packages/${organization}/maven"
setGiteaRepoAuth(it, token)
}
}
/**
* Add auth to repo, attempt to find token by first non-null:
* - function argument "token"
* - env var: GITEA_BUILD_TOKEN
* - env var: CI_JOB_TOKEN
* - env var: CI_REGISTRY_PASSWORD
* - gradle project var: giteaPrivateToken
* Throws exception if no auth token was found.
*/
def setGiteaRepoAuth(MavenArtifactRepository maven, String token = null) {
if (token != null) {
maven.credentials(HttpHeaderCredentials) {
name = 'Authorization'
value = "token " + token
}
} else if (System.getenv("GITEA_BUILD_TOKEN") != null) {
maven.credentials(HttpHeaderCredentials) {
name = 'Authorization'
value = "token " + System.getenv("GITEA_BUILD_TOKEN")
}
} else if (System.getenv("CI_JOB_TOKEN") != null) {
maven.credentials(HttpHeaderCredentials) {
name = 'Authorization'
value = "token " + System.getenv("CI_JOB_TOKEN")
}
} else if (System.getenv("CI_REGISTRY_PASSWORD") != null) {
maven.credentials(HttpHeaderCredentials) {
name = 'Authorization'
value = "token " + System.getenv("CI_REGISTRY_PASSWORD")
}
} else if (project.hasProperty("giteaPrivateToken")) {
maven.credentials(HttpHeaderCredentials) {
name = 'Authorization'
value = "token " + giteaPrivateToken
}
} else {
throw Exception("No gitea repository auth configured")
}
maven.authentication {
header(HttpHeaderAuthentication)
}
}
// Export methods by turning them into closures
ext {
setGiteaRepoAuth = this.&setGiteaRepoAuth
addGiteaRepository = this.&addGiteaRepository
addGiteaPublishingRepository = this.&addGiteaPublishingRepository
configureDefaultPublishing = this.&configureDefaultPublishing
}