From 9e982c6abc55f55bb7495fa35f21f4bdd8dd38cc Mon Sep 17 00:00:00 2001 From: ashwini Date: Thu, 3 Jan 2019 10:41:09 -0500 Subject: [PATCH] Report status to github --- .gitlab-ci.yml | 39 +++++++++++++++++++++++++++++---------- reportCiResult.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 reportCiResult.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 52dc597ae97..6dc6d8003b7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,16 +1,17 @@ stages: -- runall +- runall-and-report-to-github-pending - build - collate - -# This is just a config to help trigger rest of the builds -run_all_builds: - image: ubuntu:latest - stage: runall - variables: - GIT_STRATEGY: none +- report-to-github-done + +############################################################################### +# report result to github +############################################################################### +runall-and-report-to-github-pending: + image: python:2.7 + stage: runall-and-report-to-github-pending script: - - pwd + - python reportCiResult.py "gitlab-ci" "pending" when: manual allow_failure: false @@ -235,4 +236,22 @@ collate_builds: artifacts: paths: - collectedbuilds/builds.7z - expire_in: 1 week \ No newline at end of file + expire_in: 1 week + + +############################################################################### +# report result to github +############################################################################### +report-to-github-done:failure: + image: python:2.7 + when: on_failure + stage: report-to-github-done + script: + - python reportCiResult.py "gitlab-ci" "failure" + +report-to-github-done:success: + image: python:2.7 + when: on_success + stage: report-to-github-done + script: + - python reportCiResult.py "gitlab-ci" "success" \ No newline at end of file diff --git a/reportCiResult.py b/reportCiResult.py new file mode 100644 index 00000000000..a23345a11c6 --- /dev/null +++ b/reportCiResult.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python2.7 + +import os +import sys +import textwrap + +def post_to_github(context, state, description=None): + if 'GITHUB_TOKEN' not in os.environ: + print 'NOT posting results to GitHub ($GITHUB_TOKEN not available)' + return + + payload = dict( + context=context, + state=state, + #target_url="{CI_PROJECT_URL}/-/jobs/{CI_BUILD_ID}".format(**os.environ), + target_url="{CI_PROJECT_URL}/pipelines/{CI_PIPELINE_ID}".format(**os.environ), + ) + + if description: + payload.update(dict(description=description)) + + import requests + print 'sending status to github...' + print 'sending to: {GITHUB_REPO_API}/statuses/{CI_COMMIT_SHA}'.format(**os.environ) + print 'Bearer {GITHUB_TOKEN}'.format(**os.environ) + response = requests.post( + '{GITHUB_REPO_API}/statuses/{CI_COMMIT_SHA}'.format(**os.environ), + headers={'Authorization': 'Bearer {GITHUB_TOKEN}'.format(**os.environ)}, + json=payload, + ) + print response.text + +os.system('pip install requests') +post_to_github(context=sys.argv[1], state=sys.argv[2]) -- GitLab