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
// Set LABEL variable if empty or not declared
try{ if(LABEL.trim() == "") {throw new Exception();} }catch(Exception e){LABEL="onnx && ci"}; echo "${LABEL}"
try{ if(BRANCH.trim() == "") {throw new Exception();} }catch(Exception e){BRANCH="master"}; echo "${BRANCH}"
if(DOCKER_REGISTRY.trim() == "") {throw new Exception("No Docker registry specified!");}
// CI settings and constants
PROJECT_NAME = "ngraph_cpp"
CI_ROOT = ".ci/onnx/jenkins"
DOCKER_CONTAINER_NAME = "jenkins_ngraph-onnx_ci"
NGRAPH_GIT_ADDRESS = "https://github.com/NervanaSystems/ngraph.git"
JENKINS_GITHUB_CREDENTIAL_ID = "7157091e-bc04-42f0-99fd-dc4da2922a55"
def cloneRepository(String jenkins_github_credential_id, String ngraph_git_address) {
stage('Clone Repo') {
checkout([$class: 'GitSCM',
branches: [[name: "${BRANCH}"]],
doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', timeout: 30]], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: "${jenkins_github_credential_id}",
url: "${ngraph_git_address}"]]])
}
}
def buildImage(configurationMaps) {
Closure buildMethod = { configMap ->
sh """
${CI_ROOT}/utils/docker.sh build \
--docker_registry=${DOCKER_REGISTRY} \
--name=${configMap["projectName"]} \
--version=${configMap["name"]} \
--dockerfile_path=${configMap["dockerfilePath"]} || return 1
"""
}
UTILS.createStage("Build_image", buildMethod, configurationMaps)
}
def pushImage(configurationMaps) {
Closure pushMethod = { configMap ->
UTILS.propagateStatus("Build_image", configMap["name"])
withCredentials([usernamePassword(credentialsId: "${DOCKER_CREDENTIALS}",
usernameVariable: 'DOCKER_USERNAME',
passwordVariable: 'DOCKER_PASSWORD')]) {
sh """
docker login ${DOCKER_REGISTRY} --username ${DOCKER_USERNAME} --password-stdin <<< \${DOCKER_PASSWORD}
${CI_ROOT}/utils/docker.sh push \
--docker_registry=${DOCKER_REGISTRY} \
--name=${configMap["projectName"]} \
--version=${configMap["name"]} || return 1
"""
}
}
UTILS.createStage("Push_image", pushMethod, configurationMaps)
}
def cleanup(configurationMaps) {
Closure cleanupMethod = { configMap ->
sh """
rm -rf ${WORKSPACE}/${BUILD_NUMBER}
"""
}
UTILS.createStage("Cleanup", cleanupMethod, configurationMaps)
}
def main(String label, String projectName, String projectRoot, String dockerContainerName, String jenkins_github_credential_id, String ngraph_git_address) {
node(label) {
timeout(activity: true, time: 15) {
WORKDIR = "${WORKSPACE}/${BUILD_NUMBER}"
def configurationMaps;
try {
dir ("${WORKDIR}") {
cloneRepository(jenkins_github_credential_id, ngraph_git_address)
// Load CI API
UTILS = load "${CI_ROOT}/utils/utils.groovy"
result = 'SUCCESS'
// Create configuration maps
configurationMaps = UTILS.getDockerEnvList(projectName, dockerContainerName, projectRoot)
// Build and push base images
buildImage(configurationMaps)
pushImage(configurationMaps)
}
}
catch(e) {
// Set result to ABORTED if exception contains exit code of a process interrupted by SIGTERM
if ("$e".contains("143")) {
currentBuild.result = "ABORTED"
} else {
currentBuild.result = "FAILURE"
}
}
finally {
cleanup(configurationMaps)
}
}
}
}
main(LABEL, PROJECT_NAME, CI_ROOT, DOCKER_CONTAINER_NAME, JENKINS_GITHUB_CREDENTIAL_ID, NGRAPH_GIT_ADDRESS)