1
+ final String cronExpr = env. BRANCH_IS_PRIMARY ? ' @daily' : ' '
2
+
3
+ properties([
4
+ buildDiscarder(logRotator(numToKeepStr : ' 10' )),
5
+ disableConcurrentBuilds(abortPrevious : true ),
6
+ pipelineTriggers([cron(cronExpr)]),
7
+ ])
8
+
1
9
def agentSelector (String imageType ) {
2
10
// Linux agent
3
11
if (imageType == ' linux' ) {
4
- return ' linux'
12
+ // This function is defined in the jenkins-infra/pipeline-library
13
+ if (infra. isTrusted()) {
14
+ return ' linux'
15
+ } else {
16
+ // Need Docker and a LOT of memory for faster builds (due to multi archs) or fallback to linux (trusted.ci)
17
+ return ' docker-highmem'
18
+ }
5
19
}
6
20
// Windows Server Core 2022 agent
7
21
if (imageType. contains(' 2022' )) {
@@ -11,85 +25,85 @@ def agentSelector(String imageType) {
11
25
return ' windows-2019'
12
26
}
13
27
14
- pipeline {
15
- agent none
28
+ // Ref. https://github.com/jenkins-infra/pipeline-library/pull/917
29
+ def spotAgentSelector (String agentLabel , int counter ) {
30
+ // This function is defined in the jenkins-infra/pipeline-library
31
+ if (infra. isTrusted()) {
32
+ // Return early if on trusted (no spot agent)
33
+ return agentLabel
34
+ }
16
35
17
- options {
18
- buildDiscarder(logRotator( daysToKeepStr : ' 10 ' ))
36
+ if (counter > 1 ) {
37
+ return agentLabel + ' && nonspot '
19
38
}
20
39
21
- stages {
22
- stage(' docker-ssh-agent' ) {
23
- environment {
24
- DOCKERHUB_ORGANISATION = " ${ infra.isTrusted() ? 'jenkins' : 'jenkins4eval'} "
25
- }
26
- matrix {
27
- axes {
28
- axis {
29
- name ' IMAGE_TYPE'
30
- values ' linux' , ' nanoserver-1809' , ' nanoserver-ltsc2019' , ' nanoserver-ltsc2022' , ' windowsservercore-ltsc2019' , ' windowsservercore-ltsc2022'
31
- }
32
- }
33
- stages {
34
- stage(' Main' ) {
35
- agent {
36
- label agentSelector(env. IMAGE_TYPE )
37
- }
38
- options {
39
- timeout(time : 60 , unit : ' MINUTES' )
40
- }
41
- stages {
40
+ return agentLabel + ' && spot'
41
+ }
42
+
43
+ // Specify parallel stages
44
+ def parallelStages = [failFast : false ]
45
+ [
46
+ ' linux' ,
47
+ ' nanoserver-1809' ,
48
+ ' nanoserver-ltsc2019' ,
49
+ ' nanoserver-ltsc2022' ,
50
+ ' windowsservercore-1809' ,
51
+ ' windowsservercore-ltsc2019' ,
52
+ ' windowsservercore-ltsc2022'
53
+ ]. each { imageType ->
54
+ parallelStages[imageType] = {
55
+ withEnv([
56
+ " IMAGE_TYPE=${ imageType} " ,
57
+ " REGISTRY_ORG=${ infra.isTrusted() ? 'jenkins' : 'jenkins4eval'} " ,
58
+ ]) {
59
+ int retryCounter = 0
60
+ retry(count : 2 , conditions : [agent(), nonresumable()]) {
61
+ // Use local variable to manage concurrency and increment BEFORE spinning up any agent
62
+ final String resolvedAgentLabel = spotAgentSelector(agentSelector(imageType), retryCounter)
63
+ retryCounter++
64
+ node(resolvedAgentLabel) {
65
+ timeout(time : 60 , unit : ' MINUTES' ) {
66
+ checkout scm
67
+ if (imageType == " linux" ) {
42
68
stage(' Prepare Docker' ) {
43
- when {
44
- environment name : ' IMAGE_TYPE' , value : ' linux'
45
- }
46
- steps {
47
- sh ' make docker-init'
48
- }
69
+ sh ' make docker-init'
49
70
}
50
- stage(' Build and Test' ) {
51
- // This stage is the "CI" and should be run on all code changes triggered by a code change
52
- when {
53
- not { buildingTag() }
54
- }
55
- steps {
56
- script {
57
- if (isUnix()) {
58
- sh ' make build'
59
- sh ' make test'
60
- // If the tests are passing for Linux AMD64, then we can build all the CPU architectures
61
- sh ' make every-build'
71
+ }
72
+ // This function is defined in the jenkins-infra/pipeline-library
73
+ if (infra. isTrusted()) {
74
+ // trusted.ci.jenkins.io builds (e.g. publication to DockerHub)
75
+ stage(' Deploy to DockerHub' ) {
76
+ withEnv([
77
+ " ON_TAG=true" ,
78
+ " VERSION=${ env.TAG_NAME} " ,
79
+ ]) {
80
+ // This function is defined in the jenkins-infra/pipeline-library
81
+ infra. withDockerCredentials {
82
+ if (isUnix()) {
83
+ sh ' make publish'
62
84
} else {
63
- powershell ' & ./build.ps1 test '
85
+ powershell ' & ./build.ps1 publish '
64
86
}
65
87
}
66
88
}
67
- post {
68
- always {
69
- junit(allowEmptyResults : true , keepLongStdio : true , testResults : ' target/**/junit-results.xml' )
70
- }
71
- }
72
89
}
73
- stage(' Deploy to DockerHub' ) {
74
- // This stage is the "CD" and should only be run when a tag triggered the build
75
- when {
76
- buildingTag()
77
- }
78
- environment {
79
- ON_TAG = ' true'
80
- VERSION = " ${ env.TAG_NAME} "
90
+ } else {
91
+ stage(' Build and Test' ) {
92
+ // ci.jenkins.io builds (e.g. no publication)
93
+ if (isUnix()) {
94
+ sh ' make build'
95
+ sh ' make test'
96
+ } else {
97
+ powershell ' & ./build.ps1 test'
98
+ archiveArtifacts artifacts : ' build-windows_*.yaml' , allowEmptyArchive : true
81
99
}
82
- steps {
83
- script {
84
- // This function is defined in the jenkins-infra/pipeline-library
85
- infra. withDockerCredentials {
86
- if (isUnix()) {
87
- sh ' make publish'
88
- } else {
89
- powershell ' & ./build.ps1 publish'
90
- }
91
- }
92
- }
100
+ junit(allowEmptyResults : true , keepLongStdio : true , testResults : ' target/**/junit-results.xml' )
101
+ }
102
+ // If the tests are passing for Linux AMD64, then we can build all the CPU architectures
103
+ if (isUnix()) {
104
+ stage(' Multi-Arch Build' ) {
105
+
106
+ sh ' make every-build'
93
107
}
94
108
}
95
109
}
@@ -100,4 +114,6 @@ pipeline {
100
114
}
101
115
}
102
116
103
- // vim: ft=groovy
117
+ // Execute parallel stages
118
+ parallel parallelStages
119
+ // // vim: ft=groovy
0 commit comments