<?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: WK</title>
    <description>The latest articles on DEV Community by WK (@distributhor).</description>
    <link>https://dev.to/distributhor</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%2F313611%2F201892d1-bb40-41b9-88e2-71f3a434f289.jpg</url>
      <title>DEV Community: WK</title>
      <link>https://dev.to/distributhor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/distributhor"/>
    <language>en</language>
    <item>
      <title>Invoking webhooks from your Github workflow actions</title>
      <dc:creator>WK</dc:creator>
      <pubDate>Fri, 17 Jan 2020 09:02:04 +0000</pubDate>
      <link>https://dev.to/distributhor/invoking-webhooks-from-your-github-workflow-actions-2fbd</link>
      <guid>https://dev.to/distributhor/invoking-webhooks-from-your-github-workflow-actions-2fbd</guid>
      <description>&lt;p&gt;Github webhooks are great for triggering deployments (or performing any other task) on certain repository events, such as a push to master. But they can't do so conditionally. For example, you may first want to run some tests and then invoke a webhook only if they passed. In this case you could just run the tests on the webhook implementation itself before doing deployment. However, if you really have to call a webhook only in conjunction with a certain condition (other than the repository event such as a push to master), then you would need additional tooling.&lt;/p&gt;

&lt;p&gt;This is where Github Actions can provide a clean workflow solution, which can perform any number of tasks in a pipeline fashion. For more info on Github Action workflows and how to use them, see &lt;a href="https://github.com/features/actions"&gt;https://github.com/features/actions&lt;/a&gt;. Unfortunately, by default, Github  workflows cannot trigger a webhook. There are community developed Actions available from the Github Marketplace which can help in this regard. Of course, they are not official Github webhooks, but an alternate mechanism with which to invoke an endpoint, and in such a way emulate the functionality of a webhook. &lt;/p&gt;

&lt;p&gt;At the time of writing this post, none of the Actions available in the Github Marketplace (that I could find) supported the invocation of webhooks with the same hash signature as that used by official Github webhook calls. This is unfortunate, since one would not be able to re-use any existing webhook signature validation methods. It would be nice if the method for validating an official Github webhook call and those from an Action in a Github workflow were interchangeable. I don't currently have any actual use case that would require a webhook to be called from both, but still it would be nice. &lt;/p&gt;

&lt;p&gt;Since I couldn't find a suitable Action from the Marketplace with such signature validation, I decided to implement one myself. It's available at &lt;a href="https://github.com/marketplace/actions/workflow-webhook-action"&gt;https://github.com/marketplace/actions/workflow-webhook-action&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;This Github workflow action will call a webhook endpoint with a &lt;strong&gt;text/csv&lt;/strong&gt; or &lt;strong&gt;json&lt;/strong&gt; payload, and has support for BASIC authentication. A hash signature is derived from the payload of the POST along with a configurable secret token, and it is identical to the hash which a regular Github webhook call would generate. This signature is sent in a header named &lt;strong&gt;X-Hub-Signature&lt;/strong&gt;, therefore any existing Github webhook signature validation will continue to work. For more information on how to validate the signature, see &lt;a href="https://developer.github.com/webhooks/securing"&gt;https://developer.github.com/webhooks/securing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By default, the values of the following workflow environment variables are sent in the payload: GITHUB_REPOSITORY, GITHUB_REF, GITHUB_SHA, GITHUB_EVENT_NAME and GITHUB_WORKFLOW. Additional data can also added to the payload along with these.&lt;/p&gt;

&lt;p&gt;This post won't go into the details of how to configure a Github Action workflow, but as a simple example, to configure your workflow to invoke a webhook, you can add something like this to your workflow configuration file:&lt;br&gt;
&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;Invoke deployment hook&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;distributhor/workflow-webhook@v1&lt;/span&gt;
      &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;webhook_url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.WEBHOOK_URL }}&lt;/span&gt;
        &lt;span class="na"&gt;webhook_secret&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.WEBHOOK_SECRET }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which will deliver a payload with the following properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"repository"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"owner/project"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"ref"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"refs/heads/master"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"commit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"a636b6f0861bbee98039bf3df66ee13d8fbc9c74"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"event"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"push"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"workflow"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Build and deploy"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more details on configuration and all the available options, visit the official page for the Github Action at &lt;a href="https://github.com/marketplace/actions/workflow-webhook-action"&gt;https://github.com/marketplace/actions/workflow-webhook-action&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>github</category>
      <category>deployment</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
