diff --git a/README.md b/README.md index 79ff755..3db1bf6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/send-slack-notification/action.yml b/send-slack-notification/action.yml new file mode 100644 index 0000000..fe2216f --- /dev/null +++ b/send-slack-notification/action.yml @@ -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) \ No newline at end of file