Automatically deploy docker containers to a server after you commit to GitHub
After I push to GitHub, I have a GitHub action that automatically creates a docker package. My goal was to create a system that would automatically deploy my docker container after the main branch of my project had been pushed to.
Prerequisites– Ubuntu Server >=17
– Github
– Docker
– Webhook ( https://github.com/adnanh/webhook )
Things you should have prepared:
– Server IP address
– Project Name
– Github action to automatically create a package when you push to master or however else you define it (https://gist.github.com/ARezaK/ad0697302915f34c53224239d293bc7d)
Setup webhook on github
- Go to your Github project repository -> settings -> webhooks -> add webhook
- Create a new webhook.
- Use the payload URL as your http://
:9000/hooks/deploy_project_name - Click “Let me select individual events” -> Click “Packages” and uncheck everything else
- Click Save
- Use the payload URL as your http://
Setup webhook on your server
- Run
sudo apt-get install webhook
. I found out using the apt-get method that it already creates a service for me which you can see withsystemctl status webhook
. You won’t see this on the documentation page of the project repo. Use this command to restart the servicesudo systemctl restart webhook
if you make changes.
- Using the status command above you can see that the service is using a file called /
etc/webhook.conf
That is the equivalent of yourhooks.json
which you will see mentioned in the project repo. - I also went to and edited the file
/lib/systemd/system/webhook.service
to add the-verbose
tag so now it looks like this
[Unit]
Description=Small server for creating HTTP endpoints (hooks)
Documentation=https://github.com/adnanh/webhook/ [Service]
ExecStart=/usr/bin/webhook -nopanic -hooks /etc/webhook.conf -verbose
[Install]
WantedBy=multi-user.target
Below is what my webhook.conf looks for my 2 projects. Adjust it to your liking.
[
{
“id”: “deploy_project_name”,
“execute-command”: “/root/webhoooks/deploy_project_name.sh”,
“command-working-directory”: “/”,
“include-command-output-in-response”: true,
“response-message”: “Deploy Project_name”
},
{
“id”: “deploy_project_name_2”,
“execute-command”: “/root/webhooks/deploy_project_name_2.sh”,
“command-working-directory”: “/”,
“include-command-output-in-response”: true,
“response-message”: “Deploy Project_name_2”
}
]
Go to your root folder and create a folder called webhooks. This is where you will place your bash files
#!bin/bash
docker pull ghcr.io/:main
docker stop
docker system prune -f
docker run –name=“” -d –restart always -e “PORT=8765” -e “DEBUG=1” -p 8012:8765 ghcr.io/:main
with their respective names being deploy_project_name.sh and deploy_project_name_2.sh
Once you have created those you should be all set. Now whenever your package is built on GitHub it should be automatically deployed on your server. You can always manually deploy by going to the url (http://server_ip_address:9000/hooks/deploy_Project_name)