自定义GitHub的WebHook

背景/介绍

如果我们想利用github搭建cicd平台的话需要使用webhook,即网络钩子。它的作用是当发生一些事件的时候会向目标服务器发送一个请求,通知服务器该仓库发生了变更,具体什么粒度我们可以指定,一般push即可

在这里插入图片描述

搭建简易服务器

from flask import Flask, request

app = Flask(__name__)


@app.route("/ping")
def hello_world():
    return "pong!"


@app.route("/river-webhook",methods=['POST'])
def on_push():
    data = request.get_json()
    print(data)
    return '1111'


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5555)

配置webhook

在这里插入图片描述

返回值

当我们触发push操作的时候会收到github的参数,具体值如下:

{
    
    
    'ref': 'refs/heads/main',
    'before': '015b353d3ac085ed7d9af121299437349301a397',
    'after': '17223505822d8ca78ee3657e9e4f8a710068f74a',
    'repository': {
    
    
        'id': 309353422,
        'node_id': 'MDEwOlJlcG9zaXRvcnkzMDkzNTM0MjI=',
        'name': 'java-',
        'full_name': 'coder-zrl/java-',
        'private': True,
        'owner': {
    
    
            'name': 'coder-zrl',
            'email': '[email protected]',
            'login': 'coder-zrl',
            'id': 64766774,
            'node_id': 'MDQ6VXNlcjY0NzY2Nzc0',
            'avatar_url': 'https://avatars.githubusercontent.com/u/64766774?v=4',
            'gravatar_id': '',
            'url': 'https://api.github.com/users/coder-zrl',
            'html_url': 'https://github.com/coder-zrl',
            'followers_url': 'https://api.github.com/users/coder-zrl/followers',
            'following_url': 'https://api.github.com/users/coder-zrl/following{/other_user}',
            'gists_url': 'https://api.github.com/users/coder-zrl/gists{/gist_id}',
            'starred_url': 'https://api.github.com/users/coder-zrl/starred{/owner}{/repo}',
            'subscriptions_url': 'https://api.github.com/users/coder-zrl/subscriptions',
            'organizations_url': 'https://api.github.com/users/coder-zrl/orgs',
            'repos_url': 'https://api.github.com/users/coder-zrl/repos',
            'events_url': 'https://api.github.com/users/coder-zrl/events{/privacy}',
            'received_events_url': 'https://api.github.com/users/coder-zrl/received_events',
            'type': 'User',
            'site_admin': False
        },
        'html_url': 'https://github.com/coder-zrl/java-',
        'description': '一些很有价值的程序',
        'fork': False,
        'url': 'https://github.com/coder-zrl/java-',
        'forks_url': 'https://api.github.com/repos/coder-zrl/java-/forks',
        'keys_url': 'https://api.github.com/repos/coder-zrl/java-/keys{/key_id}',
        'collaborators_url': 'https://api.github.com/repos/coder-zrl/java-/collaborators{/collaborator}',
        'teams_url': 'https://api.github.com/repos/coder-zrl/java-/teams',
        'hooks_url': 'https://api.github.com/repos/coder-zrl/java-/hooks',
        'issue_events_url': 'https://api.github.com/repos/coder-zrl/java-/issues/events{/number}',
        'events_url': 'https://api.github.com/repos/coder-zrl/java-/events',
        'assignees_url': 'https://api.github.com/repos/coder-zrl/java-/assignees{/user}',
        'branches_url': 'https://api.github.com/repos/coder-zrl/java-/branches{/branch}',
        'tags_url': 'https://api.github.com/repos/coder-zrl/java-/tags',
        'blobs_url': 'https://api.github.com/repos/coder-zrl/java-/git/blobs{/sha}',
        'git_tags_url': 'https://api.github.com/repos/coder-zrl/java-/git/tags{/sha}',
        'git_refs_url': 'https://api.github.com/repos/coder-zrl/java-/git/refs{/sha}',
        'trees_url': 'https://api.github.com/repos/coder-zrl/java-/git/trees{/sha}',
        'statuses_url': 'https://api.github.com/repos/coder-zrl/java-/statuses/{sha}',
        'languages_url': 'https://api.github.com/repos/coder-zrl/java-/languages',
        'stargazers_url': 'https://api.github.com/repos/coder-zrl/java-/stargazers',
        'contributors_url': 'https://api.github.com/repos/coder-zrl/java-/contributors',
        'subscribers_url': 'https://api.github.com/repos/coder-zrl/java-/subscribers',
        'subscription_url': 'https://api.github.com/repos/coder-zrl/java-/subscription',
        'commits_url': 'https://api.github.com/repos/coder-zrl/java-/commits{/sha}',
        'git_commits_url': 'https://api.github.com/repos/coder-zrl/java-/git/commits{/sha}',
        'comments_url': 'https://api.github.com/repos/coder-zrl/java-/comments{/number}',
        'issue_comment_url': 'https://api.github.com/repos/coder-zrl/java-/issues/comments{/number}',
        'contents_url': 'https://api.github.com/repos/coder-zrl/java-/contents/{+path}',
        'compare_url': 'https://api.github.com/repos/coder-zrl/java-/compare/{base}...{head}',
        'merges_url': 'https://api.github.com/repos/coder-zrl/java-/merges',
        'archive_url': 'https://api.github.com/repos/coder-zrl/java-/{archive_format}{/ref}',
        'downloads_url': 'https://api.github.com/repos/coder-zrl/java-/downloads',
        'issues_url': 'https://api.github.com/repos/coder-zrl/java-/issues{/number}',
        'pulls_url': 'https://api.github.com/repos/coder-zrl/java-/pulls{/number}',
        'milestones_url': 'https://api.github.com/repos/coder-zrl/java-/milestones{/number}',
        'notifications_url': 'https://api.github.com/repos/coder-zrl/java-/notifications{?since,all,participating}',
        'labels_url': 'https://api.github.com/repos/coder-zrl/java-/labels{/name}',
        'releases_url': 'https://api.github.com/repos/coder-zrl/java-/releases{/id}',
        'deployments_url': 'https://api.github.com/repos/coder-zrl/java-/deployments',
        'created_at': 1604317489,
        'updated_at': '2020-11-02T11:44:53Z',
        'pushed_at': 1649684177,
        'git_url': 'git://github.com/coder-zrl/java-.git',
        'ssh_url': '[email protected]:coder-zrl/java-.git',
        'clone_url': 'https://github.com/coder-zrl/java-.git',
        'svn_url': 'https://github.com/coder-zrl/java-',
        'homepage': None,
        'size': 0,
        'stargazers_count': 0,
        'watchers_count': 0,
        'language': None,
        'has_issues': True,
        'has_projects': True,
        'has_downloads': True,
        'has_wiki': True,
        'has_pages': False,
        'forks_count': 0,
        'mirror_url': None,
        'archived': False,
        'disabled': False,
        'open_issues_count': 0,
        'license': None,
        'allow_forking': True,
        'is_template': False,
        'topics': [],
        'visibility': 'private',
        'forks': 0,
        'open_issues': 0,
        'watchers': 0,
        'default_branch': 'main',
        'stargazers': 0,
        'master_branch': 'main'
    },
    'pusher': {
    
    
        'name': 'coder-zrl',
        'email': '[email protected]'
    },
    'sender': {
    
    
        'login': 'coder-zrl',
        'id': 64766774,
        'node_id': 'MDQ6VXNlcjY0NzY2Nzc0',
        'avatar_url': 'https://avatars.githubusercontent.com/u/64766774?v=4',
        'gravatar_id': '',
        'url': 'https://api.github.com/users/coder-zrl',
        'html_url': 'https://github.com/coder-zrl',
        'followers_url': 'https://api.github.com/users/coder-zrl/followers',
        'following_url': 'https://api.github.com/users/coder-zrl/following{/other_user}',
        'gists_url': 'https://api.github.com/users/coder-zrl/gists{/gist_id}',
        'starred_url': 'https://api.github.com/users/coder-zrl/starred{/owner}{/repo}',
        'subscriptions_url': 'https://api.github.com/users/coder-zrl/subscriptions',
        'organizations_url': 'https://api.github.com/users/coder-zrl/orgs',
        'repos_url': 'https://api.github.com/users/coder-zrl/repos',
        'events_url': 'https://api.github.com/users/coder-zrl/events{/privacy}',
        'received_events_url': 'https://api.github.com/users/coder-zrl/received_events',
        'type': 'User',
        'site_admin': False
    },
    'created': False,
    'deleted': False,
    'forced': False,
    'base_ref': None,
    'compare': 'https://github.com/coder-zrl/java-/compare/015b353d3ac0...17223505822d',
    'commits': [{
    
    
        'id': '17223505822d8ca78ee3657e9e4f8a710068f74a',
        'tree_id': '48fc74294d84104c25df1a4194613cd17fe244da',
        'distinct': True,
        'message': 'test river',
        'timestamp': '2022-04-11T21:36:17+08:00',
        'url': 'https://github.com/coder-zrl/java-/commit/17223505822d8ca78ee3657e9e4f8a710068f74a',
        'author':
            {
    
    
                'name': 'coder-zrl',
                'email': '[email protected]',
                'username': 'coder-zrl'
            },
        'committer':
            {
    
    
                'name': 'GitHub',
                'email': '[email protected]',
                'username': 'web-flow'
            },
        'added': [],
        'removed': [],
        'modified': ['README.md']
    }],
    'head_commit': {
    
    
        'id': '17223505822d8ca78ee3657e9e4f8a710068f74a',
        'tree_id': '48fc74294d84104c25df1a4194613cd17fe244da',
        'distinct': True,
        'message': 'test river',
        'timestamp': '2022-04-11T21:36:17+08:00',
        'url': 'https://github.com/coder-zrl/java-/commit/17223505822d8ca78ee3657e9e4f8a710068f74a',
        'author':
            {
    
    
                'name': 'coder-zrl',
                'email': '[email protected]',
                'username': 'coder-zrl'
            },
        'committer':
            {
    
    
                'name': 'GitHub',
                'email': '[email protected]',
                'username': 'web-flow'
            },
        'added': [],
        'removed': [],
        'modified': ['README.md']
    }
}

猜你喜欢

转载自blog.csdn.net/m0_46521785/article/details/124110305