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", "extends": "recommended",
"rules": { "rules": {
"CompileStatic": {
"enabled": false
},
"DuplicateStringLiteral": { "DuplicateStringLiteral": {
"enabled": false "enabled": false
}, },
"FactoryMethodName": {
"enabled": false
},
"Indentation": { "Indentation": {
"enabled": false "enabled": false
} }

View File

@ -1,8 +1,8 @@
#!/usr/bin/env groovy #!/usr/bin/env groovy
/* /*
Calculate the current version by querying a list of assets in a Nexus Repository. Calculate the current version by querying a list of assets in a Nexus Repository.
Return '0.0.1' if there are no matching assets. Return '0.0.1' if there are no matching assets.
Jenkins Plugin dependencies: 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 call(Map config = [:]) {
Map defaults = [ Map defaults = [
nexusBase: 'https://repo.thejimnicholson.com', nexusBase: 'https://repo.thejimnicholson.com',
searchAPI: 'service/rest/v1/search', searchAPI: 'service/rest/v1/search',
build_flag: Flags.BUILDNUM_IS_FROM_QUERY,
queryParams: [ queryParams: [
'repository': 'tools', 'repository': 'tools',
'sort': 'name', 'sort': 'name',
@ -33,16 +43,41 @@ Map call(Map config = [:]) {
body = response.content body = response.content
assets = readJSON text: body assets = readJSON text: body
latest = assets.items.last() 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 return runConfig
} }
def buildUrlWithQueryParams(baseUrl, queryParams) { /* groovylint-disable-next-line MethodParameterTypeRequired, NoDef */
def url = baseUrl String buildUrlWithQueryParams(baseUrl, queryParams) {
url = baseUrl
if (!queryParams.empty) { if (!queryParams.empty) {
def queryParamsString = queryParams.collect { key, value -> queryParamsString = queryParams.collect { key, value ->
"${URLEncoder.encode(key, 'UTF-8')}=${URLEncoder.encode(value, 'UTF-8')}" "${URLEncoder.encode(key, 'UTF-8')}=${URLEncoder.encode(value, 'UTF-8')}"
}.join('&') }.join('&')
url += '?' + queryParamsString url += '?' + queryParamsString