diff --git a/examples/failure-notification/CHANGELOG.md b/examples/failure-notification/CHANGELOG.md new file mode 100644 index 00000000..27385e03 --- /dev/null +++ b/examples/failure-notification/CHANGELOG.md @@ -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. diff --git a/examples/failure-notification/assets/icon.webp b/examples/failure-notification/assets/icon.webp new file mode 100644 index 00000000..3112d1fa Binary files /dev/null and b/examples/failure-notification/assets/icon.webp differ diff --git a/examples/failure-notification/assets/slack.png b/examples/failure-notification/assets/slack.png new file mode 100644 index 00000000..e5c4755b Binary files /dev/null and b/examples/failure-notification/assets/slack.png differ diff --git a/examples/failure-notification/assets/workflow.png b/examples/failure-notification/assets/workflow.png new file mode 100644 index 00000000..5c7b7bc6 Binary files /dev/null and b/examples/failure-notification/assets/workflow.png differ diff --git a/examples/failure-notification/versions/0.0.1/README.md b/examples/failure-notification/versions/0.0.1/README.md new file mode 100644 index 00000000..a9bb2361 --- /dev/null +++ b/examples/failure-notification/versions/0.0.1/README.md @@ -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. diff --git a/examples/failure-notification/versions/0.0.1/docs/failure-notification.md b/examples/failure-notification/versions/0.0.1/docs/failure-notification.md new file mode 100644 index 00000000..ec9c021b --- /dev/null +++ b/examples/failure-notification/versions/0.0.1/docs/failure-notification.md @@ -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: + - 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) diff --git a/examples/failure-notification/versions/0.0.1/workflowTemplate.yaml b/examples/failure-notification/versions/0.0.1/workflowTemplate.yaml new file mode 100644 index 00000000..b3f8b7fe --- /dev/null +++ b/examples/failure-notification/versions/0.0.1/workflowTemplate.yaml @@ -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: 'lukas.goodfellow@codefresh.io' + 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: + 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: + 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 + 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 \ No newline at end of file