forked from OpenLiberty/sample-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebcDockerBuilderLGS.jenkinsfile
127 lines (116 loc) · 4.87 KB
/
ebcDockerBuilderLGS.jenkinsfile
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
properties([
parameters([
// EBC relevant properties
string(name: 'executionId', defaultValue: UUID.randomUUID().toString(), description: 'Unique execution id'),
string(name: 'ebcPriority', defaultValue: '200', description: 'EBC Priority'),
string(name: 'ebcPlan', defaultValue: 'svl-dockerJenkins-ubuntu20_ppcle.yml', description: 'EBC plan to use when provisioning a Jenkins node'),
string(name: 'ebcBranch', defaultValue: "${env.ecosystem_branch}", description: 'Git branch used for ebc code'),
string(name: 'scriptOrg', defaultValue: "OpenLiberty", description: 'Git org containing docker build scripts'),
string(name: 'command', defaultValue: "make build-app-pipeline IMAGE=stg.icr.io/cp/olc-sample/open-liberty/samples/getting-started", description: 'Build command to execute on target arch machine, e.g. make build-pipeline-releases'),
string(name: 'BRANCH', defaultValue: "main", description: 'release branch to use'),
string(name: 'CONTAINER_REGISTRY', defaultValue: "stg.icr.io", description: 'staging registry to push images to')
])
])
timestamps {
// Identify if the job was kicked off by the seed job.
def causes = currentBuild.getBuildCauses()
for(cause in causes) {
if ("seed".equalsIgnoreCase(cause.upstreamProject)) {
// As the seed job kicked off this build, bail early returning success.
// This allows the jenkinsfile's properties to be populated.
currentBuild.result = 'SUCCESS'
println "Returning success as upstream job is the seed job; this is therefore a dummy run to populate job parameters."
return
}
}
def ebcPriority = "${params.ebcPriority}"
def executionId = "${params.executionId}"
def ebcPlan = "${params.ebcPlan}"
try {
node (label: 'built-in') {
ws("workspace/${env.JOB_NAME}-${env.BUILD_NUMBER}") {
stage ("EBC Demand"){
//This is executing on Jenkins Server
ebcDemand()
gitCloneAndStash();
}
}
}
node(label: "ebc_${executionId}"){
stage("Running Job"){
withCredentials([usernamePassword(credentialsId: 'operator_icrId', usernameVariable: 'PIPELINE_USERNAME', passwordVariable: 'PIPELINE_PASSWORD'),
usernamePassword(credentialsId: 'dockerId', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
//This is executing on ebc dynamic machine
doWork();
} // withCredentials() end
}
}
} finally {
node (label: 'built-in') {
ws("workspace/${env.JOB_NAME}-${env.BUILD_NUMBER}") {
stage ("EBC Cleanup"){
//This is executing on Jenkins Server
ebcCleanup();
// Clean up the workspace
cleanWs(cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenNotBuilt: false,
cleanWhenSuccess: true,
cleanWhenUnstable: true,
deleteDirs: true,
disableDeferredWipeout: false,
notFailBuild: true)
}
}
}
}
}
// Functions Only Below here
// Clone the git repo and stash it, so that the jenkins agent machine can grab it later
def gitCloneAndStash() {
git branch: BRANCH, url: "[email protected]:${scriptOrg}/sample-getting-started.git"
stash(name: 'sample-getting-started')
}
// Job Specific Functions
def void doWork(){
// Setup global variables
// Unstash the git repo
unstash(name: 'sample-getting-started')
withMaven(maven: '3.6.3') {
sh "${COMMAND}"
}
}
// EBC Functions
def void ebcDemand(){
buildName executionId
//cleanWs()
git branch: ebcBranch, url:'[email protected]:elastic-build-cloud/ebc-gateway-http'
withCredentials([usernamePassword(credentialsId: 'intranetId', usernameVariable: 'intranetId_USR', passwordVariable: 'intranetId_PSW')]) {
withEnv([
"demandId=${executionId}",
"ebcEnvironment=${ebcBranch}",
"ebc_plan=${ebcPlan}",
"ebc_priority=${ebcPriority}",
"ebc_autoCompleteAfterXHours=24",
"ebc_reasonForEnvironment=${env.BUILD_URL}",
"ebc_jenkins_agent_label=ebc_${executionId}",
"ebc_jenkins_server_instance_name=${env.jenkins_server_instance_name}",
"ebc_jenkins_service_name=${env.jenkins_service_name}"
]){
sh "./ebc_demand.sh"
}
}
stash(name: 'ebc-gateway-http')
}
def void ebcCleanup(){
//cleanWs()
unstash(name: 'ebc-gateway-http')
withCredentials([usernamePassword(credentialsId: 'intranetId', usernameVariable: 'intranetId_USR', passwordVariable: 'intranetId_PSW')]) {
withEnv([
"demandId=${executionId}",
"ebcEnvironment=${ebcBranch}"
]){
sh "./ebc_complete.sh"
}
}
}