Deploy website via Github webhook

Getting started with Github webhook

  1. Firstly, create new workload and choose Stateful Set

  2. Fill Stateful Set Name and Service Name.



  3. Create new container that’s have webhook image inside.
    Container Name: <”Your container name”>
    Image Name: “maxoatzadn/webhook”
    Ports:
           -Name: <”Name container port”>
           -Port: “80”

    Hint: Port 80 is default port of nginx.

    container1


  4. Fill Enviroment Varible with your GitHub source code repository for website’s content which will be cloned into the container.
    Environment Variable:
           -Key: “LOCATION”
           -Value: <”Your project on github”>
             (eg. https://github.com/toptapznt/nginx-test.git)

    container2


  5. Create new service that’s target to your container and select service name that’s same as previous part.
    Service Name : <”Your service name”>
    select : Label
    Selector: select container that’s your create.
    Ports:
           -Name: <”Name service port”>
           -Service Port: “80”

    service1


  6. Create new volume, type Config Map.
    Volume Name: <”Your Volume name”>
    ConfigMap Name: <”Your ConfigMap name”>
    then, click Add New Config Map.

    volume3

  7. Insert new ConfigMap data that’s for nginx config file.
    Key: “default.conf”
    Content: you can use content of default.conf file below.

    Given root and index are root contents of your app (html) from GitHub for nginx.

    # default.conf
    server {
    listen       80;
    server_name  localhost;
    
    location / {
        root   /App;
        index  index.nginx-debian.html;
    }
        
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }
    
    location /_hook {
        default_type text/html;
        content_by_lua_block {
            os.execute("/bin/webhook.sh")
            ngx.say("<p>pull complete !!</p>")
        } 
      }
    }
    


    volume4


  8. Back to Stateful Set, Add new volume mounts for mounts default.conf from configMap to /etc/nginx/conf.d .
    Volume Name: <”select your configMap name”>
    Mount Point: “/etc/nginx/conf.d”

    volume6


  9. Click Deploy.
    deploy

  10. Now we got new workload that’s contain webhook container inside pod.

    workload


  11. Scroll down to services and click link for view your web page has deploy.

    deployment1

  1. Go to your project on GitHub, then click setting select webhook menu and click Add webhook.

    hook1


  2. Change Payload URL to your domain name and append with /_hook.
    Select Content type to application/json.
    Select Just the push event. for git request to trigger your website and pull contents when every push event of your project.
    Click Add webhook.

    hook2

  3. Now, you complete to set webhook.

    hook3


  4. Test git webhook by change content project and push.
    <!DOCTYPE html>
    <html>
    <head>
    <title>Sawadee to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Hello World !</h1>
    </body>
    </html>


Now, Content of your website will change to below picture.

hello


  1. Click Edit your workload.

    clickEdit


  2. Add your PHP Application image by click on Add New Container. Then fill Container Name, Image Name and set port of your application will listen to in this tutorial will be 8000.
    Container Name: <”your container name”>
    Image Name: “maxoatzadn/php-server”
    (This’s php-apache image running on port 8000)

    Warning! Webhook base on nginx running on port 80 and care about web server port make sure your application is not running on port 80 too. In this tutorial we use modify image from php:7.2-apache.


    addContainerPhp

  3. Create new volume, type Persistent Volume Claim. which will hold your source code
    Volume Name: <”Your PVC Volume name”>
    PVC Name: <”Your PVC name”>
    then, click Add New PVC.

    addNewVolume

  4. Select Storage Class.
    Storage Class: “rbd-r2”

    storageClass


  5. We will make previously created PVC available on both webhook container and php application container. In Stateful Set menu, click Add New Volume Mount on each container to create a mount point of webhook container and web server container (php). Same volume content will available between two container.

    • Mount of webhook
      Volume Name: <”select your PVC name”>
      Mount Point: “/App”
      mountWebhook


    • Mount of web server
      Volume Name: <”select your PVC name”>
      Mount Point: “/var/www/html”
      mountPhp

  6. For next, lets open port 8000 of service.

    editServices


  7. Go to Config Map change code to below.

      # default.conf
      server {
      listen       80;
      server_name  localhost;
    
      location / {
         # root   /App;
         # index  index.nginx-debian.html;
         # add proxy_pass to web server that's running at port 8000.
         proxy_pass http://localhost:8000;
      }
    
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/local/openresty/nginx/html;
      }
    
      location /_hook {
          default_type text/html;
          content_by_lua_block {
              os.execute("/bin/webhook.sh")
              ngx.say("<p>pull complete !!</p>")
          } 
        }
      }
    
  8. Save Workload

  9. Click link service to view your website.

    clickLinkService

  10. push index.php to your GitHub.

       # index.php
       <?php
       echo phpinfo();
       ?>
    

    Now, you can see content of your website.

    phpWeb

Reference : https://hub.docker.com/r/maxoatzadn/webhook

Good luck … ✌️