Create a script for publishing a set of debian packages to a remote update server

This commit is contained in:
Dan Hawkins 2022-01-09 21:56:34 +00:00
commit 9285f75c77

View file

@ -0,0 +1,174 @@
def packagecollection = null
/* Example expected JSON
{
"packages": {
"gis-hmb-fd-hv-esw-el-sg1024": {
"Location": "Jenkins job artifact",
"Job": "HMB_FD_HV_0.9.6/HMB_FD_HV_app_0.9.6_debpackage",
"Job selector": "1248",
"Release repo": "git@hydra:hmb_fd_hv-releases/sg1024/hmb_fd_hv_package.git",
"Release repo revision": "refs/tag/0.9.6.57669"
"Path": "package/gis-hmb-fd-hv-esw-el-sg1024_armhf.deb",
},
"gis-hmb-fd-hv-esw-el-sg1024": {
"Location": "Jenkins job artifact",
"Job": "HMB_FD_HV_0.9.6/HMB_FD_HV_control_0.9.6_debpackage",
"Job selector": "1722",
"Release repo": "git@hydra:hmb_fd_hv-releases/sg1024/hmb_fd_hv_control_package.git",
"Release repo revision": "refs/tag/0.9.6.7964"
"Path": "package/gis-hmb-fd-hv-control-service-sg1024_armhf.deb",
}
}
}
*/
def String packagecollection_asJsonText() {
return writeJSON(returnText: true, json: packagecollection, pretty: 1)
}
def boolean packagecollection_exists() {
return (packagecollection != null)
}
def packagecollection_set(String jsontext) {
packagecollection = readJSON(text: jsontext)
echo "Package collection info:\n${asJsonText()}"
}
def packagecollection_get() {
return packagecollection
}
pipeline {
agent { label AGENT_LABEL }
stages {
stage('Initial setup') {
steps {
script {
meta = load("${WORKSPACE}/autobuild/Common_scripts/autobuilder.groovy").startnonbuild(this)
}
}
}
stage('Clean exports') {
steps {
script {
PACKAGEDEST = "${WORKSPACE}/package"
cleanfolders = [
PACKAGEDEST,
]
cleaner = load("${autobuilder.commonscripts()}/clean/clean.groovy")
cleaner.clean(cleanfolders)
}
}
}
stage('Transfer packages') {
steps {
script {
importer = load("${autobuilder.commonscripts()}/import/importer.groovy")
packagecollection_set(PACKAGE_COLLECTION)
debpackagecollection = packagecollection_get()
debpackagecollection.packages.each { debpackageinfo->
def packagekey = debpackageinfo.key
echo "Processing package: ${packagekey}"
def packagedest = "${PACKAGEDEST}/packagekey"
def packageimportoptions = importer.getNewImportOptions()
// For backward compatibility - in case these parameters are not supplied
packageimportoptions.source(debpackageinfo['Location'],
debpackageinfo['Job'],
debpackageinfo['Job selector'],
debpackageinfo['Release repo'],
debpackageinfo['Release repo revision'])
packageimportoptions.setincrementversionnumber(false)
packagemeta = importer.artifactimport(meta, packageimportoptions, packagedest)
PACKAGE_FILE = "${packagedest}/package/${debpackageinfo['Path']}"
echo "Aptly config file is ${APTLY_CONFIG_FILE}"
echo "Repo name is ${REPO_NAME}"
echo "Unique package folder is ${UNIQUE_PACKAGE_FOLDER}"
sh """#!/bin/bash -v
aptly config show
if [ \$? -ne 0 ]; then exit 1; fi
aptly repo -config="${APTLY_CONFIG_FILE}" -with-packages show "${REPO_NAME}"
if [ \$? -ne 0 ]; then
echo "aptly repo ${REPO_NAME} does not exist; Creating it"
aptly repo -config="${APTLY_CONFIG_FILE}" create "${REPO_NAME}"
if [ \$? -ne 0 ]; then exit 1; fi
fi
aptly repo -config="${APTLY_CONFIG_FILE}" -with-packages show "${REPO_NAME}"
if [ \$? -ne 0 ]; then exit 1; fi
# This errors if the published repo exists already; Ignore it
aptly publish -config="${APTLY_CONFIG_FILE}" drop "${DISTRIBUTION}" "${PUBLISH_PATH_PREFIX}"
aptly publish -config="${APTLY_CONFIG_FILE}" list
if [ \$? -ne 0 ]; then exit 1; fi
aptly repo -config="${APTLY_CONFIG_FILE}" add "${REPO_NAME}" "${UNIQUE_PACKAGE_FOLDER}"
if [ \$? -ne 0 ]; then exit 1; fi
aptly repo -config="${APTLY_CONFIG_FILE}" show -with-packages "${REPO_NAME}"
if [ \$? -ne 0 ]; then exit 1; fi
"""
echo "Snapshot name is ${SNAPSHOT_NAME}"
FULL_SNAPSHOT_NAME = "${REPO_NAME}_${SNAPSHOT_NAME}"
PACKAGE_VERSION_PREFIX = packagemeta.buildstampinfo.buildstampprefix
echo "Full Snapshot name is ${FULL_SNAPSHOT_NAME} containing packages of versions beginning with: ${PACKAGE_VERSION_PREFIX}"
// Convert prefix to regular expression
PACKAGE_VERSION_PREFIX_SPLIT = PACKAGE_VERSION_PREFIX.split("[.]")
PACKAGE_VERSION_REGEX = ''
i = 0
while(true) {
if (i == PACKAGE_VERSION_PREFIX_SPLIT.size() - 1) {
PACKAGE_VERSION_REGEX += PACKAGE_VERSION_PREFIX_SPLIT[i]
break;
}
PACKAGE_VERSION_REGEX += PACKAGE_VERSION_PREFIX_SPLIT[i]
PACKAGE_VERSION_REGEX += '\\.'
i++
}
sh """#!/bin/bash -v
# This errors if the snapshot exists already; Ignore it
aptly snapshot -config="${APTLY_CONFIG_FILE}" drop "${FULL_SNAPSHOT_NAME}"
aptly snapshot -config="${APTLY_CONFIG_FILE}" drop "All_${FULL_SNAPSHOT_NAME}"
aptly snapshot -config="${APTLY_CONFIG_FILE}" create "All_${FULL_SNAPSHOT_NAME}" from repo "${REPO_NAME}"
if [ \$? -ne 0 ]; then exit 1; fi
aptly snapshot filter -config="/home/repos/gis-aptly-config/gis.conf" "All_${FULL_SNAPSHOT_NAME}" "${FULL_SNAPSHOT_NAME}" 'Version (~ .*${PACKAGE_VERSION_REGEX}.*)'
if [ \$? -ne 0 ]; then exit 1; fi
aptly snapshot -config="${APTLY_CONFIG_FILE}" -with-packages show "${FULL_SNAPSHOT_NAME}"
if [ \$? -ne 0 ]; then exit 1; fi
aptly publish -config="${APTLY_CONFIG_FILE}" -distribution="${DISTRIBUTION}" -component="${COMPONENT}" -gpg-key="${GPG_KEY_ID}" snapshot "${FULL_SNAPSHOT_NAME}" "${PUBLISH_PATH_PREFIX}"
if [ \$? -ne 0 ]; then exit 1; fi
aptly publish -config="${APTLY_CONFIG_FILE}" list
if [ \$? -ne 0 ]; then exit 1; fi
aptly publish -config="${APTLY_CONFIG_FILE}" show "${DISTRIBUTION}" "${PUBLISH_PATH_PREFIX}"
if [ \$? -ne 0 ]; then exit 1; fi
"""
}
}
}
}
}
post {
always {
echo 'Post-build'
}
failure {
echo 'Failed build'
}
unstable {
echo 'Unstable build'
}
success {
echo 'Succeeded build'
}
}
}