generated from fastai/nbdev_template
/
tutorial_actions.html
898 lines (653 loc) · 38.7 KB
/
tutorial_actions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
---
title: Getting started with GitHub Actions
keywords: fastai
sidebar: home_sidebar
nb_path: "80_tutorial_actions.ipynb"
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: 80_tutorial_actions.ipynb
# command to build the docs after a change: nbdev_build_docs
-->
<div class="container" id="notebook-container">
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="About-GitHub-Actions">About GitHub Actions<a class="anchor-link" href="#About-GitHub-Actions"> </a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><a href="https://docs.github.com/en/free-pro-team@latest/actions">GitHub Actions</a> help you automate tasks within your software development life cycle. GitHub say that:</p>
<blockquote><p>"<em>GitHub Actions are event-driven, meaning that you can run a series of commands after a specified event has occurred. For example, every time someone creates a pull request for a repository, you can automatically run a command that executes a software testing script.</em>"</p>
</blockquote>
<p>They are a powerful system for automating many software development processes, and open source projects get unlimited compute hours for free! A sequence of steps in GitHub Actions are be combined together into a "workflow", which can furthermore contain multiple "jobs", which are run on multiple machines, with (if needed) multiple operating systems, in parallel. For more information, read GitHub's <a href="https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/introduction-to-github-actions">Introduction to GitHub Actions</a>.
However, workflows can be difficult to create and debug, because:</p>
<ul>
<li>They run in a special environment which is difficult to replicate locally</li>
<li>Each time you make a change it takes quite a few minutes before your updated workflow is run and you can see the results</li>
<li>It is difficult to develop interatively and iteratively</li>
<li>The actions environment and contexts are complex and the documentation, whilst detailed, provides few real examples to work from</li>
<li>The main "language" for building workflows is a YAML-based language which is quite verbose, and doesn't allow you to leverage your knowledge of other programming languages</li>
<li>Building full-featured extensions requires using TypeScript, which is not a language that most developers are used to using for scripting, sysadmin, and continuous integration tasks.</li>
</ul>
<p>However, with <a href="/cli.html#ghapi"><code>ghapi</code></a> you can write your workflows in Python and shell, and can do nearly all your development on your local machine. We'll start by importing from the library, along with <code>fastcore</code>:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">ghapi</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">fastcore.utils</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Your-first-Python-based-workflow">Your first Python-based workflow<a class="anchor-link" href="#Your-first-Python-based-workflow"> </a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We're going to help you get started by building a simple workflow which will add a comment to all new pull requests, saying "thank you" to the contributor.</p>
<p><a href="/core.html#GhApi"><code>GhApi</code></a> makes developing workflows easier, because your actual YAML file is created for you entirely automatically. To create the new workflow, run the <a href="/actions.html#create_workflow"><code>create_workflow</code></a> function, like so:</p>
<div class="highlight"><pre><span></span><span class="n">create_workflow</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'thankyou'</span><span class="p">,</span> <span class="n">event</span><span class="o">=</span><span class="n">Event</span><span class="o">.</span><span class="n">pull_request</span><span class="p">)</span>
</pre></div>
<p>Now it's time to create our python script. You'll find it in <code>.github/scripts</code>, named based on your <a href="/actions.html#create_workflow"><code>create_workflow</code></a> parameters. The initial skeleton just contains import statements.</p>
<p>It will be much easier for us to iterate on our script if we have a sample <em>context</em> that the GitHub workflow runner will provide to us. We can get one by calling <a href="/actions.html#example_payload"><code>example_payload</code></a>:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">example</span> <span class="o">=</span> <span class="n">example_payload</span><span class="p">(</span><span class="n">Event</span><span class="o">.</span><span class="n">pull_request</span><span class="p">)</span>
<span class="nb">list</span><span class="p">(</span><span class="n">example</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>['action', 'number', 'pull_request', 'repository', 'sender']</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>For information about all the fields in a <em>payload</em>, use the GitHub <a href="https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#pull_request">webhook payload</a> documentation. For instance, the docs tell us that the "action" field can be:</p>
<blockquote><p>"<em>opened, edited, closed, assigned, unassigned, review_requested, review_request_removed, ready_for_review, labeled, unlabeled, synchronize, locked, unlocked, or reopened</em>"</p>
</blockquote>
<p>Let's see what it contains for our example payload:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">example</span><span class="o">.</span><span class="n">action</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'opened'</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>For our script, we should ensure that it's only run for the <code>opened</code> action. Next up, we need to find out how to add a comment to a pull request. First, we'll need to create our <a href="/core.html#GhApi"><code>GhApi</code></a> object. On your own PC, your GitHub token should be in the <code>GITHUB_TOKEN</code> environment variable, whereas when run in a workflow it will be part of the <code>github</code> context. The <a href="/actions.html#github_token"><code>github_token</code></a> function handles this for you automatically, so we can say:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">api</span> <span class="o">=</span> <span class="n">GhApi</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="s1">'fastai'</span><span class="p">,</span> <span class="n">repo</span><span class="o">=</span><span class="s1">'ghapi-test'</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">github_token</span><span class="p">())</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>One way to find the correct operation to call is to search the <a href="https://ghapi.fast.ai/fullapi.html">full API reference</a>. Operations are generally named as <code>{verb}_{object}</code>, so search for <code>create_comment</code>. Alternatively, you can jump to the section that you expect to contain your required operation -- in this case, it's important to know that GitHub considers a "pull request" to be a special kind of "issue". After some looking through the page, we found this operation:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">api</span><span class="o">.</span><span class="n">issues</span><span class="o">.</span><span class="n">create_comment</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea output_execute_result">
<p><a href="https://docs.github.com/rest/reference/issues#create-an-issue-comment">issues.create_comment</a>(issue_number, body): <em>Create an issue comment</em></p>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The hyperlink provided will take you to the GitHub docs for this operation, so take a look at that now. We need to provide an <code>issue_number</code> and the <code>body</code> of the comment. We can look inside the payload to find the issue number we need to use:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="s1">', '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">example</span><span class="o">.</span><span class="n">pull_request</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'url, id, node_id, html_url, diff_url, patch_url, issue_url, number, state, locked, title, user, body, created_at, updated_at, closed_at, merged_at, merge_commit_sha, assignee, assignees, requested_reviewers, requested_teams, labels, milestone, commits_url, review_comments_url, review_comment_url, comments_url, statuses_url, head, base, _links, author_association, draft, merged, mergeable, rebaseable, mergeable_state, merged_by, comments, review_comments, maintainer_can_modify, commits, additions, deletions, changed_files'</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Since the docs call the parameter <code>issue_number</code>, and there is a <code>number</code> field here, that's probably what we should use.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To test this out, we'll first create a PR or issue, and then create our comment:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">com</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">issues</span><span class="o">.</span><span class="n">create_comment</span><span class="p">(</span><span class="n">issue_number</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="s1">'Thank you for your *valuable* contribution'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We can view the comment by visiting its URL:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">com</span><span class="o">.</span><span class="n">url</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'https://api.github.com/repos/fastai/ghapi-test/issues/comments/737393228'</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>...and then delete it since we were just testing:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">com</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">issues</span><span class="o">.</span><span class="n">delete_comment</span><span class="p">(</span><span class="n">com</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The payload will be available in the <a href="https://docs.github.com/en/free-pro-team@latest/actions/reference/context-and-expression-syntax-for-github-actions#github-context"><code>github</code> context</a>, which is provided automatically to you using the <code>context_github</code> variable. When running locally, an example github context will be used instead. (This example github context will not, in general, have the same payload as the event you're using, so use the <code>example payload</code> for that.)</p>
<p>The <a href="/event.html"><code>event</code></a> contains the actual payload:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">list</span><span class="p">(</span><span class="n">context_github</span><span class="o">.</span><span class="n">event</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>['inputs', 'organization', 'ref', 'repository', 'sender', 'workflow']</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>When we called <a href="/actions.html#create_workflow"><code>create_workflow</code></a>, the workflow it created can also be triggered using <a href="https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/">workflow dispatch</a>, which is more convenient for testing. In this case, our payload will not have the same information, so we should write our function in a way that can handle workflow dispatch as well.</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">dispatch</span> <span class="o">=</span> <span class="n">example_payload</span><span class="p">(</span><span class="n">Event</span><span class="o">.</span><span class="n">workflow_dispatch</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We can check what type of trigger we're responding to, by checking the keys of our payload:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="s1">'workflow'</span> <span class="ow">in</span> <span class="n">dispatch</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>True</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>In that case, we'll use a fixed issue number, instead of using the actual payload number.</p>
<p>We can now write our function.</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">reply_thanks</span><span class="p">():</span>
<span class="n">api</span> <span class="o">=</span> <span class="n">GhApi</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="s1">'fastai'</span><span class="p">,</span> <span class="n">repo</span><span class="o">=</span><span class="s1">'ghapi'</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">github_token</span><span class="p">())</span>
<span class="n">payload</span> <span class="o">=</span> <span class="n">context_github</span><span class="o">.</span><span class="n">event</span>
<span class="k">if</span> <span class="s1">'workflow'</span> <span class="ow">in</span> <span class="n">payload</span><span class="p">:</span> <span class="n">issue</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="n">payload</span><span class="o">.</span><span class="n">action</span> <span class="o">!=</span> <span class="s1">'opened'</span><span class="p">:</span> <span class="k">return</span>
<span class="n">issue</span> <span class="o">=</span> <span class="n">payload</span><span class="o">.</span><span class="n">number</span>
<span class="n">api</span><span class="o">.</span><span class="n">issues</span><span class="o">.</span><span class="n">create_comment</span><span class="p">(</span><span class="n">issue_number</span><span class="o">=</span><span class="n">issue</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="s1">'Thank you for your *valuable* contribution'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Finally, copy this to the script in <code>.github/scripts</code>, along with a line to run it (<code>reply_thanks()</code>), and commit it to GitHub.</p>
<p>You can now test the workflow by going to GitHub, clicking the "Actions" tab, then clicking the workflow you just created, and then clicking the "Run workflow" button.</p>
<p>Alternatively, you can run it using <a href="/core.html#GhApi"><code>GhApi</code></a>, which we'll do now:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">wf</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">actions</span><span class="o">.</span><span class="n">get_workflow</span><span class="p">(</span><span class="s1">'thankyou-pull_request.yml'</span><span class="p">)</span>
<span class="n">api</span><span class="o">.</span><span class="n">actions</span><span class="o">.</span><span class="n">create_workflow_dispatch</span><span class="p">(</span><span class="n">wf</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="n">ref</span><span class="o">=</span><span class="s1">'master'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea output_execute_result">
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To find out if the dispatch succeeded, go to the Actions tab on your repo and look for a yellow circle, which means it's running, or a green tick, which means it succeeded. If you get a red cross, it failed - you can click the item for details. You can also get the results of the latest run through the API (but be sure to wait at least 30 seconds for the workflow to kick off):</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">last_run</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">actions</span><span class="o">.</span><span class="n">list_workflow_runs</span><span class="p">(</span><span class="n">wf</span><span class="o">.</span><span class="n">id</span><span class="p">)</span><span class="o">.</span><span class="n">workflow_runs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">last_run</span><span class="o">.</span><span class="n">conclusion</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'success'</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>If it doesn't work, you can add <code>print(payload)</code> after the first line, and when GitHub runs your workflow you'll see the whole payload in your "Actions" panel.</p>
<p>Once it's working OK using the manual workflow dispatch, try creating a pull request.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Advanced-multi-job-workflow">Advanced multi-job workflow<a class="anchor-link" href="#Advanced-multi-job-workflow"> </a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Sometimes, you need to join more than one <em>job</em> in a workflow. This isn't common, but one example it's absolutely needed is if you want to build software or run tests on multiple platforms, and then have some step that runs before or after all of them. For instance, building and releasing software that needs to be built on each platform requires one job to create a release, and then a separate job to do the builds and add artifacts to the release, since GitHub Actions runs all your steps in a job in a single platform. Furthermore, we need to set up <a href="https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#creating-dependent-jobs">dependencies between jobs</a> using <code>needs</code>. <a href="/cli.html#ghapi"><code>ghapi</code></a> provides helpers to set up multi-job workflows with depedencies for you, which we'll show an example of here.</p>
<p>In this tutorial we're going to show how we created <a href="https://github.com/fastai/hugo-mathjax">hugo-mathjax</a>. The workflow in this project patches <a href="https://gohugo.io/">Hugo</a> to add <a href="https://www.mathjax.org/">Mathjax</a> support, then builds Mac, Ubuntu, and Windows versions of the software, creating a release which includes each of those builds as artifacts. It does the following:</p>
<ul>
<li>On a Ubuntu runner:<ul>
<li>Find out what the latest release of Hugo is</li>
<li>It we haven't yet built a patched version of this release, create a new release</li>
<li>Output the release number for the next job to use</li>
</ul>
</li>
<li>On Mac, Ubuntu, and Windows runners all running in parallel:<ul>
<li>Get the release number output by the previous step</li>
<li>Download this release from the Hugo repo, and untar it</li>
<li>Patch, build, and tar this release</li>
<li>Add the tarred release as an artifact of the release created in the first job</li>
</ul>
</li>
</ul>
<p>First, we'll create the workflow and script files:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">create_workflow</span><span class="p">(</span><span class="s1">'create'</span><span class="p">,</span> <span class="n">Event</span><span class="o">.</span><span class="n">schedule</span><span class="p">,</span> <span class="n">contexts</span><span class="o">=</span><span class="s1">'needs'</span><span class="p">,</span> <span class="n">opersys</span><span class="o">=</span><span class="s1">'macos, ubuntu, windows'</span><span class="p">,</span> <span class="n">prebuild</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This has some pieces we haven't seen before, so let's take a look at them now.</p>
<p>The <code>prebuild</code> bool tells <a href="/cli.html#ghapi"><code>ghapi</code></a> to include a prebuild job (see <a href="/actions.html#create_workflow"><code>create_workflow</code></a> for the full workflow created). After you run the above, take a look at the YAML that was created, to see how it all works. We're going to need to modify the YAML, to add the <a href="https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#schedule">schedule</a> we want to run it on, using a <code>cron:</code> line. You can see the final YAML file <a href="https://github.com/fastai/hugo-mathjax/blob/master/.github/workflows/python.yml">here</a>.</p>
<p>The prebuild job calls a prebuild script using a Ubuntu runner. We'll create the prebuild script now.</p>
<p>The script needs to get the current Hugo release tag:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">tag</span> <span class="o">=</span> <span class="n">GhApi</span><span class="p">()</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">get_latest_release</span><span class="p">(</span><span class="s1">'gohugoio'</span><span class="p">,</span> <span class="n">repo</span><span class="o">=</span><span class="s1">'hugo'</span><span class="p">)</span><span class="o">.</span><span class="n">name</span>
<span class="n">tag</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'v0.79.0'</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>...and check whether we have released that patched version:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">api</span> <span class="o">=</span> <span class="n">GhApi</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="s1">'fastai'</span><span class="p">,</span> <span class="n">repo</span><span class="o">=</span><span class="s1">'hugo-mathjax'</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">github_token</span><span class="p">())</span>
<span class="n">exists</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">try</span><span class="p">:</span> <span class="n">api</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">get_release_by_tag</span><span class="p">(</span><span class="n">tag</span><span class="p">)</span>
<span class="k">except</span> <span class="n">HTTP404NotFoundError</span><span class="p">:</span> <span class="n">exists</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">exists</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>True</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>In the case that the release already exists, we'll want to just exit, otherwise we'll create a new release:</p>
<div class="highlight"><pre><span></span><span class="n">rel</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">create_release</span><span class="p">(</span><span class="n">tag</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">tag</span><span class="p">)</span>
</pre></div>
<p>The <code>build</code> job will run in a separate runner, so we need to create an output telling it the tag of the release we've created. The <a href="/actions.html#actions_output"><code>actions_output</code></a> function does that, but print a special format that GitHub Actions uses:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">actions_output</span><span class="p">(</span><span class="s1">'tag'</span><span class="p">,</span> <span class="n">tag</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_subarea output_stream output_stdout output_text">
<pre>::set-output name=tag::v0.79.0
</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>You can see the completed prebuild script <a href="https://github.com/fastai/hugo-mathjax/blob/master/.github/scripts/prebuild.py">here</a>.</p>
<p>Now we can move on to creating the <code>build</code> script. First, we can get the outputs of the <code>prebuild</code> job as a <code>dict</code> by using <code>context_needs</code>, which contains information from any jobs in the <code>needs</code> section (which <a href="/cli.html#ghapi"><code>ghapi</code></a> will automatically set up for you if you enable <code>prebuild</code>):</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">out</span> <span class="o">=</span> <span class="n">loads</span><span class="p">(</span><span class="n">nested_idx</span><span class="p">(</span><span class="n">context_needs</span><span class="p">,</span> <span class="s1">'prebuild'</span><span class="p">,</span> <span class="s1">'outputs'</span><span class="p">,</span> <span class="s1">'out'</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To get the <code>tag</code> output of the job, we index into the <code>dict</code>:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">tag</span> <span class="o">=</span> <span class="n">nested_idx</span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="s1">'step1'</span><span class="p">,</span> <span class="s1">'outputs'</span><span class="p">,</span> <span class="s1">'tag'</span><span class="p">)</span>
<span class="n">tag</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'v0.79.0'</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We get the release details using the same approach as before, then download, untar, patch, and build it. We won't discuss those steps here since they're not specific to <a href="/cli.html#ghapi"><code>ghapi</code></a>.</p>
<p>Once the software is built, we need to know the name of the created file, which depends on the operating system we're running on. We can use the following approach to check:</p>
</div>
</div>
</div>
{% raw %}
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">platform</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">linux</span><span class="o">=</span><span class="s1">'linux'</span><span class="p">,</span> <span class="n">linux2</span><span class="o">=</span><span class="s1">'linux'</span><span class="p">,</span> <span class="n">win32</span><span class="o">=</span><span class="s1">'win'</span><span class="p">,</span> <span class="n">darwin</span><span class="o">=</span><span class="s1">'mac'</span><span class="p">)[</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="p">]</span>
<span class="n">ext_nm</span> <span class="o">=</span> <span class="s1">'hugo.exe'</span> <span class="k">if</span> <span class="n">platform</span><span class="o">==</span><span class="s1">'win'</span> <span class="k">else</span> <span class="s1">'hugo'</span>
<span class="n">ext_nm</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'hugo'</pre>
</div>
</div>
</div>
</div>
</div>
{% endraw %}
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Finally, we can upload our file as an artifact:</p>
<div class="highlight"><pre><span></span><span class="n">api</span> <span class="o">=</span> <span class="n">GhApi</span><span class="p">(</span><span class="n">owner</span><span class="o">=</span><span class="s1">'fastai'</span><span class="p">,</span> <span class="n">repo</span><span class="o">=</span><span class="s1">'hugo-mathjax'</span><span class="p">,</span> <span class="n">token</span><span class="o">=</span><span class="n">github_token</span><span class="p">())</span>
<span class="n">rel</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">repos</span><span class="o">.</span><span class="n">get_release_by_tag</span><span class="p">(</span><span class="n">tag</span><span class="p">)</span>
<span class="n">api</span><span class="o">.</span><span class="n">upload_file</span><span class="p">(</span><span class="n">rel</span><span class="p">,</span> <span class="n">fn</span><span class="p">)</span>
</pre></div>
<p>You can see the complete script <a href="https://github.com/fastai/hugo-mathjax/blob/master/.github/scripts/build.py">here</a>.</p>
<p>We've now successfully built a workflow that automatically patches, builds, and releases a mixed C/Go project, across multiple platforms, in parallel!</p>
</div>
</div>
</div>
</div>