<!-- begin _includes/seo.html --><title>Automatically Build and Deploy Your Site using GitHub Actions and Webhooks - Şahin Akkaya’s Personal Page</title>
<metaname="description"content="In this post I will explain how you can use GitHub to automate the build and deployment processes that you have. I am going to automate the deployment of this site but you can do whatever you want. Just understanding the basics will be enough.">
<metaproperty="og:description"content="In this post I will explain how you can use GitHub to automate the build and deployment processes that you have. I am going to automate the deployment of this site but you can do whatever you want. Just understanding the basics will be enough.">
<!-- insert favicons. use https://realfavicongenerator.net/ -->
<!-- end custom head snippets -->
</head>
<bodyclass="layout--single">
<navclass="skip-links">
<ul>
<li><ahref="#site-nav"class="screen-reader-shortcut">Skip to primary navigation</a></li>
<li><ahref="#main"class="screen-reader-shortcut">Skip to content</a></li>
<li><ahref="#footer"class="screen-reader-shortcut">Skip to footer</a></li>
</ul>
</nav>
<!--[if lt IE 9]>
<divclass="notice--danger align-center"style="margin: 0;">You are using an <strong>outdated</strong> browser. Please <ahref="https://browsehappy.com/">upgrade your browser</a> to improve your experience.</div>
<metaitemprop="headline"content="Automatically Build and Deploy Your Site using GitHub Actions and Webhooks">
<metaitemprop="description"content="In this post I will explain how you can use GitHub to automate the build and deployment processes that you have. I am going to automate the deployment of this site but you can do whatever you want. Just understanding the basics will be enough.">
<p>In this post I will explain how you can use GitHub to automate the build and deployment processes that you have. I am going to automate the deployment of this site but you can do whatever you want. Just understanding the basics will be enough.</p>
<h2id="introduction-to-github-actions-and-webhooks">Introduction to GitHub Actions and Webhooks</h2>
<p>Let me start by explaining what are GitHub Actions and GitHub Webhooks.</p>
<blockquote>
<p><strong>Github Actions</strong> is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.</p>
</blockquote>
<blockquote>
<p><strong>Webhooks</strong> provide a way for notifications to be delivered to an external web server whenever certain actions occur on a repository or organization. … For example, you can configure a webhook to execute whenever:</p>
<ul>
<li>A repository is pushed to</li>
<li>A pull request is opened</li>
<li>A GitHub Pages site is built</li>
<li>A new member is added to a team</li>
</ul>
</blockquote>
<h2id="defining-the-problem-and-solution">Defining the problem and solution</h2>
<p>As I said, my example will be automating the deployment of this site. Here is the normal workflow of me doing it manually:
<p>As you can see, the only place where my work is really required is writing the post. Other two steps can be automated. We will use GitHub Actions to generate the site content and Webhooks to let our server know about the new content so it can pull the changes. Let’s get started.</p>
<h3id="setting-up-github-actions">Setting up GitHub Actions</h3>
<p>Setting up a GitHub Action is as easy as creating a <codeclass="language-plaintext highlighter-rouge">.yml</code> file in <codeclass="language-plaintext highlighter-rouge">.github/workflows/</code> directory in your repository. Let us create a new action to build our site. Fortunately, there is already a <ahref="https://github.com/marketplace/actions/jekyll-actions">GitHub action</a> to do it for us. Create a file called <codeclass="language-plaintext highlighter-rouge">.github/workflows/jekyll.yml</code> in your root directory of your repository and put the following contents:</p>
<divclass="language-yaml highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="na">name</span><spanclass="pi">:</span><spanclass="s">Jekyll site CI</span>
<p>That’s it! We have created our first Action. When we push this change, GitHub will start building our site and push the result to <codeclass="language-plaintext highlighter-rouge">gh-pages</code> branch. Currently, it will take a while to build because we don’t use caching. So let’s include it to build faster. Add the following piece as a second step:</p>
<divclass="language-yaml highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="c1"># Use GitHub Actions' cache to shorten build times and decrease load on servers</span>
<p>We are done with the Actions part. You can see the final code <ahref="https://github.com/Asocia/sahinakkayadotdev/blob/main/.github/workflows/jekyll.yml">here</a>. When you are also done with the code, just push it to trigger the action.</p>
<h3id="setting-up-the-webhook-and-related-endpoint">Setting up the Webhook and related endpoint</h3>
<h4id="creating-a-webhook-from-github">Creating a Webhook from GitHub</h4>
<p>To add a Webhook, open your repository in browser and navigate to <em>‘Settings > Webhooks’</em> and click <em>‘Add Webhook’</em>. Fill in the form with appropriate values. Here is an example:
<imgsrc="/assets/images/gh-actions-and-webhooks/add-webhook.png"alt="Webhook form example"/></p>
<p>This is all you have to do from GitHub. Now, whenever there is a <em><codeclass="language-plaintext highlighter-rouge">push</code></em> event to your repository, GitHub will send a POST request to your <em>payload url</em> with the details.</p>
<pclass="notice--info"><strong>Note:</strong> Our Action is configured to push to a branch in our repository, so it will also trigger this hook and we will catch it.</p>
<h4id="creating-an-endpoint-to-handle-the-requests">Creating an endpoint to handle the requests</h4>
<p>I will use <ahref="https://flask.palletsprojects.com/en/2.0.x/">Flask</a> framework to handle the post requests coming to our endpoint. You can use whatever programming language or framework you want. It will be very simple code with just one job: Validate the secret keys and run a specific code.</p>
<spanclass="n">APP_KEY</span><spanclass="o">=</span><spanclass="s">"your-secret-key"</span><spanclass="c1"># same key that is used in github while creating the webhook
</span><spanclass="n">PROJECT_PATH</span><spanclass="o">=</span><spanclass="s">"/path/to/your/project/"</span><spanclass="c1"># you will want to cd into this path and perform commands such as git pull etc.
<p>I will not go into details explaining what each line does. Basically, we are checking if the request is a POST request and if so we are comparing the secret keys to make sure that the request is coming from GitHub. In our case, this is not too important because when the keys match we are running a simple <codeclass="language-plaintext highlighter-rouge">git pull</code> command in our repository but you might need it if you are doing something more complicated. And here is the contents of <codeclass="language-plaintext highlighter-rouge">perform-git-pull.sh</code> file:</p>
<p>Create a new file <codeclass="language-plaintext highlighter-rouge">post_receiver.service</code> in <codeclass="language-plaintext highlighter-rouge">/etc/systemd/system/</code>:</p>
<p>That’s it! <codeclass="language-plaintext highlighter-rouge">curl https://yourdomain.com/postreceive/</code> should return <codeclass="language-plaintext highlighter-rouge">"OK"</code> and we are ready to accept POST requests from GitHub.</p>
<h3id="notes-for-debugging">Notes for debugging</h3>
<p>In case anything goes wrong, here are a few tips to debug:</p>
<ul>
<li>Every GitHub Action produces a log that you can examine. Check them to see if anything is odd.</li>
<li>In the <em>Webhooks</em> tab, there is a sub-tab called <em>Recent Deliveries</em>. You can take a look at there to see the results of the requests from your hooks.</li>
<li>You can always test your code locally with <codeclass="language-plaintext highlighter-rouge">curl</code>:
<pclass="archive__item-excerpt"itemprop="description">Hello, World!* So here I am and welcome to my first blog. Having a personal space on the Internet has been a dream for me for years and I am happy that it fi…