Skip to content

Commit

Permalink
feat(slack): add a workflow about github status
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbriere1 committed Feb 19, 2025
1 parent ae4fda6 commit 8894111
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
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.
106 changes: 106 additions & 0 deletions .github/workflows/docs/send_slack_gha_status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Slack Github Actions Status Workflow

This workflow provides a standardized interface for sending GitHub Actions deployment status messages to Slack channels, with a predefined message format that includes repository, environment, and deployment details.

## Usage

```yaml
jobs:
notify:
uses: ZeroGachis/.github/.github/workflows/send_slack_gha_status.yaml@v4
with:
channel_id: "C0123456789"
environment_name: production
status: success # Optional, defaults to "success"
secrets: inherit
```
## Inputs
### Required Inputs
| Input | Description |
| ------------------ | ---------------------------------------------------------------------------------- |
| `channel_id` | Slack channel ID to send the message to (Can be find in Channel info in Slack app) |
| `environment_name` | Target environment for Vault configuration |

### Optional Inputs

| Input | Default | Description |
| --------------------------- | --------- | ------------------------------------------------------------ |
| `vault_url` | - | Vault server URL |
| `vault_github_actions_role` | - | Role to use for Vault authentication |
| `status` | "success" | Deployment status ("success" or any other value for failure) |

## Features

- 🚀 Standardized deployment status messages
- 📊 Rich message formatting with repository and deployment details
- 👤 Automatic inclusion of triggering user
- 🔗 Direct links to workflow run and commit
- 🎨 Color-coded status indicators
- 🔒 Secure credential management via HashiCorp Vault
- 🌐 Environment-specific configuration

## Example Usage

### Basic Status Message

```yaml
jobs:
notify-success:
uses: ZeroGachis/.github/.github/workflows/send_slack_gha_status.yaml@v4
with:
channel_id: "C0123456789"
environment_name: staging
status: success
secrets: inherit
```

### Failure Status Message

```yaml
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Deploy application
run: ./deploy.sh
notify-failure:
needs: deploy
if: failure()
uses: ZeroGachis/.github/.github/workflows/send_slack_gha_status.yaml@v4
with:
channel_id: "C0123456789"
environment_name: production
status: failure
secrets: inherit
```

This example shows how to automatically send a failure notification when a job fails. The `if: failure()` condition ensures the notification is only sent if the `deploy` job fails.

### Message Example

![Slack GHA Success example](resources/slack_gha_status_success.png)

_Example of a successful deployment status message in Slack_

![Slack GHA Failure example](resources/slack_gha_status_failure.png)

_Example of a failed deployment status message in Slack_

### Message Format

The message includes:

- Repository name and link
- Environment name
- Deployment status (with emoji indicators)
- Triggering user
- Links to workflow run and commit
- Color-coded status (green for success, red for failure)

![Status message example](resources/slack_message_example.png)

_Example of a deployment status message in Slack_
81 changes: 81 additions & 0 deletions .github/workflows/send_slack_gha_status.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Send Slack Github Status

on:
workflow_call:
inputs:
channel_id:
required: true
type: string
environment_name:
required: true
type: string
vault_url:
required: false
type: string
vault_github_actions_role:
required: false
type: string
status:
required: false
type: string
default: "success"

jobs:
send-slack-gha-status:
uses: ZeroGachis/.github/.github/workflows/send_slack_notif.yaml@feature/pla-2052
with:
environment_name: ${{ inputs.environment_name }}
vault_url: ${{ inputs.VAULT_URL || vars.VAULT_URL || vars.PULLREQUEST_VAULT_URL }}
vault_github_actions_role: ${{ inputs.vault_github_actions_role || vars.VAULT_GITHUB_ACTIONS_ROLE }}
payload: |
{
"channel": ${{ inputs.channel_id }},
"attachments": [
{
"color": "${{ inputs.status == 'success' && '2eb67d' || 'e01e5a' }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "${{ inputs.status == 'success' && '✅' || '🔥' }} Deployment Status: ${{ github.event.repository.name }}",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Repository:* <${{ github.event.repository.html_url }}|${{ github.event.repository.name }}>\n*Environment:* `${{ inputs.environment_name }}`"
}
},
{
"type": "divider"
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Status:*\n${{ inputs.status == 'success' && '✅ Success' || '🔥 Failure' }}"
},
{
"type": "mrkdwn",
"text": "*Triggered by:*\n👤 `${{ github.actor }}`"
}
]
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Workflow:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Workflow Run ↗️>"
}
]
}
]
}
]
}
secrets: inherit
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Below is the list of available templates and associated documentations:
| Run Python | `.github/workflows/run-python.yml` | Executes Python scripts | [Documentation](.github/workflows/docs/run-python.md) |
| Run Pytest | `.github/workflows/run-pytest.yml` | Runs Python tests | [Documentation](.github/workflows/docs/run-pytest.md) |
| Send Slack Notification | `.github/workflows/send_slack_notif.yaml` | Sends Slack notifications | [Documentation](.github/workflows/docs/send-slack-notification.md) |
| Send Slack Message | `.github/workflows/send_slack_message.yaml` | Sends Slack messages | [Documentation](.github/workflows/docs/send_slack_message.md) |
| Send Slack Github Actions Status | `.github/workflows/send_slack_gha_status.yaml` | Sends Slack notifications | [Documentation](.github/workflows/docs/send_slack_gha_status.md) |
| Terraform | `.github/workflows/terraform.yml` | Manages Terraform infrastructure | [Documentation](.github/workflows/docs/terraform.md) |
| Terraform Check Kube Enabled | `.github/workflows/terraform_check_kube_enabled.yml` | Checks Kubernetes enablement for Terraform | [Documentation](.github/workflows/docs/terraform-check-kube-enabled.md) |

Expand Down

0 comments on commit 8894111

Please sign in to comment.