How to Set up Automated Deployments with GitHub Webhooks
Introduction
Nowadays, new software versions are typically provided with the help of deployment processes. The days of manually exchanging code via FTP or similar methods are long gone. Many use deployment scripts that reside on the server or establish a connection to the server and then perform the desired actions. Wouldn't it be handy if the deployment is automatically triggered after a code push to GitHub to further automate the CI/CD pipeline? In this tutorial, we will show you how to achieve this in Pipetower. Let's get started!
Create a Deployment Action
Let's assume that you have added your server already to Pipetower. The first step for our webhook triggered deployment is to create an Action in Pipetower.
Go to Actions and then click Create action.
The Action in Pipetower will define your actual tasks for the deployment. You can do everything that you could do with your terminal, for example executing multiple commands or running your pre-defined deployment script.
In our example we will create an Action that consists of 2 Action steps.
- In the 1. step we'll cd into our repository and pull the latest changes from GitHub
- In the 2. step we'll restart the supervisor service
You can create as many or as few action steps as you like to perform your deployment. Of course, you can also just call your existing deployment script on the server using the appropriate command.
Even more complex processes with multiple actions that are connected by a Pipetower Workflow can also be built. But for now our Action is completely sufficient.
Save, that's it.
Create a Pipe as a Wrapper
The Action we created in the previous step can already be called from the outside through a specific Pipetower URL to run our deployment. However, in order to make the execution of the deployment dependent on the GitHub payload (in the next step), we need to wrap the Action in a Pipe.
Go to Pipes and then click Create pipe.
Create a Pipe with the following settings:
- Trigger type: Webhook
- Trigger event: Catch hook
Your unique Webhook URL you need for GitHub will be displayed after saving the Pipe.
After the Trigger is ready, we still need to create a Step. In this Step we define what should be executed when the Pipe is triggered.
Next, click on the plus icon directly below the Trigger.
A new section opens to define our Step.
Create the Step with the following settings:
- Step type: Action
- Step event: Execute action
- Action: Deployment (Our previously created Action)
- Server: Select your server on which the Action will run
Save. Now our Pipe is ready and your unique Pipetower Webhook URL is generated.
Click on the Trigger title to open it's section and to see the URL:
Copy this URL. We'll need it to set up the GitHub Webhook.
Only listen to certain Branches (Optional)
You often work with several branches in GitHub. E.g. a dev branch for the current development status and a main branch that reflects the current production status.
In such a scenario, it makes sense to only run an automatic deployment if the commit refers to a specific branch. This means that when you commit to the main branch, you can deploy to the production server. Or, when committing to the dev branch, deploy to the development server.
Creating a conditional execution like this is quite easy with Pipetower.
Edit your Pipe and open the section of the Action Step.
Where you selected the Deployment Action, you can add a condition.
Click on the Add condition link.
A new area opens in which you can set the conditions under which the Deployment Action will be executed.
Set up the condition like this:
- Condition field: Payload from the Trigger
- Condition: (Output) Matches regular expression
- Value: "ref":"(.*)/main"
When you push changes to your repository, GitHub will call your Pipetower Webhook URL with it's payload. The payload includes the branch that was pushed to. We use this information to decide whether the Deployment Action should be executed or not.
Save. Your Pipe is ready.
When it's triggered by calling your Webhook URL and the condition is met
Then it will execute your Deployment Action
Set up a GitHub Webhook
The last step is to set up the GitHub webhook in order to call your Pipetower URL when pushing to your repository.
Go to your repository settings and click on the Webhooks menu item.
Click on Add webhook.
In the Payload URL you have to enter your Pipetower Webhook URL from your Pipe.
Select as Content type the value application/json.
Make sure that "Just the push event." is selected.
Mark the webhook as active and Save.
That's it. GitHub is now configured to invoke your Pipetower Webhook at every push event.
Test: Push to GitHub
To test your automated deployment workflow, just push changes to the main branch.
Pipetower will know about it and do its job.
On the Run history tab of your Pipe, you can see that the Pipe was invoked. You can also see the payload of GitHub and how your Action was executed.
Conclusion
Automated Deployments are an essential part of your CI/CD pipeline to deliver frequently new releases. Pipetower makes it easy to react on GitHub pushes and to execute your deployment on your server. With Pipetower's Actions and Pipes you have everything at your fingertips to make your server automation accessible from outside.