run-release-ci-jobs.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { Octokit } from '@octokit/rest';
  2. import * as assert from 'node:assert';
  3. import { createGitHubTokenStrategy } from './github-token';
  4. import { ELECTRON_ORG, ELECTRON_REPO } from './types';
  5. const octokit = new Octokit({
  6. authStrategy: createGitHubTokenStrategy(ELECTRON_REPO)
  7. });
  8. const GH_ACTIONS_PIPELINE_URL = 'https://github.com/electron/electron/actions';
  9. const GH_ACTIONS_WAIT_TIME = process.env.GH_ACTIONS_WAIT_TIME ? parseInt(process.env.GH_ACTIONS_WAIT_TIME, 10) : 30000;
  10. const ghActionsPublishWorkflows = [
  11. 'linux-publish',
  12. 'macos-publish',
  13. 'windows-publish'
  14. ] as const;
  15. let jobRequestedCount = 0;
  16. type GitHubActionsCallOptions = {
  17. ghRelease?: boolean;
  18. newVersion: string;
  19. runningPublishWorkflows?: boolean;
  20. }
  21. async function githubActionsCall (targetBranch: string, workflowName: string, options: GitHubActionsCallOptions) {
  22. console.log(`Triggering GitHub Actions to run build job: ${workflowName} on branch: ${targetBranch} with release flag.`);
  23. const buildRequest = {
  24. branch: targetBranch,
  25. parameters: {} as Record<string, string | boolean>
  26. };
  27. if (options.ghRelease) {
  28. buildRequest.parameters['upload-to-storage'] = '0';
  29. } else {
  30. buildRequest.parameters['upload-to-storage'] = '1';
  31. }
  32. buildRequest.parameters[`run-${workflowName}`] = true;
  33. jobRequestedCount++;
  34. try {
  35. const commits = await octokit.repos.listCommits({
  36. owner: ELECTRON_ORG,
  37. repo: ELECTRON_REPO,
  38. sha: targetBranch,
  39. per_page: 5
  40. });
  41. if (!commits.data.length) {
  42. console.error('Could not fetch most recent commits for GitHub Actions, returning early');
  43. }
  44. await octokit.actions.createWorkflowDispatch({
  45. repo: ELECTRON_REPO,
  46. owner: ELECTRON_ORG,
  47. workflow_id: `${workflowName}.yml`,
  48. ref: `refs/tags/${options.newVersion}`,
  49. inputs: {
  50. ...buildRequest.parameters
  51. }
  52. });
  53. const runNumber = await getGitHubActionsRun(workflowName, commits.data[0].sha);
  54. if (runNumber === -1) {
  55. return;
  56. }
  57. console.log(`GitHub Actions release build pipeline ${runNumber} for ${workflowName} triggered.`);
  58. const runUrl = `${GH_ACTIONS_PIPELINE_URL}/runs/${runNumber}`;
  59. if (options.runningPublishWorkflows) {
  60. console.log(`GitHub Actions release workflow request for ${workflowName} successful. Check ${runUrl} for status.`);
  61. } else {
  62. console.log(`GitHub Actions release build workflow running at ${GH_ACTIONS_PIPELINE_URL}/runs/${runNumber} for ${workflowName}.`);
  63. console.log(`GitHub Actions release build request for ${workflowName} successful. Check ${runUrl} for status.`);
  64. }
  65. } catch (err) {
  66. console.log('Error calling GitHub Actions: ', err);
  67. }
  68. }
  69. async function getGitHubActionsRun (workflowName: string, headCommit: string) {
  70. let runNumber = 0;
  71. let actionRun;
  72. while (runNumber === 0) {
  73. const actionsRuns = await octokit.actions.listWorkflowRuns({
  74. repo: ELECTRON_REPO,
  75. owner: ELECTRON_ORG,
  76. workflow_id: `${workflowName}.yml`
  77. });
  78. if (!actionsRuns.data.workflow_runs.length) {
  79. console.log(`No current workflow_runs found for ${workflowName}, response was: ${actionsRuns.data.workflow_runs}`);
  80. runNumber = -1;
  81. break;
  82. }
  83. for (const run of actionsRuns.data.workflow_runs) {
  84. if (run.head_sha === headCommit) {
  85. console.log(`GitHub Actions run ${run.html_url} found for ${headCommit}, waiting on status.`);
  86. actionRun = run;
  87. break;
  88. }
  89. }
  90. if (actionRun) {
  91. switch (actionRun.status) {
  92. case 'in_progress':
  93. case 'pending':
  94. case 'queued':
  95. case 'requested':
  96. case 'waiting': {
  97. if (actionRun.id && !isNaN(actionRun.id)) {
  98. console.log(`GitHub Actions run ${actionRun.status} for ${actionRun.html_url}.`);
  99. runNumber = actionRun.id;
  100. }
  101. break;
  102. }
  103. case 'action_required':
  104. case 'cancelled':
  105. case 'failure':
  106. case 'skipped':
  107. case 'timed_out':
  108. case 'failed': {
  109. console.log(`Error workflow run returned a status of ${actionRun.status} for ${actionRun.html_url}`);
  110. runNumber = -1;
  111. break;
  112. }
  113. }
  114. await new Promise(resolve => setTimeout(resolve, GH_ACTIONS_WAIT_TIME));
  115. }
  116. }
  117. return runNumber;
  118. }
  119. type BuildGHActionsOptions = {
  120. job?: typeof ghActionsPublishWorkflows[number];
  121. arch?: string;
  122. } & GitHubActionsCallOptions;
  123. async function buildGHActions (targetBranch: string, options: BuildGHActionsOptions) {
  124. if (options.job) {
  125. assert(ghActionsPublishWorkflows.includes(options.job), `Unknown GitHub Actions workflow name: ${options.job}. Valid values are: ${ghActionsPublishWorkflows}.`);
  126. await githubActionsCall(targetBranch, options.job, options);
  127. } else {
  128. assert(!options.arch, 'Cannot provide a single architecture while building all workflows, please specify a single workflow via --workflow');
  129. options.runningPublishWorkflows = true;
  130. for (const job of ghActionsPublishWorkflows) {
  131. await githubActionsCall(targetBranch, job, options);
  132. }
  133. }
  134. }
  135. type RunReleaseOptions = ({
  136. ci: 'GitHubActions'
  137. } & BuildGHActionsOptions) | ({
  138. ci: undefined,
  139. } & BuildGHActionsOptions);
  140. export async function runReleaseCIJobs (targetBranch: string, options: RunReleaseOptions) {
  141. if (options.ci) {
  142. switch (options.ci) {
  143. case 'GitHubActions': {
  144. await buildGHActions(targetBranch, options);
  145. break;
  146. }
  147. default: {
  148. console.log(`Error! Unknown CI: ${(options as any).ci}.`);
  149. process.exit(1);
  150. }
  151. }
  152. } else {
  153. await Promise.all([
  154. buildGHActions(targetBranch, options)
  155. ]);
  156. }
  157. console.log(`${jobRequestedCount} jobs were requested.`);
  158. }