Jenkinsfile 9.7 KB
Newer Older
1
// INTEL CONFIDENTIAL
2
// Copyright 2018-2019 Intel Corporation All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// The source code contained or described herein and all documents related to the
// source code ("Material") are owned by Intel Corporation or its suppliers or
// licensors. Title to the Material remains with Intel Corporation or its
// suppliers and licensors. The Material may contain trade secrets and proprietary
// and confidential information of Intel Corporation and its suppliers and
// licensors, and is protected by worldwide copyright and trade secret laws and
// treaty provisions. No part of the Material may be used, copied, reproduced,
// modified, published, uploaded, posted, transmitted, distributed, or disclosed
// in any way without Intel's prior express written permission.
// No license under any patent, copyright, trade secret or other intellectual
// property right is granted to or conferred upon you by disclosure or delivery of
// the Materials, either expressly, by implication, inducement, estoppel or
// otherwise. Any license under such intellectual property rights must be express
// and approved by Intel in writing.

// Set LABEL variable if empty or not declared
try{ if(LABEL.trim() == "") {throw new Exception();} }catch(Exception e){LABEL="onnx && ci"}; echo "${LABEL}"
// CI settings and constants
21
PROJECT_NAME = "ngraph-onnx"
22 23
CI_ROOT = "ngraph/.ci/onnx/jenkins"
DOCKER_CONTAINER_NAME = "jenkins_${PROJECT_NAME}_ci"
24 25
NGRAPH_GIT_ADDRESS = "https://github.com/NervanaSystems/ngraph.git"
ONNX_GIT_ADDRESS = "https://github.com/NervanaSystems/ngraph-onnx.git"
26
JENKINS_GITHUB_CREDENTIAL_ID = "7157091e-bc04-42f0-99fd-dc4da2922a55"
27 28 29 30 31
ONNX_BRANCH = "$CHANGE_BRANCH"
GIT_PR_AUTHOR_EMAIL=""
GIT_COMMIT_AUTHOR_EMAIL=""
GIT_COMMIT_HASH=""
GIT_COMMIT_SUBJECT=""
32

33 34 35 36 37 38 39 40 41
// workaround for aborting previous builds on PR update
// TODO: Move to plugin based solution as soon as it's available
@NonCPS
def killPreviousRunningJobs() {
    def jobname = env.JOB_NAME
    def buildnum = env.BUILD_NUMBER.toInteger()

    def job = Jenkins.instance.getItemByFullName(jobname)
    for (build in job.builds) {
42 43
        if (!build.isBuilding()){
            continue;
44
        }
45
        if (buildnum == build.getNumber().toInteger()){
46 47 48 49 50 51 52 53
            continue;
        }
        echo "Kill task = ${build}"
        build.doStop();
    }
}

def cloneRepository(String jenkins_github_credential_id, String ngraph_git_address, String onnx_git_address) {
54 55
    stage('Clone Repos') {
        try {
56
            sh "git clone $onnx_git_address -b $CHANGE_BRANCH ."
57 58
        }
        catch (Exception e) {
59
            ONNX_BRANCH = "master"
60
            sh """
61
                echo "WARNING! Failed to clone ngraph-onnx branch $CHANGE_BRANCH ! Falling back to master."
62
                echo "EXCEPTION: $e"
63 64 65 66 67 68 69 70 71
            """
            checkout([$class: 'GitSCM',
                branches: [[name: "master"]],
                doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', timeout: 30]], submoduleCfg: [],
                userRemoteConfigs: [[credentialsId: "${jenkins_github_credential_id}",
                url: "${onnx_git_address}"]]])
        }
        dir ("ngraph") {
                checkout([$class: 'GitSCM',
72
                    branches: [[name: "$CHANGE_BRANCH"]],
73 74 75
                    doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', timeout: 30]], submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: "${jenkins_github_credential_id}",
                    url: "${ngraph_git_address}"]]])
76 77 78 79
                GIT_PR_AUTHOR_EMAIL = sh (script: 'git log -1 --pretty="format:%ae" ', returnStdout: true).trim()
                GIT_COMMIT_AUTHOR_EMAIL = sh (script: 'git log -1 --pretty="format:%ce" ', returnStdout: true).trim()
                GIT_COMMIT_HASH = sh (script: 'git log -1 --pretty="format:%H" ', returnStdout: true).trim()
                GIT_COMMIT_SUBJECT = sh (script: 'git log -1 --pretty="format:%s" ', returnStdout: true).trim()
80 81 82 83
        }
    }
}

84
def buildImage(configurationMaps) {
85 86 87 88 89 90 91 92
    Closure buildMethod = { configMap ->
        sh """
            ${CI_ROOT}/utils/docker.sh build \
                                --name=${configMap["projectName"]} \
                                --version=${configMap["name"]} \
                                --dockerfile_path=${configMap["dockerfilePath"]}
        """
    }
93
    UTILS.createStage("Build_image", buildMethod, configurationMaps)
94 95
}

96
def runDockerContainers(configurationMaps) {
97
    Closure runContainerMethod = { configMap ->
98
        UTILS.propagateStatus("Build_image", configMap["name"])
99 100 101 102 103 104 105 106 107
        sh """
            mkdir -p ${HOME}/ONNX_CI
            ${CI_ROOT}/utils/docker.sh start \
                                --name=${configMap["projectName"]} \
                                --version=${configMap["name"]} \
                                --container_name=${configMap["dockerContainerName"]} \
                                --volumes="-v ${WORKSPACE}/${BUILD_NUMBER}:/logs -v ${HOME}/ONNX_CI:/home -v ${WORKDIR}:/root"
        """
    }
108
    UTILS.createStage("Run_docker_containers", runContainerMethod, configurationMaps)
109 110
}

111
def prepareEnvironment(configurationMaps) {
112
    Closure prepareEnvironmentMethod = { configMap ->
113
        UTILS.propagateStatus("Run_docker_containers", configMap["dockerContainerName"])
114 115
        sh """
            docker cp ${CI_ROOT}/utils/docker.sh ${configMap["dockerContainerName"]}:/home
116
            docker exec ${configMap["dockerContainerName"]} bash -c "/root/${CI_ROOT}/prepare_environment.sh"
117 118
        """
    }
119
    UTILS.createStage("Prepare_environment", prepareEnvironmentMethod, configurationMaps)
120 121
}

122
def runToxTests(configurationMaps) {
123
    Closure runToxTestsMethod = { configMap ->
124
        UTILS.propagateStatus("Prepare_environment", configMap["dockerContainerName"])
125 126 127 128 129
        sh """
            NGRAPH_WHL=\$(docker exec ${configMap["dockerContainerName"]} find /root/ngraph/python/dist/ -name 'ngraph*.whl')
            docker exec -e TOX_INSTALL_NGRAPH_FROM=\${NGRAPH_WHL} ${configMap["dockerContainerName"]} tox -c /root/
        """
    }
130
    UTILS.createStage("Run_tox_tests", runToxTestsMethod, configurationMaps)
131 132
}

133
def cleanup(configurationMaps) {
134 135 136 137 138 139 140 141 142 143 144
    Closure cleanupMethod = { configMap ->
        sh """
            cd ${HOME}/ONNX_CI
            ./docker.sh chmod --container_name=${configMap["dockerContainerName"]} --directory="/logs" --options="-R 777" || true
            ./docker.sh stop --container_name=${configMap["dockerContainerName"]} || true
            ./docker.sh remove --container_name=${configMap["dockerContainerName"]} || true
            ./docker.sh clean_up || true
            rm ${HOME}/ONNX_CI/docker.sh
            rm -rf ${WORKSPACE}/${BUILD_NUMBER}
        """
    }
145
    UTILS.createStage("Cleanup", cleanupMethod, configurationMaps)
146 147
}

148
def notifyByEmail() {
149 150
    configurationMaps = []
    configurationMaps.add([
151
        "name": "Notify"
152
    ])
153
    String notifyPeople = "$GIT_PR_AUTHOR_EMAIL, $GIT_COMMIT_AUTHOR_EMAIL"
154
    Closure notifyMethod = { configMap ->
155
        if(currentBuild.result == "FAILURE") {
156
            emailext (
157
                subject: "NGraph-Onnx CI: NGraph PR $CHANGE_ID $currentBuild.result!",
158 159 160
                body: """
                    <table style="width:100%">
                        <tr><td>Status:</td> <td>${currentBuild.result}</td></tr>
161 162 163 164 165 166
                        <tr><td>Pull Request Title:</td> <td>$CHANGE_TITLE</td></tr>
                        <tr><td>Pull Request:</td> <td><a href=$CHANGE_URL>$CHANGE_ID</a> </td></tr>
                        <tr><td>Branch:</td> <td>$CHANGE_BRANCH</td></tr>
                        <tr><td>Commit Hash:</td> <td>$GIT_COMMIT_SUBJECT</td></tr>
                        <tr><td>Commit Subject:</td> <td>$GIT_COMMIT_HASH</td></tr>
                        <tr><td>Jenkins Build:</td> <td> <a href=$RUN_DISPLAY_URL> ${BUILD_NUMBER} </a> </td></tr>
167
                        <tr><td>nGraph-ONNX Branch:</td> <td>${ONNX_BRANCH}</td></tr>
168 169 170 171 172 173
                    </table>
                """,
                to: "${notifyPeople}"
            )
        }
    }
174
    UTILS.createStage("Notify", notifyMethod, configurationMaps)
175 176 177
}

def main(String label, String projectName, String projectRoot, String dockerContainerName, String jenkins_github_credential_id, String ngraph_git_address, String onnx_git_address) {
178
    killPreviousRunningJobs()
179
    node(label) {
180
        timeout(activity: true, time: 15) {
181 182 183 184
            WORKDIR = "${WORKSPACE}/${BUILD_NUMBER}"
            def configurationMaps;
            try {
                dir ("${WORKDIR}") {
185
                    cloneRepository(jenkins_github_credential_id, ngraph_git_address, onnx_git_address)
186 187 188 189
                    // Load CI API
                    UTILS = load "${CI_ROOT}/utils/utils.groovy"
                    result = 'SUCCESS'
                    // Create configuration maps
190
                    configurationMaps = UTILS.getDockerEnvList(projectName, dockerContainerName, projectRoot)
191
                    // Execute CI steps
192 193 194 195
                    buildImage(configurationMaps)
                    runDockerContainers(configurationMaps)
                    prepareEnvironment(configurationMaps)
                    runToxTests(configurationMaps)
196 197
                }
            }
198 199 200 201 202 203 204 205
            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"
                }
            }
206
            finally {
207 208
                cleanup(configurationMaps)
                notifyByEmail()
209 210 211 212 213 214
            }
        }
    }
}

main(LABEL, PROJECT_NAME, CI_ROOT, DOCKER_CONTAINER_NAME, JENKINS_GITHUB_CREDENTIAL_ID, NGRAPH_GIT_ADDRESS, ONNX_GIT_ADDRESS)