prepare-appveyor.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. const { Octokit } = require('@octokit/rest');
  2. const got = require('got');
  3. const assert = require('node:assert');
  4. const fs = require('node:fs');
  5. const path = require('node:path');
  6. const { handleGitCall, ELECTRON_DIR } = require('./lib/utils.js');
  7. const octokit = new Octokit();
  8. const APPVEYOR_IMAGES_URL = 'https://ci.appveyor.com/api/build-clouds';
  9. const APPVEYOR_JOB_URL = 'https://ci.appveyor.com/api/builds';
  10. const ROLLER_BRANCH_PATTERN = /^roller\/chromium$/;
  11. const DEFAULT_BUILD_CLOUD_ID = '1598';
  12. const DEFAULT_BUILD_CLOUD = 'electronhq-16-core';
  13. const DEFAULT_BAKE_BASE_IMAGE = 'base-bake-image';
  14. const DEFAULT_BUILD_IMAGE = 'base-bake-image';
  15. const appveyorBakeJob = 'electron-bake-image';
  16. const appVeyorJobs = {
  17. 'electron-x64': 'electron-x64-testing',
  18. 'electron-woa': 'electron-woa-testing',
  19. 'electron-ia32': 'electron-ia32-testing'
  20. };
  21. async function makeRequest ({ auth, username, password, url, headers, body, method }) {
  22. const clonedHeaders = {
  23. ...(headers || {})
  24. };
  25. if (auth?.bearer) {
  26. clonedHeaders.Authorization = `Bearer ${auth.bearer}`;
  27. }
  28. const options = {
  29. headers: clonedHeaders,
  30. body,
  31. method
  32. };
  33. if (username || password) {
  34. options.username = username;
  35. options.password = password;
  36. }
  37. const response = await got(url, options);
  38. if (response.statusCode < 200 || response.statusCode >= 300) {
  39. console.error('Error: ', `(status ${response.statusCode})`, response.body);
  40. throw new Error(`Unexpected status code ${response.statusCode} from ${url}`);
  41. }
  42. return JSON.parse(response.body);
  43. }
  44. async function checkAppVeyorImage (options) {
  45. const IMAGE_URL = `${APPVEYOR_IMAGES_URL}/${options.cloudId}`;
  46. const requestOpts = {
  47. url: IMAGE_URL,
  48. auth: {
  49. bearer: process.env.APPVEYOR_TOKEN
  50. },
  51. headers: {
  52. 'Content-Type': 'application/json'
  53. },
  54. method: 'GET'
  55. };
  56. try {
  57. const { settings } = await makeRequest(requestOpts);
  58. const { cloudSettings } = settings;
  59. return cloudSettings.images.find(image => image.name === `${options.imageVersion}`) || null;
  60. } catch (err) {
  61. if (err.response?.body) {
  62. console.error('Could not call AppVeyor: ', {
  63. statusCode: err.response.statusCode,
  64. body: JSON.parse(err.response.body)
  65. });
  66. } else {
  67. console.error('Error calling AppVeyor:', err);
  68. }
  69. }
  70. }
  71. async function getPullRequestId (targetBranch) {
  72. const prsForBranch = await octokit.pulls.list({
  73. owner: 'electron',
  74. repo: 'electron',
  75. state: 'open',
  76. head: `electron:${targetBranch}`
  77. });
  78. if (prsForBranch.data.length === 1) {
  79. return prsForBranch.data[0].number;
  80. } else {
  81. return null;
  82. }
  83. }
  84. function useAppVeyorImage (targetBranch, options) {
  85. const validJobs = Object.keys(appVeyorJobs);
  86. if (options.job) {
  87. assert(validJobs.includes(options.job), `Unknown AppVeyor CI job name: ${options.job}. Valid values are: ${validJobs}.`);
  88. callAppVeyorBuildJobs(targetBranch, options.job, options);
  89. } else {
  90. for (const job of validJobs) {
  91. callAppVeyorBuildJobs(targetBranch, job, options);
  92. }
  93. }
  94. }
  95. async function callAppVeyorBuildJobs (targetBranch, job, options) {
  96. console.log(`Using AppVeyor image ${options.version} for ${job}`);
  97. const pullRequestId = await getPullRequestId(targetBranch);
  98. const environmentVariables = {
  99. APPVEYOR_BUILD_WORKER_CLOUD: DEFAULT_BUILD_CLOUD,
  100. APPVEYOR_BUILD_WORKER_IMAGE: options.version,
  101. ELECTRON_OUT_DIR: 'Default',
  102. ELECTRON_ENABLE_STACK_DUMPING: 1,
  103. ELECTRON_ALSO_LOG_TO_STDERR: 1,
  104. DEPOT_TOOLS_WIN_TOOLCHAIN: 0,
  105. PYTHONIOENCODING: 'UTF-8'
  106. };
  107. const requestOpts = {
  108. url: APPVEYOR_JOB_URL,
  109. auth: {
  110. bearer: process.env.APPVEYOR_TOKEN
  111. },
  112. headers: {
  113. 'Content-Type': 'application/json'
  114. },
  115. body: JSON.stringify({
  116. accountName: 'electron-bot',
  117. projectSlug: appVeyorJobs[job],
  118. branch: targetBranch,
  119. pullRequestId: pullRequestId || undefined,
  120. commitId: options.commit || undefined,
  121. environmentVariables
  122. }),
  123. method: 'POST'
  124. };
  125. try {
  126. const { version } = await makeRequest(requestOpts);
  127. const buildUrl = `https://ci.appveyor.com/project/electron-bot/${appVeyorJobs[job]}/build/${version}`;
  128. console.log(`AppVeyor CI request for ${job} successful. Check status at ${buildUrl}`);
  129. } catch (err) {
  130. console.log('Could not call AppVeyor: ', err);
  131. }
  132. }
  133. async function bakeAppVeyorImage (targetBranch, options) {
  134. console.log(`Baking a new AppVeyor image for ${options.version}, on build cloud ${options.cloudId}`);
  135. const environmentVariables = {
  136. APPVEYOR_BUILD_WORKER_CLOUD: DEFAULT_BUILD_CLOUD,
  137. APPVEYOR_BUILD_WORKER_IMAGE: DEFAULT_BAKE_BASE_IMAGE,
  138. APPVEYOR_BAKE_IMAGE: options.version
  139. };
  140. const requestOpts = {
  141. url: APPVEYOR_JOB_URL,
  142. auth: {
  143. bearer: process.env.APPVEYOR_TOKEN
  144. },
  145. headers: {
  146. 'Content-Type': 'application/json'
  147. },
  148. body: JSON.stringify({
  149. accountName: 'electron-bot',
  150. projectSlug: appveyorBakeJob,
  151. branch: targetBranch,
  152. commitId: options.commit || undefined,
  153. environmentVariables
  154. }),
  155. method: 'POST'
  156. };
  157. try {
  158. const { version } = await makeRequest(requestOpts);
  159. const bakeUrl = `https://ci.appveyor.com/project/electron-bot/${appveyorBakeJob}/build/${version}`;
  160. console.log(`AppVeyor image bake request for ${options.version} successful. Check bake status at ${bakeUrl}`);
  161. } catch (err) {
  162. console.log('Could not call AppVeyor: ', err);
  163. }
  164. }
  165. async function prepareAppVeyorImage (opts) {
  166. const branch = await handleGitCall(['rev-parse', '--abbrev-ref', 'HEAD'], ELECTRON_DIR);
  167. if (ROLLER_BRANCH_PATTERN.test(branch)) {
  168. useAppVeyorImage(branch, { ...opts, version: DEFAULT_BUILD_IMAGE, cloudId: DEFAULT_BUILD_CLOUD_ID });
  169. } else {
  170. const versionRegex = /chromium_version':\n +'(.+?)',/m;
  171. const deps = fs.readFileSync(path.resolve(__dirname, '..', 'DEPS'), 'utf8');
  172. const [, CHROMIUM_VERSION] = versionRegex.exec(deps);
  173. const cloudId = opts.cloudId || DEFAULT_BUILD_CLOUD_ID;
  174. const imageVersion = opts.imageVersion || `e-${CHROMIUM_VERSION}`;
  175. const image = await checkAppVeyorImage({ cloudId, imageVersion });
  176. if (image && image.name) {
  177. console.log(`Image exists for ${image.name}. Continuing AppVeyor jobs using ${cloudId}.\n`);
  178. } else {
  179. console.log(`No AppVeyor image found for ${imageVersion} in ${cloudId}.
  180. Creating new image for ${imageVersion}, using Chromium ${CHROMIUM_VERSION} - job will run after image is baked.`);
  181. await bakeAppVeyorImage(branch, { ...opts, version: imageVersion, cloudId });
  182. // write image to temp file if running on CI
  183. if (process.env.CI) fs.writeFileSync('./image_version.txt', imageVersion);
  184. }
  185. }
  186. }
  187. module.exports = prepareAppVeyorImage;
  188. // Load or bake AppVeyor images for Windows CI.
  189. // Usage: prepare-appveyor.js [--cloudId=CLOUD_ID] [--appveyorJobId=xxx] [--imageVersion=xxx]
  190. // [--commit=sha] [--branch=branch_name]
  191. if (require.main === module) {
  192. const args = require('minimist')(process.argv.slice(2));
  193. prepareAppVeyorImage(args)
  194. .catch((err) => {
  195. console.error(err);
  196. process.exit(1);
  197. });
  198. }