Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Added e-mail feedback from the broken pipeline

Michal Rutkaacced348afb4
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d542b8af0..97c8d3990 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -5,6 +5,13 @@ default:
- docker:dind
timeout: 1h 30m
+stages:
+ - build
+ - build_fail
+ - test
+ - deploy
+ - dast
+
lint-Dockerfile:
stage: build
script:
@@ -61,6 +68,27 @@ build-master:
only:
- master
+# This step is executed when build fails. The purpose is to e-mail a git commiter about the problem.
+email failed pipeline:
+ stage: build_fail
+ image: docker:latest
+ script:
+ - docker pull registry.gitlab.com/openremote/openremote:master
+ - export WHO=$(docker run --rm -v $(pwd):/or registry.gitlab.com/openremote/openremote:master git log --pretty="%ce" -1)
+ - export WHEN=$(docker run --rm -v $(pwd):/or registry.gitlab.com/openremote/openremote:master git log --pretty="%ad %ar" -1)
+ - export WHAT=$(docker run --rm -v $(pwd):/or registry.gitlab.com/openremote/openremote:master git log --pretty="%h %s" -1)
+ - export REPO=$(docker run --rm -v $(pwd):/or registry.gitlab.com/openremote/openremote:master git remote get-url origin|xargs basename)
+ - echo $REPO
+ - docker run -v $(pwd):/usr/src/myapp -w /usr/src/myapp
+ -e "WHO=$WHO"
+ -e "WHAT=$WHAT"
+ -e "WHEN=$WHEN"
+ -e "REPO=$REPO"
+ -e "SMTP_USERNAME=$SMTP_USERNAME"
+ -e "SMTP_PASSWORD=$SMTP_PASSWORD"
+ --rm python:3 python deployment/aws-mail.py
+ when: on_failure
+
test-master:
before_script:
- docker run -v $(pwd):/or
diff --git a/deployment/aws-mail.py b/deployment/aws-mail.py
new file mode 100644
index 000000000..1c1adc746
--- /dev/null
+++ b/deployment/aws-mail.py
@@ -0,0 +1,35 @@
+#!/usr/bin/python3
+
+import smtplib
+import os
+
+sender = '[email protected]'
+receivers = [os.environ['WHO']]
+subject = 'Gitlab pipeline FAILED: '+os.environ['WHAT']
+
+print(sender,receivers, subject)
+message = '\r\n'.join(['From: %s' % sender,
+ 'To: %s' % receivers[0],
+ 'Subject: %s' % subject,
+ """
+The GitLab CI/CD pipeline FAILED for {repo}
+
+{what}
+{when}
+{who}.
+""".format(when=os.environ['WHEN'], who=os.environ['WHO'], what=os.environ['WHAT'], repo=os.environ['REPO'])
+ ])
+
+try:
+ server = smtplib.SMTP('email-smtp.eu-west-1.amazonaws.com',587)
+ server.ehlo()
+ server.starttls()
+ server.login(os.environ['SMTP_USERNAME'], os.environ['SMTP_PASSWORD'])
+
+ server.sendmail(sender, receivers, message)
+ print ("Successfully sent email")
+except Exception as ex:
+ print ("Error: unable to send email for "+os.environ['SMTP_USERNAME'])
+
+ template = "An exception of type {0} occurred. Arguments:\n{1!r}"
+ print (template.format(type(ex).__name__, ex.args))