|
| 1 | +@Library('blossom-github-lib@master') |
| 2 | +import ipp.blossom.* |
| 3 | + |
| 4 | +podTemplate(cloud:'sc-ipp-blossom-prod', yaml : """ |
| 5 | + apiVersion: v1 |
| 6 | + kind: Pod |
| 7 | + spec: |
| 8 | + nodeSelector: |
| 9 | + kubernetes.io/os: linux""", |
| 10 | + containers: [ |
| 11 | + containerTemplate(name: 'golang', image: 'golang:1.12.9', ttyEnabled: true, command: 'cat') |
| 12 | + ]) { |
| 13 | + node(POD_LABEL) { |
| 14 | + def githubHelper |
| 15 | + stage('Get Token') { |
| 16 | + withCredentials([usernamePassword(credentialsId: 'github-token', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { |
| 17 | + // create new instance of helper object |
| 18 | + githubHelper = GithubHelper.getInstance("${GIT_PASSWORD}", githubData) |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + } |
| 23 | + def stageName = '' |
| 24 | + try { |
| 25 | + currentBuild.description = githubHelper.getBuildDescription() |
| 26 | + stageName = 'Code checkout' |
| 27 | + stage(stageName) { |
| 28 | + // update status on github |
| 29 | + githubHelper.updateCommitStatus("$BUILD_URL", "$stageName Running", GitHubCommitState.PENDING) |
| 30 | + if("Open".equalsIgnoreCase(githubHelper.getPRState())){ |
| 31 | + println "PR State is Open" |
| 32 | + // checkout head of pull request |
| 33 | + checkout changelog: true, poll: true, scm: [$class: 'GitSCM', branches: [[name: "pr/"+githubHelper.getPRNumber()]], doGenerateSubmoduleConfigurations: false, |
| 34 | + submoduleCfg: [], |
| 35 | + userRemoteConfigs: [[credentialsId: 'github-token', url: githubHelper.getCloneUrl(), refspec: '+refs/pull/*/head:refs/remotes/origin/pr/*']]] |
| 36 | + } |
| 37 | + else if("Merged".equalsIgnoreCase(githubHelper.getPRState())){ |
| 38 | + println "PR State is Merged" |
| 39 | + // use following if you want to build merged code of the head & base branch |
| 40 | + // ref : https://developer.github.com/v3/pulls/ |
| 41 | + checkout changelog: true, poll: true, scm: [$class: 'GitSCM', branches: [[name: githubHelper.getMergedSHA()]], |
| 42 | + doGenerateSubmoduleConfigurations: false, |
| 43 | + submoduleCfg: [], |
| 44 | + userRemoteConfigs: [[credentialsId: 'github-token', url: githubHelper.getCloneUrl(), refspec: '+refs/pull/*/merge:refs/remotes/origin/pr/*']]] |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + stageName = 'Build' |
| 49 | + stage(stageName) { |
| 50 | + container('golang') { |
| 51 | + githubHelper.updateCommitStatus("$BUILD_URL", "$stageName Running", GitHubCommitState.PENDING) |
| 52 | + //add build steps |
| 53 | + println "Building code" |
| 54 | + sh "sleep 10" |
| 55 | + |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + stageName = 'Test' |
| 60 | + stage(stageName) { |
| 61 | + container('golang') { |
| 62 | + githubHelper.updateCommitStatus("$BUILD_URL", "$stageName Running", GitHubCommitState.PENDING) |
| 63 | + // add test cases here |
| 64 | + println "Running test" |
| 65 | + |
| 66 | + } |
| 67 | + } |
| 68 | + // upload jenkins job logs to github for external users |
| 69 | + // this function remove sensitive data from logs before upload |
| 70 | + // user can provide list of plain guard words (sensitive words) or regular expression for guard words |
| 71 | + // def guardWords = ["gitlab-master.nvidia.com"] |
| 72 | + // |
| 73 | + // githubHelper.uploadLogs(this, env.JOB_NAME, env.BUILD_NUMBER, guardWords, <extraGuardWordRegEx>) |
| 74 | + githubHelper.uploadLogs(this, env.JOB_NAME, env.BUILD_NUMBER, null, null) |
| 75 | + |
| 76 | + // update status on github |
| 77 | + githubHelper.updateCommitStatus("$BUILD_URL", "Complete", GitHubCommitState.SUCCESS) |
| 78 | + } |
| 79 | + catch (Exception ex){ |
| 80 | + currentBuild.result = 'FAILURE' |
| 81 | + println ex |
| 82 | + // upload jenkins job logs to github for external users |
| 83 | + // this function remove sensitive data from logs before upload |
| 84 | + // user can provide list of plain guard words (sensitive words) or regular expression for guard words |
| 85 | + // def guardWords = ["gitlab-master.nvidia.com"] |
| 86 | + // |
| 87 | + // githubHelper.uploadLogs(this, env.JOB_NAME, env.BUILD_NUMBER, guardWords, <extraGuardWordRegEx>) |
| 88 | + githubHelper.uploadLogs(this, env.JOB_NAME, env.BUILD_NUMBER, null, null) |
| 89 | + githubHelper.updateCommitStatus("$BUILD_URL", "$stageName Failed", GitHubCommitState.FAILURE) |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
0 commit comments