Options for version and setting build number

This commit is contained in:
Jim Nicholson 2024-02-07 22:19:21 -08:00
parent 3ec31b4a79
commit 12df0f4986
2 changed files with 48 additions and 7 deletions

View File

@ -1,9 +1,15 @@
{
"extends": "recommended",
"rules": {
"CompileStatic": {
"enabled": false
},
"DuplicateStringLiteral": {
"enabled": false
},
"FactoryMethodName": {
"enabled": false
},
"Indentation": {
"enabled": false
}

View File

@ -1,8 +1,8 @@
#!/usr/bin/env groovy
/*
Calculate the current version by querying a list of assets in a Nexus Repository.
Return '0.0.1' if there are no matching assets.
Calculate the current version by querying a list of assets in a Nexus Repository.
Return '0.0.1' if there are no matching assets.
Jenkins Plugin dependencies:
@ -11,10 +11,20 @@ Jenkins Plugin dependencies:
*/
// Define the enum
enum Flags {
BUILDNUM_IS_COMMITS_SINCE_TAG,
BUILDNUM_IS_JOB_NUMBER,
BUILDNUM_IS_FROM_QUERY
}
Map call(Map config = [:]) {
Map defaults = [
nexusBase: 'https://repo.thejimnicholson.com',
searchAPI: 'service/rest/v1/search',
build_flag: Flags.BUILDNUM_IS_FROM_QUERY,
queryParams: [
'repository': 'tools',
'sort': 'name',
@ -33,16 +43,41 @@ Map call(Map config = [:]) {
body = response.content
assets = readJSON text: body
latest = assets.items.last()
runConfig.nexusQuery = nexusSearch
runConfig.latestVersion = latest.name
version = latest.version
if (version == 'null') {
matches = (latest.name =~ /.*-([0-9\.]+)-([0-9]+)\.)[0]/)[0]
version = matches[1]
query_build = matches[2]
}
switch (build_flag) {
case Flags.BUILDNUM_IS_COMMITS_SINCE_TAG:
build = sh(
script: 'git rev-list $(git tag | tail -1).. --count',
returnStdout: true
).trim()
break
case Flags.BUILDNUM_IS_JOB_NUMBER:
build = env.BUILD_NUMBER
break
case Flags.BUILDNUM_IS_FROM_QUERY:
build = query_build
break
default:
build = query_build
break
}
runConfig.nexusQuery = nexusSearch
runConfig.latestVersion = version
runConfig.latestBuild = build
return runConfig
}
def buildUrlWithQueryParams(baseUrl, queryParams) {
def url = baseUrl
/* groovylint-disable-next-line MethodParameterTypeRequired, NoDef */
String buildUrlWithQueryParams(baseUrl, queryParams) {
url = baseUrl
if (!queryParams.empty) {
def queryParamsString = queryParams.collect { key, value ->
queryParamsString = queryParams.collect { key, value ->
"${URLEncoder.encode(key, 'UTF-8')}=${URLEncoder.encode(value, 'UTF-8')}"
}.join('&')
url += '?' + queryParamsString