March 8, 2022

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

  1. Go to your Github project repository -> settings -> webhooks -> add webhook
  2. Create a new webhook.
    1. Use the payload URL as your http://:9000/hooks/deploy_project_name
    2. Click Let me select individual events” -> Click Packages” and uncheck everything else
    3. Click Save

Setup webhook on your server

  1. 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 with systemctl status webhook. You won’t see this on the documentation page of the project repo. Use this command to restart the service sudo systemctl restart webhook if you make changes.
  2. Using the status command above you can see that the service is using a file called /etc/webhook.conf That is the equivalent of your hooks.json which you will see mentioned in the project repo.
  3. 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

  1. 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”
    }
    ]

  2. Go to your root folder and create a folder called webhooks. This is where you will place your bash files

For each one of my projects I have a bash file in the webhooks folder that looks like this

#!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)


Previous post
Favorite Psychological Tendencies Want to know how to the world works? Use psychological tendencies to your advantage. Here’s a long list of some of
Next post
How I made a bot that scrapes HTML and posts to Twitter using No-Code Background:I recently finished launching a small webapp that I made for fun
Share this post if you enjoyed it :)
Subscribe to my newsletter to get notified of new posts
Follow Me On Twitter