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 all commits
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 notification

This Workflow Template is an example of parsing and sending a slack notification on failure of a workflow.
Binary file added examples/failure-notification/assets/icon.webp
Binary file not shown.
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-notification

## Summary

This Workflow template is an example of how to parse and send a slack notification upon failure of a workflow.

## 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: argo-hub.failure-notification-example.0.0.1
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 utilize the `when` parameter as `when: "{{workflow.status}} != Succeeded"`. This will make sure that only Errors / Failures will send the notification.

This template first uses the parsing template of the failure. Then utilize 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](https://raw.githubusercontent.com/codefresh-io/argo-hub//main/examples/failure-notification/assets/workflow.png)

Slack notification that gets sent.

![workflow failure](https://raw.githubusercontent.com/codefresh-io/argo-hub//main/examples/failure-notification/assets/slack.png)
87 changes: 87 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,87 @@
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: argo-hub.failure-notification-example.0.0.1
annotations:
argo-hub/version: '0.0.1'
argo-hub/description: 'Example Workflow Template for Failure Notifcations'
argo-hub/categories: 'argo'
argo-hub/license: 'MIT'
argo-hub/owner_name: 'Luke Goodfellow'
argo-hub/owner_email: '[email protected]'
argo-hub/owner_avatar: 'https://avatars.githubusercontent.com/u/107487942'
argo-hub/owner_url: 'https://github.com/xplrior'
argo-hub/icon_url: 'https://cdn.jsdelivr.net/gh/codefresh-io/argo-hub@main/examples/failure-notification/assets/icon.webp'
argo-hub/icon_background: '#f4f4f4'
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
arguments:
parameters:
- name: SLACK_HOOK_URL
templates:
# primary workflow template
- name: intentional-fail
metadata:
annotations:
argo-hub-template/description: 'This Template is an intentional failure'
argo-hub-template/icon_url: 'https://cdn.jsdelivr.net/gh/codefresh-io/argo-hub@main/examples/failure-notification/assets/icon.webp'
argo-hub-template/icon_background: '#f4f4f4'
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
metadata:
annotations:
argo-hub-template/description: 'This process the failure information and sends a slack notification'
argo-hub-template/icon_url: 'https://cdn.jsdelivr.net/gh/codefresh-io/argo-hub@main/examples/failure-notification/assets/icon.webp'
argo-hub-template/icon_background: '#f4f4f4'
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: '{{ workflow.parameters.SLACK_HOOK_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
metadata:
annotations:
argo-hub-template/description: 'This Template parses the failure information into individual components'
argo-hub-template/icon_url: 'https://cdn.jsdelivr.net/gh/codefresh-io/argo-hub@main/examples/failure-notification/assets/icon.webp'
argo-hub-template/icon_background: '#f4f4f4'
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