Skip to content

Commit ea5ad07

Browse files
committed
Add send-notification for GChat
Issue gh-50
1 parent 2928114 commit ea5ad07

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

.github/actions/send-notification/action.yml

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,65 @@ inputs:
44
webhook-url:
55
description: 'Google Chat Webhook URL'
66
required: true
7-
status:
8-
description: 'Status of the job'
9-
required: true
10-
build-scan-url:
11-
description: 'URL of the build scan to include in the notification'
12-
run-name:
13-
description: 'Name of the run to include in the notification'
14-
default: ${{ format('{0} {1}', github.ref_name, github.job) }}
157
runs:
168
using: composite
179
steps:
18-
- shell: bash
19-
run: |
20-
echo "BUILD_SCAN=${{ inputs.build-scan-url == '' && ' [build scan unavailable]' || format(' [<{0}|Build Scan>]', inputs.build-scan-url) }}" >> "$GITHUB_ENV"
21-
echo "RUN_URL=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> "$GITHUB_ENV"
22-
- shell: bash
23-
if: ${{ inputs.status == 'success' }}
24-
run: |
25-
curl -X POST '${{ inputs.webhook-url }}' -H 'Content-Type: application/json' -d '{ text: "<${{ env.RUN_URL }}|${{ inputs.run-name }}> was successful ${{ env.BUILD_SCAN }}"}' || true
26-
- shell: bash
27-
if: ${{ inputs.status == 'failure' }}
10+
- id: run-info
11+
shell: bash
12+
env:
13+
GH_TOKEN: ${{ github.token }}
2814
run: |
29-
curl -X POST '${{ inputs.webhook-url }}' -H 'Content-Type: application/json' -d '{ text: "<users/all> *<${{ env.RUN_URL }}|${{ inputs.run-name }}> failed* ${{ env.BUILD_SCAN }}"}' || true
15+
# Gather vars only available from github context
16+
commitUrl="${{ github.event.head_commit.url }}"
17+
author="${{ github.event.sender.login }}"
18+
authorUrl=${{ github.event.sender.html_url }}
19+
# Pull additional details from GitHub API
20+
json=$(gh run view ${{ github.run_id }} -R ${{ github.repository }} --json name,number,displayTitle,url,headSha,jobs)
21+
# Determine overall run status (based on all jobs preceding this one)
22+
runStatus=$(echo $json | jq -r '.jobs | .[0:length - 1] | if all(.conclusion == "success" or .conclusion == "skipped") then "succeeded" elif any(.conclusion == "cancelled") then "cancelled" elif any(.conclusion == "failure") then "failed" else "unsuccessful" end')
23+
# Build job info messages, joined in single line (multiline breaks GITHUB_ENV)
24+
jobInfo=$(echo $json | jq -r '.jobs | .[0:length - 1] | map("<" + .url + "|" + .name + "> was " + .conclusion) | join(", ")')
25+
# Get sanitized display title (usually a commit message) from GitHub API
26+
displayTitle=$(echo $json | jq -r .displayTitle)
27+
# Parse workflow yaml file name from full ref name
28+
workflow=$(echo ${{ github.workflow_ref }} | awk -F'/|@' '{print $5}')
29+
# Build workflow URL with branch name
30+
workflowUrl="${{ github.server_url }}/${{ github.repository }}/actions/workflows/${workflow}?query=branch%3A${{ github.ref_name }}"
31+
# Get workflow name from GitHub API (also available as "github.workflow")
32+
workflowName=$(echo $json | jq -r .name)
33+
# Parse 7-digit commit id from head sha
34+
shaId=$(echo $json | jq -r .headSha | awk '{print substr($0, 0, 7)}')
35+
# Get workflow run URL from GitHub API
36+
runUrl=$(echo $json | jq -r .url)
37+
# Get workflow run number from GitHub API (also available as "github.run_number")
38+
runNumber=$(echo $json | jq -r .number)
39+
# **** Templates ****
40+
# Workflow status with link to workflow run
41+
workflowStatus="<${runUrl}|${displayTitle}> ${runStatus}"
42+
# Workflow info with link to all workflow runs for this branch
43+
workflowInfo="<${workflowUrl}|${workflowName}> #${runNumber}"
44+
# Determine run info, with either commit info or manual run info
45+
if [ -z "${{ github.event.head_commit.url }}" ] ; then
46+
runInfo="Manually run by <${authorUrl}|${author}>"
47+
else
48+
runInfo="Commit <${commitUrl}|${shaId}> pushed by <${authorUrl}|${author}>"
49+
fi
50+
# Set results as env vars
51+
echo "DISPLAY_TITLE=$displayTitle" >> $GITHUB_ENV
52+
echo "WORKFLOW_URL=$workflowUrl" >> $GITHUB_ENV
53+
echo "WORKFLOW_NAME=$workflowName" >> $GITHUB_ENV
54+
echo "WORKFLOW_STATUS=$workflowStatus" >> $GITHUB_ENV
55+
echo "WORKFLOW_INFO=$workflowInfo" >> $GITHUB_ENV
56+
echo "RUN_URL=$runUrl" >> $GITHUB_ENV
57+
echo "RUN_NUMBER=$runNumber" >> $GITHUB_ENV
58+
echo "RUN_INFO=$runInfo" >> $GITHUB_ENV
59+
echo "JOB_INFO=$jobInfo" >> $GITHUB_ENV
60+
echo "AUTHOR=$author" >> $GITHUB_ENV
61+
echo "AUTHOR_URL=$authorUrl" >> $GITHUB_ENV
62+
echo "SHA_ID=$shaId" >> $GITHUB_ENV
63+
echo "COMMIT_URL=$commitUrl" >> $GITHUB_ENV
64+
# Output status
65+
echo "result=$runStatus" >> $GITHUB_OUTPUT
3066
- shell: bash
31-
if: ${{ inputs.status == 'cancelled' }}
3267
run: |
33-
curl -X POST '${{ inputs.webhook-url }}' -H 'Content-Type: application/json' -d '{ text: "<${{ env.RUN_URL }}|${{ inputs.run-name }}> was cancelled"}' || true
68+
curl -X POST '${{ inputs.webhook-url }}' -H 'Content-Type: application/json' -d '{ "text": "${{ env.WORKFLOW_STATUS }}\n\n${{ env.WORKFLOW_INFO }}: ${{ env.RUN_INFO }}\n${{ env.JOB_INFO }}" }' || true

.github/workflows/build-and-deploy.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ jobs:
3232
send-notification:
3333
name: Send Notification
3434
needs: [ deploy-artifacts ]
35+
if: ${{ failure() || cancelled() }}
3536
runs-on: ubuntu-latest
3637
steps:
3738
- name: Send Notification
3839
uses: spring-io/spring-security-release-tools/.github/actions/send-notification@v1
39-
if: ${{ failure() || cancelled() }}
4040
with:
41-
webhook-url: ${{ secrets.GOOGLE_CHAT_WEBHOOK_URL }}
42-
status: ${{ job.status }}
43-
build-scan-url: ${{ steps.build-and-publish.outputs.build-scan-url }}
44-
run-name: ${{ format('{0} | Linux | Java 17', github.ref_name) }}
41+
webhook-url: ${{ secrets.SPRING_SECURITY_CI_GCHAT_WEBHOOK_URL }}

0 commit comments

Comments
 (0)