ci-release-build.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. if (!process.env.CI) require('dotenv-safe').load()
  2. const assert = require('assert')
  3. const request = require('request')
  4. const BUILD_APPVEYOR_URL = 'https://ci.appveyor.com/api/builds'
  5. const CIRCLECI_PIPELINE_URL = 'https://circleci.com/api/v2/project/gh/electron/electron/pipeline'
  6. const VSTS_URL = 'https://github.visualstudio.com/electron/_apis/build'
  7. const CIRCLECI_WAIT_TIME = process.env.CIRCLECI_WAIT_TIME || 30000
  8. const appVeyorJobs = {
  9. 'electron-x64': 'electron-x64-release',
  10. 'electron-ia32': 'electron-ia32-release',
  11. 'electron-woa': 'electron-woa-release'
  12. }
  13. const circleCIJobs = [
  14. 'linux-arm-publish',
  15. 'linux-arm64-publish',
  16. 'linux-ia32-publish',
  17. 'linux-x64-publish',
  18. 'mas-publish',
  19. 'osx-publish'
  20. ]
  21. const vstsArmJobs = [
  22. 'electron-arm-testing',
  23. 'electron-arm64-testing',
  24. 'electron-woa-testing'
  25. ]
  26. let jobRequestedCount = 0
  27. async function makeRequest (requestOptions, parseResponse) {
  28. return new Promise((resolve, reject) => {
  29. request(requestOptions, (err, res, body) => {
  30. if (!err && res.statusCode >= 200 && res.statusCode < 300) {
  31. if (parseResponse) {
  32. const build = JSON.parse(body)
  33. resolve(build)
  34. } else {
  35. resolve(body)
  36. }
  37. } else {
  38. console.error('Error occurred while requesting:', requestOptions.url)
  39. if (parseResponse) {
  40. try {
  41. console.log('Error: ', `(status ${res.statusCode})`, err || JSON.parse(res.body))
  42. } catch (err) {
  43. console.log('Error: ', `(status ${res.statusCode})`, res.body)
  44. }
  45. } else {
  46. console.log('Error: ', `(status ${res.statusCode})`, err || res.body)
  47. }
  48. reject(err)
  49. }
  50. })
  51. })
  52. }
  53. async function circleCIcall (targetBranch, job, options) {
  54. console.log(`Triggering CircleCI to run build job: ${job} on branch: ${targetBranch} with release flag.`)
  55. const buildRequest = {
  56. 'branch': targetBranch,
  57. 'parameters': {
  58. 'run-lint': false,
  59. 'run-build-linux': false,
  60. 'run-build-mac': false
  61. }
  62. }
  63. if (options.ghRelease) {
  64. buildRequest.parameters['upload-to-s3'] = '0'
  65. } else {
  66. buildRequest.parameters['upload-to-s3'] = '1'
  67. }
  68. buildRequest.parameters[`run-${job}`] = true
  69. jobRequestedCount++
  70. // The logic below expects that the CircleCI workflows for releases each
  71. // contain only one job in order to maintain compatibility with sudowoodo.
  72. // If the workflows are changed in the CircleCI config.yml, this logic will
  73. // also need to be changed as well as possibly changing sudowoodo.
  74. try {
  75. const circleResponse = await circleCIRequest(CIRCLECI_PIPELINE_URL, 'POST', buildRequest)
  76. console.log(`CircleCI release build pipeline ${circleResponse.id} for ${job} triggered.`)
  77. const pipelineInfoUrl = `https://circleci.com/api/v2/pipeline/${circleResponse.id}`
  78. const workflowId = await getCircleCIWorkflowId(circleResponse.id)
  79. if (workflowId === -1) {
  80. return
  81. }
  82. console.log(`CircleCI release build workflow running at https://circleci.com/workflow-run/${workflowId} for ${job}.`)
  83. const jobNumber = await getCircleCIJobNumber(workflowId)
  84. if (jobNumber === -1) {
  85. return
  86. }
  87. const jobUrl = `https://circleci.com/gh/electron/electron/${jobNumber}`
  88. console.log(`CircleCI release build request for ${job} successful. Check ${jobUrl} for status.`)
  89. } catch (err) {
  90. console.log('Error calling CircleCI: ', err)
  91. }
  92. }
  93. async function getCircleCIWorkflowId (pipelineId) {
  94. const pipelineInfoUrl = `https://circleci.com/api/v2/pipeline/${pipelineId}`
  95. let workflowId = 0
  96. while (workflowId === 0) {
  97. const pipelineInfo = await circleCIRequest(pipelineInfoUrl, 'GET')
  98. switch (pipelineInfo.state) {
  99. case 'created': {
  100. const workflows = await circleCIRequest(`${pipelineInfoUrl}/workflow`, 'GET')
  101. if (workflows.items.length === 1) {
  102. workflowId = workflows.items[0].id
  103. break
  104. }
  105. console.log('Unxpected number of workflows, response was:', pipelineInfo)
  106. workflowId = -1
  107. break
  108. }
  109. case 'error': {
  110. console.log('Error retrieving workflows, response was:', pipelineInfo)
  111. workflowId = -1
  112. break
  113. }
  114. }
  115. await new Promise(resolve => setTimeout(resolve, CIRCLECI_WAIT_TIME))
  116. }
  117. return workflowId
  118. }
  119. async function getCircleCIJobNumber (workflowId) {
  120. const jobInfoUrl = `https://circleci.com/api/v2/workflow/${workflowId}/job`
  121. let jobNumber = 0
  122. while (jobNumber === 0) {
  123. const jobInfo = await circleCIRequest(jobInfoUrl, 'GET')
  124. if (!jobInfo.items) {
  125. continue
  126. }
  127. if (jobInfo.items.length !== 1) {
  128. console.log('Unxpected number of jobs, response was:', jobInfo)
  129. jobNumber = -1
  130. break
  131. }
  132. switch (jobInfo.items[0].status) {
  133. case 'not_running':
  134. case 'queued':
  135. case 'running': {
  136. if (jobInfo.items[0].job_number && !isNaN(jobInfo.items[0].job_number)) {
  137. jobNumber = jobInfo.items[0].job_number
  138. }
  139. break
  140. }
  141. case 'canceled':
  142. case 'error':
  143. case 'infrastructure_fail':
  144. case 'timedout':
  145. case 'not_run':
  146. case 'failed': {
  147. console.log(`Error job returned a status of ${jobInfo.items[0].status}, response was:`, jobInfo)
  148. jobNumber = -1
  149. break
  150. }
  151. }
  152. await new Promise(resolve => setTimeout(resolve, CIRCLECI_WAIT_TIME))
  153. }
  154. return jobNumber
  155. }
  156. async function circleCIRequest (url, method, requestBody) {
  157. return makeRequest({
  158. auth: {
  159. username: process.env.CIRCLE_TOKEN,
  160. password: ''
  161. },
  162. method,
  163. url,
  164. headers: {
  165. 'Content-Type': 'application/json',
  166. 'Accept': 'application/json'
  167. },
  168. body: requestBody ? JSON.stringify(requestBody) : null
  169. }, true).catch(err => {
  170. console.log('Error calling CircleCI:', err)
  171. })
  172. }
  173. function buildAppVeyor (targetBranch, options) {
  174. const validJobs = Object.keys(appVeyorJobs)
  175. if (options.job) {
  176. assert(validJobs.includes(options.job), `Unknown AppVeyor CI job name: ${options.job}. Valid values are: ${validJobs}.`)
  177. callAppVeyor(targetBranch, options.job, options)
  178. } else {
  179. validJobs.forEach((job) => callAppVeyor(targetBranch, job, options))
  180. }
  181. }
  182. async function callAppVeyor (targetBranch, job, options) {
  183. console.log(`Triggering AppVeyor to run build job: ${job} on branch: ${targetBranch} with release flag.`)
  184. const environmentVariables = {
  185. ELECTRON_RELEASE: 1
  186. }
  187. if (!options.ghRelease) {
  188. environmentVariables.UPLOAD_TO_S3 = 1
  189. }
  190. const requestOpts = {
  191. url: BUILD_APPVEYOR_URL,
  192. auth: {
  193. bearer: process.env.APPVEYOR_CLOUD_TOKEN
  194. },
  195. headers: {
  196. 'Content-Type': 'application/json'
  197. },
  198. body: JSON.stringify({
  199. accountName: 'electron-bot',
  200. projectSlug: appVeyorJobs[job],
  201. branch: targetBranch,
  202. environmentVariables
  203. }),
  204. method: 'POST'
  205. }
  206. jobRequestedCount++
  207. const appVeyorResponse = await makeRequest(requestOpts, true).catch(err => {
  208. console.log('Error calling AppVeyor:', err)
  209. })
  210. const buildUrl = `https://ci.appveyor.com/project/electron-bot/${appVeyorJobs[job]}/build/${appVeyorResponse.version}`
  211. console.log(`AppVeyor release build request for ${job} successful. Check build status at ${buildUrl}`)
  212. }
  213. function buildCircleCI (targetBranch, options) {
  214. if (options.job) {
  215. assert(circleCIJobs.includes(options.job), `Unknown CircleCI job name: ${options.job}. Valid values are: ${circleCIJobs}.`)
  216. circleCIcall(targetBranch, options.job, options)
  217. } else {
  218. circleCIJobs.forEach((job) => circleCIcall(targetBranch, job, options))
  219. }
  220. }
  221. async function buildVSTS (targetBranch, options) {
  222. if (options.armTest) {
  223. assert(vstsArmJobs.includes(options.job), `Unknown VSTS CI arm test job name: ${options.job}. Valid values are: ${vstsArmJobs}.`)
  224. }
  225. console.log(`Triggering VSTS to run build on branch: ${targetBranch} with release flag.`)
  226. const environmentVariables = {
  227. ELECTRON_RELEASE: 1
  228. }
  229. if (options.armTest) {
  230. if (options.circleBuildNum) {
  231. environmentVariables.CIRCLE_BUILD_NUM = options.circleBuildNum
  232. } else if (options.appveyorJobId) {
  233. environmentVariables.APPVEYOR_JOB_ID = options.appveyorJobId
  234. }
  235. } else {
  236. if (!options.ghRelease) {
  237. environmentVariables.UPLOAD_TO_S3 = 1
  238. }
  239. }
  240. const requestOpts = {
  241. url: `${VSTS_URL}/definitions?api-version=4.1`,
  242. auth: {
  243. user: '',
  244. password: process.env.VSTS_TOKEN
  245. },
  246. headers: {
  247. 'Content-Type': 'application/json'
  248. }
  249. }
  250. const vstsResponse = await makeRequest(requestOpts, true).catch(err => {
  251. console.log('Error calling VSTS to get build definitions:', err)
  252. })
  253. const buildsToRun = vstsResponse.value.filter(build => build.name === options.job)
  254. buildsToRun.forEach((build) => callVSTSBuild(build, targetBranch, environmentVariables))
  255. }
  256. async function callVSTSBuild (build, targetBranch, environmentVariables) {
  257. const buildBody = {
  258. definition: build,
  259. sourceBranch: targetBranch,
  260. priority: 'high'
  261. }
  262. if (Object.keys(environmentVariables).length !== 0) {
  263. buildBody.parameters = JSON.stringify(environmentVariables)
  264. }
  265. const requestOpts = {
  266. url: `${VSTS_URL}/builds?api-version=4.1`,
  267. auth: {
  268. user: '',
  269. password: process.env.VSTS_TOKEN
  270. },
  271. headers: {
  272. 'Content-Type': 'application/json'
  273. },
  274. body: JSON.stringify(buildBody),
  275. method: 'POST'
  276. }
  277. jobRequestedCount++
  278. const vstsResponse = await makeRequest(requestOpts, true).catch(err => {
  279. console.log(`Error calling VSTS for job ${build.name}`, err)
  280. })
  281. console.log(`VSTS release build request for ${build.name} successful. Check ${vstsResponse._links.web.href} for status.`)
  282. }
  283. function runRelease (targetBranch, options) {
  284. if (options.ci) {
  285. switch (options.ci) {
  286. case 'CircleCI': {
  287. buildCircleCI(targetBranch, options)
  288. break
  289. }
  290. case 'AppVeyor': {
  291. buildAppVeyor(targetBranch, options)
  292. break
  293. }
  294. case 'VSTS': {
  295. buildVSTS(targetBranch, options)
  296. break
  297. }
  298. default: {
  299. console.log(`Error! Unknown CI: ${options.ci}.`)
  300. process.exit(1)
  301. }
  302. }
  303. } else {
  304. buildCircleCI(targetBranch, options)
  305. buildAppVeyor(targetBranch, options)
  306. }
  307. console.log(`${jobRequestedCount} jobs were requested.`)
  308. }
  309. module.exports = runRelease
  310. if (require.main === module) {
  311. const args = require('minimist')(process.argv.slice(2), {
  312. boolean: ['ghRelease', 'armTest']
  313. })
  314. const targetBranch = args._[0]
  315. if (args._.length < 1) {
  316. console.log(`Trigger CI to build release builds of electron.
  317. Usage: ci-release-build.js [--job=CI_JOB_NAME] [--ci=CircleCI|AppVeyor|VSTS]
  318. [--ghRelease] [--armTest] [--circleBuildNum=xxx] [--appveyorJobId=xxx] TARGET_BRANCH
  319. `)
  320. process.exit(0)
  321. }
  322. runRelease(targetBranch, args)
  323. }