From 495326e990efe2cf7ee4d13a2143cbf43dea7ed9 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 8 Jan 2021 15:06:34 +0100 Subject: [PATCH] Publishing tasks for rpm and deb packages --- build-plugins/build.gradle | 1 + .../com/cloudogu/scm/HttpUploadTask.groovy | 133 ++++++++++++++++++ .../com/cloudogu/scm/JavaModulePlugin.groovy | 1 - .../com/cloudogu/scm/PackagingPlugin.groovy | 1 + gradle/dependencies.gradle | 1 + scm-packaging/deb/build.gradle | 11 ++ scm-packaging/rpm/build.gradle | 12 ++ 7 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 build-plugins/src/main/groovy/com/cloudogu/scm/HttpUploadTask.groovy diff --git a/build-plugins/build.gradle b/build-plugins/build.gradle index 0e74929379..737d74ab08 100644 --- a/build-plugins/build.gradle +++ b/build-plugins/build.gradle @@ -38,6 +38,7 @@ dependencies { implementation libraries.guava implementation libraries.jettyServer implementation libraries.jettyWebapp + implementation libraries.jettyClient implementation libraries.snakeYml } diff --git a/build-plugins/src/main/groovy/com/cloudogu/scm/HttpUploadTask.groovy b/build-plugins/src/main/groovy/com/cloudogu/scm/HttpUploadTask.groovy new file mode 100644 index 0000000000..54ca036f96 --- /dev/null +++ b/build-plugins/src/main/groovy/com/cloudogu/scm/HttpUploadTask.groovy @@ -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 + } + +} diff --git a/build-plugins/src/main/groovy/com/cloudogu/scm/JavaModulePlugin.groovy b/build-plugins/src/main/groovy/com/cloudogu/scm/JavaModulePlugin.groovy index 1db269db72..37ff3c4172 100644 --- a/build-plugins/src/main/groovy/com/cloudogu/scm/JavaModulePlugin.groovy +++ b/build-plugins/src/main/groovy/com/cloudogu/scm/JavaModulePlugin.groovy @@ -57,7 +57,6 @@ class JavaModulePlugin implements Plugin { failOnError false } - project.publishing { publications { mavenJava(MavenPublication) { diff --git a/build-plugins/src/main/groovy/com/cloudogu/scm/PackagingPlugin.groovy b/build-plugins/src/main/groovy/com/cloudogu/scm/PackagingPlugin.groovy index 10a34f225c..2e371dc3ef 100644 --- a/build-plugins/src/main/groovy/com/cloudogu/scm/PackagingPlugin.groovy +++ b/build-plugins/src/main/groovy/com/cloudogu/scm/PackagingPlugin.groovy @@ -33,6 +33,7 @@ class PackagingPlugin implements Plugin { void apply(Project project) { project.ext.PackageYaml = PackageYamlTask project.ext.ReleaseYaml = ReleaseYamlTask + project.ext.HttpUploadTask = HttpUploadTask } } diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index cc17575757..f268a35ce9 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -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}", diff --git a/scm-packaging/deb/build.gradle b/scm-packaging/deb/build.gradle index b95ad62468..12913d6a62 100644 --- a/scm-packaging/deb/build.gradle +++ b/scm-packaging/deb/build.gradle @@ -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 +} diff --git a/scm-packaging/rpm/build.gradle b/scm-packaging/rpm/build.gradle index 45e8b6848e..65b86f8981 100644 --- a/scm-packaging/rpm/build.gradle +++ b/scm-packaging/rpm/build.gradle @@ -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 +}