Publishing tasks for rpm and deb packages

This commit is contained in:
Sebastian Sdorra
2021-01-08 15:06:34 +01:00
committed by René Pfeuffer
parent 217f6348c0
commit 495326e990
7 changed files with 159 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ dependencies {
implementation libraries.guava
implementation libraries.jettyServer
implementation libraries.jettyWebapp
implementation libraries.jettyClient
implementation libraries.snakeYml
}

View File

@@ -0,0 +1,133 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.cloudogu.scm
import org.eclipse.jetty.client.HttpClient
import org.eclipse.jetty.client.api.ContentResponse
import org.eclipse.jetty.client.util.BasicAuthentication
import org.eclipse.jetty.util.ssl.SslContextFactory
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.bundling.AbstractArchiveTask
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class HttpUploadTask extends DefaultTask {
private static final Logger LOG = LoggerFactory.getLogger(HttpUploadTask)
@Input
File artifact
@Input
String snapshotUrl
@Input
String releaseUrl
@Input
String method = "POST"
@Input
@Optional
String username
@Input
@Optional
String password
HttpUploadTask() {
// http upload ist not cacheable
outputs.upToDateWhen {
false
}
}
void artifact(Object object) {
println object
println object.class
if (object instanceof AbstractArchiveTask) {
artifact = object.getArchiveFile().get().asFile
} else if (object instanceof File) {
artifact = object
} else if (object instanceof String) {
artifact = new File(object)
} else {
throw new IllegalArgumentException("unknown artifact type")
}
}
@TaskAction
void upload() {
URI uri = URI.create(createURL())
LOG.info("Upload {} with {} to {}", artifact, method, uri)
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client()
HttpClient client = new HttpClient(sslContextFactory)
client.getAuthenticationStore().addAuthenticationResult(new BasicAuthentication.BasicResult(
uri, username, password
))
client.start()
try {
ContentResponse response = client.newRequest(uri)
.method(method)
.file(artifact.toPath())
.send()
int status = response.getStatus()
if (status >= 300) {
throw new GradleException("failed to upload artifact, server returned ${status}")
} else {
LOG.info("successfully upload artifact, server returned with status {}", status)
}
} finally {
client.stop()
}
}
private String createURL() {
String repositoryUrl = createRepositoryUrl()
if ("PUT".equals(method)) {
if (!repositoryUrl.endsWith("/")) {
repositoryUrl += "/"
}
return repositoryUrl + artifact.name
}
return repositoryUrl
}
private String createRepositoryUrl() {
if (project.version.contains("SNAPSHOT")) {
return snapshotUrl
}
return releaseUrl
}
}

View File

@@ -57,7 +57,6 @@ class JavaModulePlugin implements Plugin<Project> {
failOnError false
}
project.publishing {
publications {
mavenJava(MavenPublication) {

View File

@@ -33,6 +33,7 @@ class PackagingPlugin implements Plugin<Project> {
void apply(Project project) {
project.ext.PackageYaml = PackageYamlTask
project.ext.ReleaseYaml = ReleaseYamlTask
project.ext.HttpUploadTask = HttpUploadTask
}
}

View File

@@ -140,6 +140,7 @@ ext {
jettyServer: "org.eclipse.jetty:jetty-server:${jettyVersion}",
jettyWebapp: "org.eclipse.jetty:jetty-webapp:${jettyVersion}",
jettyJmx: "org.eclipse.jetty:jetty-jmx:${jettyVersion}",
jettyClient: "org.eclipse.jetty:jetty-client:${jettyVersion}",
// tests
junitJupiterApi: "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}",

View File

@@ -178,3 +178,14 @@ license {
task license(type: com.hierynomus.gradle.license.tasks.LicenseCheck) {
source = fileTree('.')
}
task publish(type: HttpUploadTask) {
artifact deb
releaseUrl "https://packages.scm-manager.org/repository/apt-v2-releases/"
snapshotUrl "https://packages.scm-manager.org/repository/apt-v2-snapshots/"
if (project.hasProperty("packagesScmManagerUsername") && project.hasProperty("packagesScmManagerPassword")) {
username project.property("packagesScmManagerUsername")
password project.property("packagesScmManagerPassword")
}
dependsOn deb
}

View File

@@ -197,3 +197,15 @@ license {
task license(type: com.hierynomus.gradle.license.tasks.LicenseCheck) {
source = fileTree('.')
}
task publish(type: HttpUploadTask) {
artifact rpm
method "PUT"
releaseUrl "https://packages.scm-manager.org/repository/yum-v2-releases/"
snapshotUrl "https://packages.scm-manager.org/repository/yum-v2-snapshots/"
if (project.hasProperty("packagesScmManagerUsername") && project.hasProperty("packagesScmManagerPassword")) {
username project.property("packagesScmManagerUsername")
password project.property("packagesScmManagerPassword")
}
dependsOn rpm
}