If you are self hosting your website and aiming for automating the deployment, this quick guide might help you. In this we are going to run the docker services inside a docker swarm cluster and will manage it using portainer. Also we use gihub webhook for automatically triggering the deployment when ever new changes are committed to the repository.
If you don’t have a Portainer service running, you can setup and connect to your docker swarm cluster. I am using Portainer business edition with free 3 nodes subscription. You can either use the Portainer business edition for free if you are having 3 or less number of nodes or can use the community version. You can request the license key at https://www.portainer.io/take-3
The approach we are going to use will follow the following flow of actions.
- We will setup a docker stack in Portainer using github repository to run our web services
- Portainer will provide a webhook URL which we can trigger when ever we want to redeploy the apps from github
- We will create a webhook event in github for commit actions and refer the portainer web hook URL
- When ever we commit changes to Github, Github will call the Portainer web hook to inform there is a change and Portainer will download repository from Github and redeploy
Below is the details steps of implementing this. First create a stack in Portainer referring a Github repository. The repository can be a private or public one. For the private repository you need to configure the authentication.
In the above screenshot you can authentication is enabled while creating a stack referring git repository. Also you can notice the GitOps update option in the end of above screenshot. Below is the possible options when the switch is enabled.
There is two possible option for automatic deployment. The first one is pulling option which will make the Portainer to check the repository in configured interval for look for a change and will deploy the codes when changed. I have chosen Webhook which gives a link to trigger when we need to redeploy the code. We will use this link in Github events to call when there is a commit.
Most of the fields in the screenshots are self explanatory, so I am not going in to the details but feel free to question me in comments when there are any questions.
Now we will login to Github to configure the events. Select the repository and select Webhooks from left side menu then click on Add Webhook. In the Payload URL enter the URL copied from Portainer. In the Events to notify you can go with just Push events as you only want to redeploy when there is something pushed to the repository. Also it is possible to trigger deployment for other git events.
Once this is configured you are good to go. When you push something to this repository Github will call your webhook from Portainer and it will auto deploy your code.
You are welcome with any questions in the comments.