Code Monkey home page Code Monkey logo

webhook's Introduction

Purpose

This project automates the update process of Docker-based project when the source code changes on github. It is designed to listen to webhook calls from github, and to execute a script in response. The test performed was to update processes that run in the same host machine

The scenario is:

  • Github is used as a repository for your code
  • You pulled the code to a machine and brought the service up using docker-compose up
  • You want to automatically rebuild the service if the source code is updated in github

How it works:

  • The code runs directly on the machine, not on a container because we need to run a scripts on the host machine and containers are isolated for security reasons.
  • We will use node.js to run the service by listening to a port and triggering scripts that can be customized by repository. For the service to launch automatically after a machine reboot or a crash, we will use pm2.

Installation:

  • In the same machine that your project is running, goto the directory of your choice.
  • Clone this repository (it will create the webhook directory)
  • cd webhook
  • mv .env_example .env
  • nano .env and
    • enter a secret, this secret will add later to github
    • choose the port
  • nano config.json
    • you can have as many projects as you want
    • project is the github repository name
    • path is the project directory in the local machine
    • script is the name of the script file that is to reside on the path directory
  • Now install the script on the directory of your code and make it executable
    • cp webhook_example.js /the_path_of_your_code/webhook.sh
    • chmod +x /the_path_to_your_code/webhook.sh
  • Now we need to configure node
    • Install node in your local machine sudo apt install node npm
    • Initialize node node init
    • Install the needed packages npm i
  • Running the server
    • Option 1 if you do not want the server to restart after a reboot or crash
      • node server.js
    • Option 2 to run with automatic restart (WARNING NEED FURTHER TESTING)
      • Start your server with pm2
        • npm i pm2 -g <-- I had to install this OUTSIDE the project...
        • pm2 startup <-- I also believe this needs to be done outside the project
        • pm2 start server.js
        • pm2 save

Configuring github to send webhook posts to your webhook server

  • Goto to your repository page
  • Click on settings
  • Click on Webhooks
  • Click on add webhook (it may prompt you to authenticate)
  • Fill the form
    • Payload URL: https:///<repository_name>
      • repository_name has to match the project name on the config.json
    • Content type: application/json
    • Secret: <the exact secret you entered in the .env file>
    • Leave the defaults and click Add webhook

That's it!!!

webhook's People

Contributors

rphllc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.