<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bertrand Paquet</title>
    <description>The latest articles on DEV Community by Bertrand Paquet (@bpaquet).</description>
    <link>https://dev.to/bpaquet</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F694312%2F4eb7d360-f93a-4b63-a47b-de7c59b8c08e.jpeg</url>
      <title>DEV Community: Bertrand Paquet</title>
      <link>https://dev.to/bpaquet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bpaquet"/>
    <language>en</language>
    <item>
      <title>GitHub actions: how to push a GitHub status in addition of GitHub checks</title>
      <dc:creator>Bertrand Paquet</dc:creator>
      <pubDate>Fri, 27 Aug 2021 12:29:17 +0000</pubDate>
      <link>https://dev.to/doctolib/github-actions-how-to-push-a-github-status-in-addition-of-github-checks-3d27</link>
      <guid>https://dev.to/doctolib/github-actions-how-to-push-a-github-status-in-addition-of-github-checks-3d27</guid>
      <description>&lt;p&gt;&lt;a href="https://docs.github.com/en/actions" rel="noopener noreferrer"&gt;GitHub actions&lt;/a&gt; are incredibly efficient to quickly deploy a CI on a project, especially because of the native parallelism system.&lt;/p&gt;

&lt;p&gt;But we found some downsides along the way. One downside is the result of a GitHub actions is a &lt;a href="https://docs.gitHub.com/en/rest/reference/checks" rel="noopener noreferrer"&gt;Check&lt;/a&gt;, not a &lt;a href="https://docs.gitHub.com/en/rest/reference/repos#statuses" rel="noopener noreferrer"&gt;Status&lt;/a&gt;.&lt;br&gt;
There are identical in the UI, but are not in the API. In the image below, the last line is a status in the API, but all the lines above are checks at API Level. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F625rbgzo27n4g5s7i826.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F625rbgzo27n4g5s7i826.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What is wrong with that? At Doctolib, we have a lot of automation relying on statuses, not on checks. To onboard some of our projects on Github action, we were looking for an elegant solution.&lt;/p&gt;

&lt;p&gt;The first idea is to add a step at the end of the workflows, which push a status from here.&lt;br&gt;
Something like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Create status&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;curl --request POST \&lt;/span&gt;
          &lt;span class="s"&gt;--url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.event.workflow_run.head_commit.id }} \&lt;/span&gt;
          &lt;span class="s"&gt;--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \&lt;/span&gt;
          &lt;span class="s"&gt;--header 'content-type: application/json' \&lt;/span&gt;
          &lt;span class="s"&gt;--data '{&lt;/span&gt;
            &lt;span class="s"&gt;"state": "${{ github.event.workflow_run.conclusion }}",&lt;/span&gt;
            &lt;span class="s"&gt;"context": "unit test (kube)",&lt;/span&gt;
            &lt;span class="s"&gt;"target_url": "${{ github.event.workflow_run.html_url }}"&lt;/span&gt;
            &lt;span class="s"&gt;}' \&lt;/span&gt;
          &lt;span class="s"&gt;--fail&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This solution is working, but only for green build (successful build). For red build (failed build), we can use a &lt;a href="https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#failure" rel="noopener noreferrer"&gt;failure condition&lt;/a&gt;. If we have workflows with multiple jobs, we have to put this failure condition in every job. This can be painful, and add code duplication.&lt;/p&gt;

&lt;p&gt;So we found a simpler solution: have an independent GitHub workflow which &lt;a href="https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#onevent_nametypes" rel="noopener noreferrer"&gt;listen&lt;/a&gt; to the CI workflow (success AND failure), and set a status accordingly.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set test status&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;workflow_run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;workflows&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Run&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;tests"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;types&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;completed&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;set_status&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;statuses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt; 
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Create status&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;curl --request POST \&lt;/span&gt;
          &lt;span class="s"&gt;--url https://api.github.com/repos/${{ github.repository }}/statuses/${{ github.event.workflow_run.head_commit.id }} \&lt;/span&gt;
          &lt;span class="s"&gt;--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \&lt;/span&gt;
          &lt;span class="s"&gt;--header 'content-type: application/json' \&lt;/span&gt;
          &lt;span class="s"&gt;--data '{&lt;/span&gt;
            &lt;span class="s"&gt;"state": "${{ github.event.workflow_run.conclusion }}",&lt;/span&gt;
            &lt;span class="s"&gt;"context": "unit test (kube)",&lt;/span&gt;
            &lt;span class="s"&gt;"target_url": "${{ github.event.workflow_run.html_url }}"&lt;/span&gt;
            &lt;span class="s"&gt;}' \&lt;/span&gt;
          &lt;span class="s"&gt;--fail&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Feel free to reuse it, you just have to change:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The name of your CI workflow in the &lt;code&gt;on&lt;/code&gt; clause.&lt;/li&gt;
&lt;li&gt;The name of the status you want to push on GitHub in the &lt;code&gt;context&lt;/code&gt; params.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>github</category>
      <category>git</category>
    </item>
  </channel>
</rss>
