Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure Notification Example Via Slack #435

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/failure-notification/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## v0.0.1 (2022.09.30)

### failure notifications

This Workflow Template is an example of parsing and sending a slack notification on failure of a worklow.
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved
Binary file added examples/failure-notification/assets/slack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions examples/failure-notification/versions/0.0.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Failure Notification

## Summary

This Workflow Template is an example of parsing and sending a slack notification on failure of a worklow.

## Templates

1. [failure-notification](https://github.com/codefresh-io/argo-hub/blob/main/examples/failure-notification/versions/0.0.1/docs/failure-notification.md)

## Security

This Utilizizes the codefresh-sa service account that is available with the codefresh hybrid installation. Also, the service account from the Slack Notification worklow.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# failure-notifications
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved

## Summary

This Workflow template is an example on how to parse and send a slack notification upon fialure of a workflow.
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved

## Template Breakdown

### Workflow Spec

To utilize failure notification you will need to set the `spec.onExit` field to a template. This will run at the end of a workflow no matter the status.

```yaml
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: failure-notification-example
spec:
onExit: failure-notification # invoke pipeline-hook template at end of the workflow
```

### Workflow Templates

#### Parsing Template

We need to parse the information from the variable `workflow.failures` and output them to smaller items to make it more manageable instead of a raw json. We are extracting the failure message, the template name that failed, and the timestamp.

```yaml
# This Steps parses the information to be utlized in the slack notifcation.
- name: parse-info
serviceAccountName: codefresh-sa
script:
image: alpine
command: [sh]
source: |
apk add jq
echo {{workflow.failures}} | jq -r '.[].message' > /tmp/message.txt
echo {{workflow.failures}} | jq -r '.[].templateName' > /tmp/templateName.txt
echo {{workflow.failures}} | jq -r '.[].finishedAt' > /tmp/finishedAt.txt
outputs:
parameters:
- name: message
valueFrom:
path: /tmp/message.txt
- name: templateName
valueFrom:
path: /tmp/templateName.txt
- name: finishedAt
valueFrom:
path: /tmp/finishedAt.txt
```

#### Notification Template

This template will run upon exit of the workflow. To make sure this is a failure notification we utlize the `when` parameter as `when: "{{workflow.status}} != Succeeded"`. This will make sure that only Errors / Failure will send the notification.

This template first use the parsing template of the failure. Then utlize the `send-message` template from `argo-hub.slack.0.0.2`.

```yaml
# After the completion of the entrypoint template, the status of the
# workflow is made available in the global variable {{workflow.status}}.
# {{workflow.status}} will be one of: Succeeded, Failed, Error
# useing slack from https://codefresh.io/argohub/workflow-template/slack
- name: failure-notification
steps:
- - name: parse-failure-info
template: parse-info
when: "{{workflow.status}} != Succeeded"
- - name: slack-notification
templateRef:
name: argo-hub.slack.0.0.2
template: send-message
arguments:
parameters:
- name: SLACK_HOOK_URL
value: <INSERT SLACK URL>
- name: SLACK_TEXT
value: "Workflow {{workflow.name}} has {{workflow.status}}. The template {{steps.parse-failure-info.outputs.parameters.templateName}} had the error of {{steps.parse-failure-info.outputs.parameters.message}} at {{steps.parse-failure-info.outputs.parameters.finishedAt}}"
when: "{{workflow.status}} != Succeeded"
```

## Example

Workflow Failed with Exit Code 1.

![workflow failure](../../../assets/workflow.png)
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved

Slack notification that gets sent.

![workflow failure](../../../assets/slack.png)
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved
58 changes: 58 additions & 0 deletions examples/failure-notification/versions/0.0.1/workflowTemplate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: failure-notification-example
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved
spec:
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved
entrypoint: intentional-fail
onExit: failure-notification # invoke pipeline-hook template at end of the workflow
templates:
# primary workflow template
- name: intentional-fail
container:
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved
image: alpine:latest
command: [sh, -c]
args: ["echo intentional failure; exit 1"]

# After the completion of the entrypoint template, the status of the
# workflow is made available in the global variable {{workflow.status}}.
# {{workflow.status}} will be one of: Succeeded, Failed, Error
# useing slack from https://codefresh.io/argohub/workflow-template/slack
- name: failure-notification
steps:
- - name: parse-failure-info
template: parse-info
when: "{{workflow.status}} != Succeeded"
- - name: slack-notification
templateRef:
name: argo-hub.slack.0.0.2
template: send-message
arguments:
parameters:
- name: SLACK_HOOK_URL
value: <INSERT SLACK URL>
- name: SLACK_TEXT
value: "Workflow {{workflow.name}} has {{workflow.status}}. The template {{steps.parse-failure-info.outputs.parameters.templateName}} had the error of {{steps.parse-failure-info.outputs.parameters.message}} at {{steps.parse-failure-info.outputs.parameters.finishedAt}}"
when: "{{workflow.status}} != Succeeded"

# This Steps parses the information to be utlized in the slack notifcation.
- name: parse-info
serviceAccountName: codefresh-sa
lukasmagik marked this conversation as resolved.
Show resolved Hide resolved
script:
image: alpine
command: [sh]
source: |
apk add jq
echo {{workflow.failures}} | jq -r '.[].message' > /tmp/message.txt
echo {{workflow.failures}} | jq -r '.[].templateName' > /tmp/templateName.txt
echo {{workflow.failures}} | jq -r '.[].finishedAt' > /tmp/finishedAt.txt
outputs:
parameters:
- name: message
valueFrom:
path: /tmp/message.txt
- name: templateName
valueFrom:
path: /tmp/templateName.txt
- name: finishedAt
valueFrom:
path: /tmp/finishedAt.txt