From b9faa961564fb7aac36d72a40624eb6cc9191fdd Mon Sep 17 00:00:00 2001 From: voltwu Date: Sat, 19 Dec 2020 02:32:54 -0800 Subject: [PATCH 1/3] add tag support --- config.py.example | 5 ++++- dockerhook/handler.py | 15 ++++++++++----- requirements.txt | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/config.py.example b/config.py.example index 2f592ac..927e17a 100644 --- a/config.py.example +++ b/config.py.example @@ -7,4 +7,7 @@ APIKEY = 'secret' # Add your script using the name of your repository as the key -HOOKS = {'testpush': 'scripts/test.sh'} +HOOKS = { + 'repo1': {'default':'tests_script2.sh','latest':'test_tags_script_latest.sh'}, + 'repo2': {'default':'tests_script.sh','2.0':'test_tags_script.sh'} + } diff --git a/dockerhook/handler.py b/dockerhook/handler.py index 849237b..a878eb5 100644 --- a/dockerhook/handler.py +++ b/dockerhook/handler.py @@ -88,9 +88,14 @@ def run_script(cls, json_data): Returns: Dict: JSON Response """ - hook = json_data['repository']['name'] - script_args = shlex.split(app.config['HOOKS'][hook]) - app.logger.info("Subprocess [%s]: %s", hook, script_args) + repname = json_data['repository']['name'] + tagname = json_data['push_data']['tag'] + repsDic = app.config['HOOKS'][repname] + hk_tag_name = tagname + if tagname not in repsDic: + hk_tag_name = 'default' + script_args = shlex.split(app.config['HOOKS'][repname][hk_tag_name]) + app.logger.info("Subprocess [%s (tag: %s)]: %s", repname,hk_tag_name, script_args) try: script_process = subprocess.Popen( @@ -107,11 +112,11 @@ def run_script(cls, json_data): app.logger.error('Exception occured: %s', str(exception)) app.logger.info('Subprocess failed.') res = cls.create_response( - 'error', '500', '{} failed.'.format(hook)) + 'error', '500', '{} {} failed.'.format(repname,hk_tag_name)) else: # no exception raised. res = cls.create_response( - 'success', '200', '{} deployed.'.format(hook)) + 'success', '200', '{} {} deployed.'.format(repname,hk_tag_name)) app.logger.info('Script completed successfully.') cls.callback(res, json_data['callback_url']) diff --git a/requirements.txt b/requirements.txt index 688d0ce..ba0d8cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -requests==2.14.2 -Flask==0.12.2 +requests +Flask From a4a9dde5bcf252e90aa447c5c6cbc7d606b837d2 Mon Sep 17 00:00:00 2001 From: David <45677662+voltwu@users.noreply.github.com> Date: Sat, 19 Dec 2020 21:43:42 +0800 Subject: [PATCH 2/3] Update README.rst --- README.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 926a82c..ecbfb24 100644 --- a/README.rst +++ b/README.rst @@ -70,14 +70,15 @@ Alternatively you can set the ``$DOCKERHOOK_TOKEN`` environment variable with yo key. This will override anything in config.py. The ``HOOKS`` dict in config.py maps respository names to serverside deploy -scripts. Keys are the names of repositories (no namespace), and values are -the full path to the script to be called, or a relative path to the current -working directory. +scripts. Keys are the names of repositories (no namespace), and value is a dict data type, +and the ``default`` means that if not matched a tag. Each tag maps a full path to the script to be called, +or a relative path to the current working directory. .. code-block:: python - HOOKS = {'repo1': '/full/path/to/script.sh', - 'repo2': 'relative_path_to_script.sh' + HOOKS = { + 'repo1': {'default':'tests_script2.sh','latest':'test_tags_script_latest.sh'}, + 'repo2': {'default':'tests_script.sh','2.0':'test_tags_script.sh'} } From 694894f622610cbaa87becb103fcd2fed14f6d8f Mon Sep 17 00:00:00 2001 From: David <45677662+voltwu@users.noreply.github.com> Date: Sat, 19 Dec 2020 23:12:04 +0800 Subject: [PATCH 3/3] Update README.rst --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index ecbfb24..eeaa0f5 100644 --- a/README.rst +++ b/README.rst @@ -43,7 +43,7 @@ from the github repository, the first step is to clone it. .. code-block:: bash - git clone https://github.com/Praisebetoscience/dockerhub-webhook.git + git clone https://github.com/voltwu/docker-hub-webhook.git Then install dependencies.