prepare-appveyor.js 7.0 KB

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