当前位置 |首页 > 代写教程 > Python教程 >
分享这个代写网站给同学或者朋友吧!

With GitHub, network hooks, developers can create many useful services.Jenkins on a trigger from the examples of continuous integration (CI) task to configure the machine in the cloud, has almost unlimited possibilities.This tutorial will demonstrate how to use Python and Flask framework one simple continuous deployment (CD) service.


In this example of the continuing deployment of the service is a simple application of Flask, which accepts the hooks of GitHub with a network request of REST endpoint to endpoint.In each request from the verification of correct GitHub repository, the server or the pulled - out pull changes to the warehouses of the local copy.Thus each time a new commit is pushed to the remote submission of GitHub repository, the repository will update.


Flask web service

The Flask is trying to build a small web services are very simple.Here may first take a look at the structure of your project.

1
2
3
4
5
├── app
│   ├── __init__.py
│   └── webhooks.py
├── requirements.txt
└── wsgi.py

First, to create the application.Application in the app code directory.

(see init _ _ _ _. py and. py) constitute the Flask.The former includes the creation of Flask and application are added to the configuration of the code.The latter has a logical endpoint to endpoint.This is the application receives a request of the local data on GitHub.

Here is the app _ init _ / _ _. py of content:

1
2
3
4
5
6
7
8
9
10
import os
from flask import Flask
from .webhooks import webhook
def create_app():
 """ Create, configure and return the Flask application """
  app = Flask(__name__)
  app.config['GITHUB_SECRET'= os.environ.get('GITHUB_SECRET')
  app.config['REPO_PATH'= os.environ.get('REPO_PATH')
  app.register_blueprint(webhook)
  return(app)

The function creates the two configuration variables:

_ SECRET save a password, an authentication request to GitHub.

REPO _ PATH save path for an automated warehouse.

This code uses the Flask Flask Blueprints blueprint to organize the application of the endpoint to the endpoint.Blueprint can be used on the API into logical groups, and make that application easy to maintain.This is generally considered a good practice.

here is an app /. py of content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import hmac
from flask import request, Blueprint, jsonify, current_app 
from git import Repo
webhook = Blueprint('webhook', __name__, url_prefix='')
@webhook.route('/github', methods=['POST']) 
def handle_github_hook(): 
 """ Entry point for github webhook """
  signature = request.headers.get('X-Hub-Signature'
  sha, signature = signature.split('=')
  secret = str.encode(current_app.config.get('GITHUB_SECRET'))
  hashhex = hmac.new(secret, request.data, digestmod='sha1').hexdigest()
  if hmac.compare_digest(hashhex, signature): 
    repo = Repo(current_app.config.get('REPO_PATH')) 
    origin = repo.remotes.origin 
    origin.pull('--rebase')
    commit = request.json['after'][0:6]
    print('Repository updated with commit {}'.format(commit))
  return jsonify({}), 200

First code creates a new blueprint.Then it uses the Flask as they added a route endpoint.Any request / GitHub endpoint URL of the POST request will invoke the route.


a verification request

When the service whereon the endpoint receiving the request, it first must verify whether the request is coming from GitHub and correctly from the warehouse.GitHub at the request of the head X - Hub - Signature, there is provided a signature.The signature is made up of a password (_ SECRET), HMAC digest of the requested body hex, and use the sha1 hash generation.

In order to verify the request, that the service needs to be locally calculated signature with the signature of the receipt of the request header.This may be done by the hmac. compare _ digest function is completed.


custom hook logic

In the authentication request, it can now be processed.This tutorial uses a module with a git repository.Repo object module in the warehouse for accessing a remote origin.The service in the local pull vAth the ORIGIN data warehouse and the recent changes, also with rebase - - options to avoid a possible merger.

Debug print statements from receipt of the request for the submission of short hash.This example shows how to use the request body.More on the body of the request data of the available information, query the GitHub document.

Finally, the service returns a JSON string and a status code of 200.This is used to tell the GitHub hook service network has received the request.


deployment service


代写