-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-release-input
77 lines (71 loc) · 2.64 KB
/
git-release-input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@Library('my-library') _
import groovy.json.JsonSlurper
pipeline {
agent any
parameters {
choice(
choices: ['develop', 'uat', 'staging', 'prod'],
description: 'Target environment',
name: 'TARGETENV'
)
activeChoiceReactiveParam(
choiceType: 'PT_SINGLE_SELECT',
description: 'Release version',
filterLength: 1,
name: 'VERSION',
script: """
def repoUrl = 'https://api.github.com/repos/<org>/<repo>/releases'
def targetEnv = '${TARGETENV}'
def preReleaseAllowed = targetEnv != 'prod'
withEnv(['GITHUB_TOKEN=your_token_here']) {
try {
def response = httpRequest(
url: repoUrl,
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
headers: [
Authorization: "Bearer ${env.GITHUB_TOKEN}"
]
)
def json = new JsonSlurper().parseText(response.getContent())
def versions = json.findAll {
it.prerelease == preReleaseAllowed && it.tag_name != null
}.collect {
it.tag_name
}
return versions.sort { a, b -> -a.compareTo(b) }
} catch (Exception ex) {
return [ex.getMessage()]
}
}
"""
)
}
stages {
stage('Validate parameters') {
steps {
script {
def targetEnv = params.TARGETENV
def version = params.VERSION
if (!['develop', 'uat', 'staging', 'prod'].contains(targetEnv)) {
error "Invalid TARGETENV parameter: ${targetEnv}"
}
if (!version) {
error "Missing VERSION parameter"
}
if (version instanceof List && version.size() == 1 && version[0].startsWith('Error')) {
error version[0]
}
}
}
}
stage('Build and deploy') {
when {
expression { params.TARGETENV != 'develop' }
}
steps {
// Build and deploy logic
}
}
}
}