Skip to content

Commit

Permalink
Add send-notification for GChat
Browse files Browse the repository at this point in the history
Issue gh-50
  • Loading branch information
sjohnr committed Aug 15, 2024
1 parent 2928114 commit ea5ad07
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
77 changes: 56 additions & 21 deletions .github/actions/send-notification/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,65 @@ inputs:
webhook-url:
description: 'Google Chat Webhook URL'
required: true
status:
description: 'Status of the job'
required: true
build-scan-url:
description: 'URL of the build scan to include in the notification'
run-name:
description: 'Name of the run to include in the notification'
default: ${{ format('{0} {1}', github.ref_name, github.job) }}
runs:
using: composite
steps:
- shell: bash
run: |
echo "BUILD_SCAN=${{ inputs.build-scan-url == '' && ' [build scan unavailable]' || format(' [<{0}|Build Scan>]', inputs.build-scan-url) }}" >> "$GITHUB_ENV"
echo "RUN_URL=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> "$GITHUB_ENV"
- shell: bash
if: ${{ inputs.status == 'success' }}
run: |
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
- shell: bash
if: ${{ inputs.status == 'failure' }}
- id: run-info
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
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
# Gather vars only available from github context
commitUrl="${{ github.event.head_commit.url }}"
author="${{ github.event.sender.login }}"
authorUrl=${{ github.event.sender.html_url }}
# Pull additional details from GitHub API
json=$(gh run view ${{ github.run_id }} -R ${{ github.repository }} --json name,number,displayTitle,url,headSha,jobs)
# Determine overall run status (based on all jobs preceding this one)
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')
# Build job info messages, joined in single line (multiline breaks GITHUB_ENV)
jobInfo=$(echo $json | jq -r '.jobs | .[0:length - 1] | map("<" + .url + "|" + .name + "> was " + .conclusion) | join(", ")')
# Get sanitized display title (usually a commit message) from GitHub API
displayTitle=$(echo $json | jq -r .displayTitle)
# Parse workflow yaml file name from full ref name
workflow=$(echo ${{ github.workflow_ref }} | awk -F'/|@' '{print $5}')
# Build workflow URL with branch name
workflowUrl="${{ github.server_url }}/${{ github.repository }}/actions/workflows/${workflow}?query=branch%3A${{ github.ref_name }}"
# Get workflow name from GitHub API (also available as "github.workflow")
workflowName=$(echo $json | jq -r .name)
# Parse 7-digit commit id from head sha
shaId=$(echo $json | jq -r .headSha | awk '{print substr($0, 0, 7)}')
# Get workflow run URL from GitHub API
runUrl=$(echo $json | jq -r .url)
# Get workflow run number from GitHub API (also available as "github.run_number")
runNumber=$(echo $json | jq -r .number)
# **** Templates ****
# Workflow status with link to workflow run
workflowStatus="<${runUrl}|${displayTitle}> ${runStatus}"
# Workflow info with link to all workflow runs for this branch
workflowInfo="<${workflowUrl}|${workflowName}> #${runNumber}"
# Determine run info, with either commit info or manual run info
if [ -z "${{ github.event.head_commit.url }}" ] ; then
runInfo="Manually run by <${authorUrl}|${author}>"
else
runInfo="Commit <${commitUrl}|${shaId}> pushed by <${authorUrl}|${author}>"
fi
# Set results as env vars
echo "DISPLAY_TITLE=$displayTitle" >> $GITHUB_ENV
echo "WORKFLOW_URL=$workflowUrl" >> $GITHUB_ENV
echo "WORKFLOW_NAME=$workflowName" >> $GITHUB_ENV
echo "WORKFLOW_STATUS=$workflowStatus" >> $GITHUB_ENV
echo "WORKFLOW_INFO=$workflowInfo" >> $GITHUB_ENV
echo "RUN_URL=$runUrl" >> $GITHUB_ENV
echo "RUN_NUMBER=$runNumber" >> $GITHUB_ENV
echo "RUN_INFO=$runInfo" >> $GITHUB_ENV
echo "JOB_INFO=$jobInfo" >> $GITHUB_ENV
echo "AUTHOR=$author" >> $GITHUB_ENV
echo "AUTHOR_URL=$authorUrl" >> $GITHUB_ENV
echo "SHA_ID=$shaId" >> $GITHUB_ENV
echo "COMMIT_URL=$commitUrl" >> $GITHUB_ENV
# Output status
echo "result=$runStatus" >> $GITHUB_OUTPUT
- shell: bash
if: ${{ inputs.status == 'cancelled' }}
run: |
curl -X POST '${{ inputs.webhook-url }}' -H 'Content-Type: application/json' -d '{ text: "<${{ env.RUN_URL }}|${{ inputs.run-name }}> was cancelled"}' || true
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
7 changes: 2 additions & 5 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ jobs:
send-notification:
name: Send Notification
needs: [ deploy-artifacts ]
if: ${{ failure() || cancelled() }}
runs-on: ubuntu-latest
steps:
- name: Send Notification
uses: spring-io/spring-security-release-tools/.github/actions/send-notification@v1
if: ${{ failure() || cancelled() }}
with:
webhook-url: ${{ secrets.GOOGLE_CHAT_WEBHOOK_URL }}
status: ${{ job.status }}
build-scan-url: ${{ steps.build-and-publish.outputs.build-scan-url }}
run-name: ${{ format('{0} | Linux | Java 17', github.ref_name) }}
webhook-url: ${{ secrets.SPRING_SECURITY_CI_GCHAT_WEBHOOK_URL }}

0 comments on commit ea5ad07

Please sign in to comment.