From 22888ab75116af69fe88ec79e95ba8c5559f1e5b Mon Sep 17 00:00:00 2001
From: Christian Poessinger <christian@poessinger.com>
Date: Sat, 20 Jun 2020 22:45:22 +0200
Subject: Jenkins: lib: deploy packages do dev.packages.vyos.net

---
 vars/buildPackage.groovy | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/vars/buildPackage.groovy b/vars/buildPackage.groovy
index 96d6a164..f2a72dc4 100644
--- a/vars/buildPackage.groovy
+++ b/vars/buildPackage.groovy
@@ -54,6 +54,7 @@ def call(description, pkgList) {
             // get relative directory path to Jenkinsfile
             BASE_DIR = currentBuild.rawBuild.parent.definition.scriptPath.replace('Jenkinsfile', '')
             CHANGESET_DIR = "**/${env.BASE_DIR}*"
+            DEBIAN_ARCH = sh(returnStdout: true, script: 'dpkg --print-architecture').trim()
         }
         options {
             disableConcurrentBuilds()
@@ -116,6 +117,47 @@ def call(description, pkgList) {
                     if (isCustomBuild()) {
                         // archive *.deb artifact on custom builds, deploy to repo otherwise
                         archiveArtifacts artifacts: '**/*.deb', allowEmptyArchive: true
+                    } else {
+                        // publish build result, using SSH-dev.packages.vyos.net Jenkins Credentials
+                        sshagent(['SSH-dev.packages.vyos.net']) {
+                            // build up some fancy groovy variables so we do not need to write/copy
+                            // every option over and over again!
+                            def RELEASE = getGitBranchName()
+                            if (getGitBranchName() == "master") {
+                                RELEASE = 'current'
+                            }
+
+                            def VYOS_REPO_PATH = '/home/sentrium/web/dev.packages.vyos.net/public_html/repositories/' + RELEASE + '/'
+                            if (getGitBranchName() == "crux")
+                                VYOS_REPO_PATH += 'vyos/'
+
+                            def SSH_OPTS = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=ERROR'
+                            def SSH_REMOTE = 'khagen@10.217.48.113'
+
+                            echo "Uploading package(s) and updating package(s) in the repository ..."
+
+                            def SSH_DIR = '~/VyOS/' + RELEASE + '/' + env.DEBIAN_ARCH
+                            def ARCH_OPT = ''
+                            if (env.DEBIAN_ARCH != 'all')
+                                ARCH_OPT = '-A ' + env.DEBIAN_ARCH
+
+                            sh """scp ${SSH_OPTS} *.deb ${SSH_REMOTE}:${SSH_DIR}/"""
+
+                            files = findFiles(glob: '*.deb')
+                            files.each { FILE ->
+                                def PKG = sh(returnStdout: true, script: "dpkg-deb -f ${FILE} Package").trim()
+                                // No need to explicitly check the return code. The pipeline
+                                // will fail if sh returns a noni-zero exit code
+                                sh """
+                                    ssh ${SSH_OPTS} ${SSH_REMOTE} "mkdir -p ${SSH_DIR}"
+                                    ssh ${SSH_OPTS} ${SSH_REMOTE} "\
+                                        uncron-add 'reprepro -v -b ${VYOS_REPO_PATH} ${ARCH_OPT} remove ${RELEASE} ${PKG}'; \
+                                        uncron-add 'reprepro -v -b ${VYOS_REPO_PATH} deleteunreferenced'; \
+                                        uncron-add 'reprepro -v -b ${VYOS_REPO_PATH} ${ARCH_OPT} includedeb ${RELEASE} ${SSH_DIR}/${FILE}'; \
+                                        "
+                                """
+                            }
+                        }
                     }
                 }
             }
-- 
cgit v1.2.3