|
| 1 | +pipeline { |
| 2 | + agent { |
| 3 | + kubernetes { |
| 4 | + label 'github_deployment' |
| 5 | + defaultContainer 'xc16-mplabx-sonar-fmpp-python' |
| 6 | + yamlFile '.citd/cloudprovider.yml' |
| 7 | + } |
| 8 | + } |
| 9 | + |
| 10 | + |
| 11 | + environment { |
| 12 | + NOTIFICATION_EMAIL = '1f1319de.microchip.com@amer.teams.ms' |
| 13 | + //Update this URL based on where(which organization) it needs to be deployed |
| 14 | + GITHUB_URL ='https://github.com/mchpTestArea' |
| 15 | + //This is the BitBucket repo URL which we want to deploy |
| 16 | + BITBUCKET_URL = 'https://bitbucket.microchip.com/scm/mcu16ce/dspic33c-power-pwm-complementary.git' |
| 17 | + //Branch to be deployed, if multiple branches use comma separated string |
| 18 | + DEPLOY_BRANCH = 'master' |
| 19 | + GITHUB_CREDENTIAL_ID = 'GITHUB_PIC_AVR_TEST_TOKEN' |
| 20 | + CHANGE_LOG_PATH = 'deploy_sandbox/changelog.md' |
| 21 | + SOURCE_PROJECT_META_DATA = '.main-meta/main.json' |
| 22 | + DEPLOY_TOOL_URL = 'https://bitbucket.microchip.com/scm/citd/tool-github-deploy.git' |
| 23 | + } |
| 24 | + |
| 25 | + options { |
| 26 | + timestamps() |
| 27 | + timeout(time: 20, unit: 'MINUTES') |
| 28 | + } |
| 29 | + |
| 30 | + stages { |
| 31 | + |
| 32 | + stage('Checkout') { |
| 33 | + steps { |
| 34 | + checkout scm |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + stage('Build') { |
| 39 | + steps { |
| 40 | + script { |
| 41 | + execute("git clone https://bitbucket.microchip.com/scm/citd/tool-mplabx-c-build.git") |
| 42 | + execute("cd ./tool-mplabx-c-build && node buildLauncher.js sp=../ rp=./output genMK=true") |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Cloning the tool used for GitHub deployment |
| 48 | + stage('GitHub tool clone'){ |
| 49 | + steps{ |
| 50 | + script{ |
| 51 | + execute("git clone https://bitbucket.microchip.com/scm/citd/tool-github-deploy.git") |
| 52 | + execute("chmod +x ./tool-github-deploy/tool-github-deploy/tool-github-deploy.py") |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // GitHub repo creation |
| 58 | + stage('GitHub Repo Creation'){ |
| 59 | + when { |
| 60 | + anyOf { |
| 61 | + expression { !env.GITHUB_URL.contains('microchip-pic-avr-examples') } |
| 62 | + allOf { |
| 63 | + not { changeRequest() } |
| 64 | + branch 'master' |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + steps{ |
| 69 | + script{ |
| 70 | + def jsonObj = readJsonObject(); |
| 71 | + |
| 72 | + String [] topics = jsonObj.content.keywords |
| 73 | + def asString = topics.join(", ") |
| 74 | + asString = asString.replace("\n", "").replace(" ", "").replace("\t", "").replace("\r", "") |
| 75 | + Boolean visibility = true |
| 76 | + if(env.GITHUB_URL.contains('microchip-pic-avr-examples')) |
| 77 | + { |
| 78 | + visibility = false |
| 79 | + } |
| 80 | + |
| 81 | + withCredentials([usernamePassword(credentialsId: "${env.GITHUB_CREDENTIAL_ID}", passwordVariable: 'PASS', usernameVariable: 'USER_NAME')]) { |
| 82 | + execute("python tool-github-deploy/tool-github-deploy/tool-github-deploy.py -rpo=true -gpat=$PASS -rporg=${env.GITHUB_URL} -rpn=${jsonObj.content.projectName} -rpd=\"${jsonObj.content.shortDescription}\" -rpt=${asString} -rpp=${visibility}") |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Deploying the code to GitHub |
| 89 | + stage('GitHub Deploy Source'){ |
| 90 | + when { |
| 91 | + anyOf { |
| 92 | + expression { !env.GITHUB_URL.contains('microchip-pic-avr-examples') } |
| 93 | + allOf { |
| 94 | + not { changeRequest() } |
| 95 | + branch 'master' |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + steps{ |
| 100 | + script{ |
| 101 | + def jsonObj = readJsonObject(); |
| 102 | + def gitHubUrl = "${env.GITHUB_URL}" + "/" + jsonObj.content.projectName |
| 103 | + gitHubUrl = gitHubUrl.replace(" ", "").replace("\n", "") |
| 104 | + Boolean visibility = true |
| 105 | + if(env.GITHUB_URL.contains('microchip-pic-avr-examples')) |
| 106 | + { |
| 107 | + visibility = false |
| 108 | + } |
| 109 | + |
| 110 | + withCredentials([usernamePassword(credentialsId: "${env.GITHUB_CREDENTIAL_ID}", passwordVariable: 'PASS', usernameVariable: 'USER_NAME')]) { |
| 111 | + // If the organization is "mchpTestArea" change the visibility to public before deploy |
| 112 | + if(visibility == true) |
| 113 | + { |
| 114 | + execute("python tool-github-deploy/tool-github-deploy/tool-github-deploy.py -rpu=true -gpat=$PASS -rporg=${env.GITHUB_URL} -rpn=${jsonObj.content.projectName} -rpp=false") |
| 115 | + } |
| 116 | + |
| 117 | + execute("python tool-github-deploy/tool-github-deploy/tool-github-deploy.py -deploy=true -gpat=$PASS -dgid=$USER_NAME -dburl=${env.BITBUCKET_URL} -dgurl=${gitHubUrl} -dbranch=${env.DEPLOY_BRANCH}") |
| 118 | + |
| 119 | + // If the organization is "mchpTestArea" change the visibility to private after deploy |
| 120 | + if(visibility == true) |
| 121 | + { |
| 122 | + execute("python tool-github-deploy/tool-github-deploy/tool-github-deploy.py -rpu=true -gpat=$PASS -rporg=${env.GITHUB_URL} -rpn=${jsonObj.content.projectName} -rpp=true") |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Creating GitHub release |
| 130 | + stage('GitHub release'){ |
| 131 | + when { |
| 132 | + anyOf { |
| 133 | + expression { !env.GITHUB_URL.contains('microchip-pic-avr-examples') } |
| 134 | + allOf { |
| 135 | + not { changeRequest() } |
| 136 | + branch 'master' |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + steps{ |
| 141 | + script{ |
| 142 | + def jsonObj = readJsonObject(); |
| 143 | + |
| 144 | + withCredentials([usernamePassword(credentialsId: "${env.GITHUB_CREDENTIAL_ID}", passwordVariable: 'PASS', usernameVariable: 'USER_NAME')]) { |
| 145 | + execute("python tool-github-deploy/tool-github-deploy/tool-github-deploy.py -rlo=true -gpat=$PASS -rpn=\"${jsonObj.content.projectName}\" -rltv=\"v${jsonObj.content.version}\" -rltt=\"${jsonObj.content.version}\" -rlnp=${env.CHANGE_LOG_PATH}") |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + post { |
| 154 | + success{ |
| 155 | + script { |
| 156 | + if (!"${env.CHANGE_AUTHOR_EMAIL}".equalsIgnoreCase("null")) { |
| 157 | + archiveArtifacts artifacts: "tool-mplabx-c-build/output/**", fingerprint: true |
| 158 | + mail to: "${env.CHANGE_AUTHOR_EMAIL}, ${env.NOTIFICATION_EMAIL}", |
| 159 | + subject: "Successful Pipeline: ${currentBuild.fullDisplayName}", |
| 160 | + body: "Something is right with ${env.BUILD_URL}" |
| 161 | + } else { |
| 162 | + archiveArtifacts artifacts: "tool-mplabx-c-build/output/**", fingerprint: true |
| 163 | + mail to: "${env.NOTIFICATION_EMAIL}", |
| 164 | + subject: "Successful Pipeline: ${currentBuild.fullDisplayName}", |
| 165 | + body: "Something is right with ${env.BUILD_URL}" |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + failure { |
| 170 | + script { |
| 171 | + if (!"${env.CHANGE_AUTHOR_EMAIL}".equalsIgnoreCase("null")) { |
| 172 | + //archiveArtifacts artifacts: "tool-mplabx-c-build/output/**", fingerprint: true |
| 173 | + mail to: "${env.CHANGE_AUTHOR_EMAIL}, ${env.NOTIFICATION_EMAIL}", |
| 174 | + subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", |
| 175 | + body: "Pipeline failure. ${env.BUILD_URL}" |
| 176 | + } else { |
| 177 | + //archiveArtifacts artifacts: "tool-mplabx-c-build/output/**", fingerprint: true |
| 178 | + mail to: "${env.NOTIFICATION_EMAIL}", |
| 179 | + subject: "Failed Pipeline: ${currentBuild.fullDisplayName}", |
| 180 | + body: "Pipeline failure. ${env.BUILD_URL}" |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +def execute(String command) { |
| 188 | + if (isUnix()) { |
| 189 | + sh command |
| 190 | + } else { |
| 191 | + bat command |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +String readJsonObject() { |
| 196 | + def jsonObj = readJSON interpolate: true, file: "${env.SOURCE_PROJECT_META_DATA}" |
| 197 | + return jsonObj |
| 198 | +} |
0 commit comments