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

Add send-slack-notification #43

Merged
merged 1 commit into from
Nov 5, 2024
Merged
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ jobs:
secrets: inherit
```

## Send Slack notification

```yml
jobs:
sample:
steps:
- uses: gsoft-inc/wl-reusable-workflows/send-slack-notification@main
with:
webhook_url: ${{secrets.SLACK_WEBHOOK_URL_IDP_DEV_ALERTS}}
# Use either text or messageTemplate
text: Sample message
messageTemplate: "FailedJob" # Support "", "FailedJob"
```

## Terraform checks

This workflow runs TF-Lint to find issues in the code, Terraform-Docs to create a README and Terraform FMT to format the code.
Expand Down
34 changes: 34 additions & 0 deletions send-slack-notification/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Post slack message"

inputs:
webhook_url:
required: true
text:
default: ""
messageTemplate:
description: "One of the supported template name: 'FailedJob'"
default: ""
type: choice
options:
- ""
- "FailedJob"

runs:
using: "composite"
steps:
- shell: pwsh
run: |
$text = $null
$template = "${{inputs.messageTemplate}}"
if ($template -eq "FailedJob") {
$text = "❌ ${{github.repository}}: The workflow '${{github.workflow}}' failed. Logs: https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}"
}

if (-not $text) {
# Attempt to avoid escaping issue when the value contains quotes or PowerShell specific characters
$text = @"
${{toJson(inputs.text)}}
"@ | ConvertFrom-Json
}

Invoke-RestMethod -Uri "${{inputs.webhook_url}}" -Headers @{ "Content-Type" = "application/json" } -Method Post -Body (@{ text = $text } | ConvertTo-Json)