I'm looking for an option to monitor and create alerts whenever a Cloud Build fail - using Stackdriver.
Lots of people are solving this using Pub/Sub and Cloud Functions (example) but I perfer to have all monitoring/alerting within Stackdriver. Stackdriver does have a Resource type named Cloud Build
but there are no Metrics
that makes any sense in terms of detecting a failed build. I'm guessing this is a missing feature?
Second option would be to analyze GCP logs (Log Viewer) and trigger an alert based on those events instead. The logs are there, but how can I utilize them inside Stackdriver?
The log based metric suggested by @yuri-grinshteyn is the right way to achieve this. Go to logging page, on the right of the search field, click on the arrow and switch to advance filter.
Then paste this example
resource.type="build"
severity="ERROR"
This search only for ERROR
level logs in the Cloud Build resource
. Now, click on the upper left corner Create Metric
. Simply name it and click on Create metric
.
Now you are in the log based metric section. Click on the "3-vertical-align-dots" and select Create alert on Metric
. Now there is a redirection to Stackdriver for creating an alert. Set none
aggregator, the threshold that you want (if you want to be alerted on each fail, select 1). Save and, select your notification method and so on.