|
| 1 | +name: job-notification |
| 2 | +description: A GitHub Action for sending job notifications |
| 3 | +inputs: |
| 4 | + repository: |
| 5 | + description: | |
| 6 | + Name of the repository (e.g scribd/job-notification) |
| 7 | + Useful for sending notifications on behalf of another repository. |
| 8 | + required: true |
| 9 | + default: ${{ github.repository }} |
| 10 | + token: |
| 11 | + description: Slack token to send the notification via "gitscribd" app. |
| 12 | + required: true |
| 13 | + channel: |
| 14 | + description: The Slack channel name for receiving the notification |
| 15 | + required: true |
| 16 | + message: |
| 17 | + description: Customize the notification message. |
| 18 | + |
| 19 | +runs: |
| 20 | + using: composite |
| 21 | + steps: |
| 22 | + - name: Set fields |
| 23 | + shell: bash |
| 24 | + if: always() |
| 25 | + id: fields |
| 26 | + run: | |
| 27 | + if [ "${{ job.status }}" = "success" ]; then |
| 28 | + echo "emoji=large_green_square" >> $GITHUB_OUTPUT |
| 29 | + echo "color=good" >> $GITHUB_OUTPUT |
| 30 | + elif [ "${{ job.status }}" = "failure" ]; then |
| 31 | + echo "emoji=large_red_square" >> $GITHUB_OUTPUT |
| 32 | + echo "color=danger" >> $GITHUB_OUTPUT |
| 33 | + else |
| 34 | + echo "emoji=large_orange_square" >> $GITHUB_OUTPUT |
| 35 | + echo "color=warning" >> $GITHUB_OUTPUT |
| 36 | + fi |
| 37 | +
|
| 38 | + if [ ! -z "${{ inputs.message }}" ]; then |
| 39 | + message="${{ inputs.message }}" |
| 40 | + elif [ ! -z "${{ github.event.head_commit.message }}" ]; then |
| 41 | + # get commit message from `push` trigger |
| 42 | + commit_message=$(echo "${{ github.event.head_commit.message }}" | head -n 1) |
| 43 | + message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>" |
| 44 | + elif git rev-parse --is-inside-git-dir > /dev/null 2>&1; then |
| 45 | + # get commit message from the current git directory to support `workflow_dispatch` trigger |
| 46 | + commit_message=$(git log --format=%B -n 1 | head -n 1) |
| 47 | + message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>" |
| 48 | + else |
| 49 | + commit_message=Link to the latest commit in the repository |
| 50 | + message="<https://github.com/${{ inputs.repository }}/commit/${{ github.sha }}|$commit_message>" |
| 51 | + fi |
| 52 | + echo "message=$message" >> $GITHUB_OUTPUT |
| 53 | +
|
| 54 | + author=${{ github.event.pusher.name }} # context from `push` trigger |
| 55 | + author=${author:-${{ github.event.sender.login }}} # context from `workflow_dispatch` trigger |
| 56 | + echo "author=$author" >> $GITHUB_OUTPUT |
| 57 | +
|
| 58 | + - name: Send notification |
| 59 | + if: always() |
| 60 | + |
| 61 | + env: |
| 62 | + SLACK_BOT_TOKEN: ${{ inputs.token }} |
| 63 | + with: |
| 64 | + channel-id: ${{ inputs.channel }} |
| 65 | + payload: | |
| 66 | + { |
| 67 | + "text": ":${{ steps.fields.outputs.emoji }}: *Workflow <https://github.com/${{ inputs.repository }}/actions/runs/${{ github.run_id }}/|${{ github.workflow }}> ${{ job.status }}*", |
| 68 | + "attachments": [ |
| 69 | + { |
| 70 | + "color": "${{ steps.fields.outputs.color }}", |
| 71 | + "fields": [ |
| 72 | + { |
| 73 | + "title": "Repository", |
| 74 | + "value": "<https://github.com/${{ inputs.repository }}|${{ inputs.repository }}>", |
| 75 | + "short": true |
| 76 | + }, |
| 77 | + { |
| 78 | + "title": "Environment", |
| 79 | + "value": "${{ github.ref_name == 'main' && 'production' || github.ref_name }}", |
| 80 | + "short": true |
| 81 | + }, |
| 82 | + { |
| 83 | + "title": "Author", |
| 84 | + "value": "<https://github.com/${{ steps.fields.outputs.author }}|${{ steps.fields.outputs.author }}>", |
| 85 | + "short": true |
| 86 | + }, |
| 87 | + { |
| 88 | + "title": "Job", |
| 89 | + "value": "${{ github.job }}", |
| 90 | + "short": true |
| 91 | + }, |
| 92 | + { |
| 93 | + "title": "Commit Message", |
| 94 | + "value": "${{ steps.fields.outputs.message }}", |
| 95 | + "short": false |
| 96 | + } |
| 97 | + ] |
| 98 | + } |
| 99 | + ] |
| 100 | + } |
0 commit comments