|
| 1 | +Tutorials |
| 2 | +========== |
| 3 | + |
| 4 | +Here are some tutorials on how to use gitWebhook. |
| 5 | +All tutorials assume you have git installed on your system, and initialized in the directory you are working in. |
| 6 | +This repository must be configured to connect via SSH to the remote repository using pre-configured SSH keys so that the webhook can just call:: |
| 7 | + |
| 8 | + git pull |
| 9 | + |
| 10 | +with no issues. |
| 11 | + |
| 12 | +Automatic deployment with GitHub using Webhooks |
| 13 | +------------------------------------------------ |
| 14 | + |
| 15 | +In this tutorial, we will setup a simple automatic deployment system using GitHub Webhooks. |
| 16 | +This will allow you to automatically deploy your code to your server whenever you push to your GitHub repository. |
| 17 | + |
| 18 | +First we are going to set up the backend server that will receive the webhook requests from GitHub. |
| 19 | +We need to have gitWebhook installed on the server, so run:: |
| 20 | + |
| 21 | + pip install gitAppWebhook |
| 22 | + |
| 23 | +to make sure you have both Flask and gitWebhook installed. |
| 24 | +Then create a new file called `webhook.py` and add the following code:: |
| 25 | + |
| 26 | + from gitWebhook import pullerWebhookBlueprint |
| 27 | + from flask import Flask |
| 28 | + |
| 29 | + TOKEN = "" |
| 30 | + |
| 31 | + app = Flask(__name__) |
| 32 | + wb = pullerWebhookBlueprint(token, url_prefix="/") |
| 33 | + app.register_blueprint(wb) |
| 34 | + |
| 35 | + if __name__ == "__main__": |
| 36 | + app.run() |
| 37 | + |
| 38 | +This code once run will start a Flask server that listens for POST requests on the root URL from either GitHub, GitLab or Gitea using :class:`pullerWebhookBlueprint`. |
| 39 | +The `TOKEN` variable is the secret token that you will use to authenticate the requests from GitHub. |
| 40 | +You can set this to any string you want, but make sure it is a strong secret. |
| 41 | +You can also set the `url_prefix` to any URL you want, but for this tutorial we will use the root URL. |
| 42 | + |
| 43 | +You can now run the server by running the following command:: |
| 44 | + |
| 45 | + python webhook.py |
| 46 | + |
| 47 | +This will start the server on the default Flask url and port. |
| 48 | +Naturally if you are running this on a production server, you will want to use a proper WSGI server like Gunicorn or uWSGI. |
| 49 | +Flask production deployment is outside the scope of this tutorial, but you can find more information in the `Flask documentation <https://flask.palletsprojects.com/en/1.1.x/deploying/>`_. |
| 50 | + |
| 51 | +After the server is running all that is left is to create a webhook on GitHub. |
| 52 | +Go to your repository on GitHub and click on the `Settings` tab. |
| 53 | +Then click on the `Webhooks` tab on the left side of the page. |
| 54 | +Click on the `Add webhook` button and you will be presented with a form to fill in the details of the webhook. |
| 55 | +Don't forget to set the `Content type` to `application/json` and the `Secret` to the same value as the `TOKEN` variable in the `webhook.py` file. |
| 56 | + |
| 57 | +GitHub has a fairly well documented and easy to use webhook interface, so you can find more information on how to set up a webhook on GitHub in the `GitHub documentation <https://developer.github.com/webhooks/creating/>`_. |
| 58 | +If you have configured everything correctly you should now be able to push to your repository and see the changes automatically deployed to your server. |
| 59 | + |
| 60 | +How to setup automatic testing with gitWebhook |
| 61 | +---------------------------------------------- |
| 62 | + |
| 63 | +This example is very similar to the previous one, but alongside deploying the code we will also run tests on the code. |
| 64 | +Like previously we will start by setting up the backend server that will receive the webhook requests from GitHub. |
| 65 | +We need to have gitWebhook installed on the server, so run:: |
| 66 | + |
| 67 | + pip install gitAppWebhook |
| 68 | + |
| 69 | +and then create a new file called `webhook.py` and add the following code:: |
| 70 | + |
| 71 | + from gitWebhook import pullerWebhookBlueprint |
| 72 | + from flask import Flask |
| 73 | + from tests import yourTestSuite |
| 74 | + |
| 75 | + TOKEN = "" |
| 76 | + |
| 77 | + app = Flask(__name__) |
| 78 | + wb = pullerWebhookBlueprint(token, url_prefix="/", tests=yourTestSuite) |
| 79 | + |
| 80 | + if __name__ == "__main__": |
| 81 | + app.run() |
| 82 | + |
| 83 | +After that you need to create a new file called `tests.py` and add the following code:: |
| 84 | + |
| 85 | + import unittest |
| 86 | + |
| 87 | + class YourTestSuite(unittest.TestCase): |
| 88 | + def test_something(self): |
| 89 | + self.assertTrue(True) |
| 90 | + |
| 91 | + yourTestSuite = unittest.TestLoader().loadTestsFromTestCase(YourTestSuite) |
| 92 | + |
| 93 | +This code will start a Flask server that listens for POST requests on the root URL from either GitHub, GitLab or Gitea using :class:`pullerWebhookBlueprint`. |
| 94 | +Upon receiving a request, the server will pull the changes from git, run the tests in the `tests.py` file, return the result to GitHub and if the tests failed return the repository to the pre merge state. |
0 commit comments