[IRBuilder] Refactor FMF interface (#121657)
[llvm-project.git] / .github / workflows / libcxx-restart-preempted-jobs.yaml
blob82d84c01c92af242b8d4ca0d8c43a1b058413df0
1 name: Restart Preempted Libc++ Workflow
3 # The libc++ builders run on preemptable VMs, which can be shutdown at any time.
4 # This workflow identifies when a workflow run was canceled due to the VM being preempted,
5 # and restarts the workflow run.
7 # We identify a canceled workflow run by checking the annotations of the check runs in the check suite,
8 # which should contain the message "The runner has received a shutdown signal."
10 # Note: If a job is both preempted and also contains a non-preemption failure, we do not restart the workflow.
12 on:
13   workflow_run:
14     workflows: [Build and Test libc\+\+]
15     types:
16       - completed
18 permissions:
19   contents: read
21 jobs:
22   restart:
23     if: github.repository_owner == 'llvm' && (github.event.workflow_run.conclusion == 'failure' || github.event.workflow_run.conclusion == 'cancelled')
24     name: "Restart Job"
25     permissions:
26       statuses: read
27       checks: write
28       actions: write
29     runs-on: ubuntu-latest
30     steps:
31       - name: "Restart Job"
32         uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7.0.1
33         with:
34           script: |
35             const failure_regex = /Process completed with exit code 1./
36             const preemption_regex = /The runner has received a shutdown signal/
38             const wf_run = context.payload.workflow_run
39             core.notice(`Running on "${wf_run.display_title}" by @${wf_run.actor.login} (event: ${wf_run.event})\nWorkflow run URL: ${wf_run.html_url}`)
42             async function create_check_run(conclusion, message) {
43                 // Create a check run on the given workflow run to indicate if
44                 // we are restarting the workflow or not.
45                 if (conclusion != 'success' && conclusion != 'skipped' && conclusion != 'neutral') {
46                   core.setFailed('Invalid conclusion: ' + conclusion)
47                 }
48                 await github.rest.checks.create({
49                     owner: context.repo.owner,
50                     repo: context.repo.repo,
51                     name: 'Restart Preempted Job',
52                     head_sha: wf_run.head_sha,
53                     status: 'completed',
54                     conclusion: conclusion,
55                     output: {
56                       title: 'Restarted Preempted Job',
57                       summary: message
58                     }
59                 })
60             }
62             console.log('Listing check runs for suite')
63             const check_suites = await github.rest.checks.listForSuite({
64               owner: context.repo.owner,
65               repo: context.repo.repo,
66               check_suite_id: context.payload.workflow_run.check_suite_id,
67               per_page: 100 // FIXME: We don't have 100 check runs yet, but we should handle this better.
68             })
70             check_run_ids = [];
71             for (check_run of check_suites.data.check_runs) {
72               console.log('Checking check run: ' + check_run.id);
73               if (check_run.status != 'completed') {
74                 console.log('Check run was not completed. Skipping.');
75                 continue;
76               }
77               if (check_run.conclusion != 'failure' && check_run.conclusion != 'cancelled') {
78                 console.log('Check run had conclusion: ' + check_run.conclusion + '. Skipping.');
79                 continue;
80               }
81               check_run_ids.push(check_run.id);
82             }
84             has_preempted_job = false;
86             for (check_run_id of check_run_ids) {
87               console.log('Listing annotations for check run: ' + check_run_id);
89               annotations = await github.rest.checks.listAnnotations({
90                 owner: context.repo.owner,
91                 repo: context.repo.repo,
92                 check_run_id: check_run_id
93               })
95               for (annotation of annotations.data) {
96                 if (annotation.annotation_level != 'failure') {
97                   continue;
98                 }
100                 const preemption_match = annotation.message.match(preemption_regex);
102                 if (preemption_match != null) {
103                   console.log('Found preemption message: ' + annotation.message);
104                   has_preempted_job = true;
105                 }
107                 const failure_match = annotation.message.match(failure_regex);
108                 if (failure_match != null) {
109                   // We only want to restart the workflow if all of the failures were due to preemption.
110                   // We don't want to restart the workflow if there were other failures.
111                   core.notice('Choosing not to rerun workflow because we found a non-preemption failure' +
112                     'Failure message: "' + annotation.message + '"');
113                   await create_check_run('skipped', 'Choosing not to rerun workflow because we found a non-preemption failure\n'
114                     + 'Failure message: ' + annotation.message)
115                   return;
116                 }
117               }
118             }
120             if (!has_preempted_job) {
121               core.notice('No preempted jobs found. Not restarting workflow.');
122               await create_check_run('neutral', 'No preempted jobs found. Not restarting workflow.')
123               return;
124             }
126             core.notice("Restarted workflow: " + context.payload.workflow_run.id);
127             await github.rest.actions.reRunWorkflowFailedJobs({
128                 owner: context.repo.owner,
129                 repo: context.repo.repo,
130                 run_id: context.payload.workflow_run.id
131               })
132             await create_check_run('success', 'Restarted workflow run due to preempted job')
134   restart-test:
135     if: github.repository_owner == 'llvm' && (github.event.workflow_run.conclusion == 'failure' || github.event.workflow_run.conclusion == 'cancelled') && github.event.actor.login == 'ldionne' # TESTING ONLY
136     name: "Restart Job (test)"
137     permissions:
138       statuses: read
139       checks: write
140       actions: write
141     runs-on: ubuntu-latest
142     steps:
143       - name: "Restart Job (test)"
144         uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7.0.1
145         with:
146           script: |
147             const FAILURE_REGEX = /Process completed with exit code 1./
148             const PREEMPTION_REGEX = /(The runner has received a shutdown signal)|(The operation was canceled)/
150             function log(msg) {
151               core.notice(msg)
152             }
154             const wf_run = context.payload.workflow_run
155             log(`Running on "${wf_run.display_title}" by @${wf_run.actor.login} (event: ${wf_run.event})\nWorkflow run URL: ${wf_run.html_url}`)
157             log('Listing check runs for suite')
158             const check_suites = await github.rest.checks.listForSuite({
159               owner: context.repo.owner,
160               repo: context.repo.repo,
161               check_suite_id: context.payload.workflow_run.check_suite_id,
162               per_page: 100 // FIXME: We don't have 100 check runs yet, but we should handle this better.
163             })
165             preemptions = [];
166             legitimate_failures = [];
167             for (check_run of check_suites.data.check_runs) {
168               log(`Checking check run: ${check_run.id}`);
169               if (check_run.status != 'completed') {
170                 log('Check run was not completed. Skipping.');
171                 continue;
172               }
174               if (check_run.conclusion != 'failure' && check_run.conclusion != 'cancelled') {
175                 log(`Check run had conclusion: ${check_run.conclusion}. Skipping.`);
176                 continue;
177               }
179               annotations = await github.rest.checks.listAnnotations({
180                 owner: context.repo.owner,
181                 repo: context.repo.repo,
182                 check_run_id: check_run.id
183               })
185               preemption_annotation = annotations.data.find(function(annotation) {
186                 return annotation.annotation_level == 'failure' &&
187                        annotation.message.match(PREEMPTION_REGEX) != null;
188               });
189               if (preemption_annotation != null) {
190                 log(`Found preemption message: ${preemption_annotation.message}`);
191                 preemptions.push(check_run);
192                 break;
193               }
195               failure_annotation = annotations.data.find(function(annotation) {
196                 return annotation.annotation_level == 'failure' &&
197                        annotation.message.match(FAILURE_REGEX) != null;
198               });
199               if (failure_annotation != null) {
200                 log(`Found legitimate failure annotation: ${failure_annotation.message}`);
201                 legitimate_failures.push(check_run);
202                 break;
203               }
204             }
206             if (preemptions) {
207               log('Found some preempted jobs');
208               if (legitimate_failures) {
209                 log('Also found some legitimate failures, so not restarting the workflow.');
210               } else {
211                 log('Did not find any legitimate failures. Restarting workflow.');
212                 await github.rest.actions.reRunWorkflowFailedJobs({
213                   owner: context.repo.owner,
214                   repo: context.repo.repo,
215                   run_id: context.payload.workflow_run.id
216                 })
217               }
218             } else {
219               log('Did not find any preempted jobs. Not restarting the workflow.');
220             }