Last active
April 2, 2023 03:24
-
-
Save parjun8840/a9293ccb2c131df2fd3efa45095f3be2 to your computer and use it in GitHub Desktop.
Github configuration for slacknotifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#######Get the Workflow id using GitHub API############### | |
% curl -L -H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer XXXXXXXXXXXXXXXX"\ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
https://api.github.com/repos/parjun8840/gha01/actions/workflows | |
{ | |
"total_count": 10, | |
"workflows": [ | |
{ | |
"id": 53067516, | |
"node_id": "W_kwDOIvf4ls4DKb78", | |
"name": "Slack Send workflow", | |
"path": ".github/workflows/send_slack.yml", | |
"state": "active", | |
"created_at": "2023-04-02T09:59:34.000+09:00", | |
"updated_at": "2023-04-02T09:59:34.000+09:00", | |
"url": "https://api.github.com/repos/parjun8840/gha01/actions/workflows/53067516", | |
"html_url": "https://github.com/parjun8840/gha01/blob/main/.github/workflows/send_slack.yml", | |
"badge_url": "https://github.com/parjun8840/gha01/workflows/Slack%20Send%20workflow/badge.svg" | |
} | |
] | |
} | |
#######Create the Workflow configuration: send_slack.yml############### | |
name: Slack Send workflow | |
on: | |
workflow_dispatch: | |
inputs: | |
text: | |
type: string | |
required: true | |
description: 'Text data' | |
jobs: | |
SlackSendMsg: | |
name: SlackSendMsg Job | |
runs-on: ubuntu-latest | |
steps: | |
- name: checkout the code | |
uses: actions/checkout@v3 | |
- name: build | |
env: | |
SLACK_URL: ${{ secrets.SLACK_URL }} | |
run: | | |
pwd | |
ls -lrtR | |
bash send_slack.sh -u "$SLACK_URL" -t ${{ github.event.inputs.text }} | |
#######Check-in the script: send_slack.sh############### | |
#!/bin/bash | |
set -e | |
usage() { | |
echo "usage: ${0##*/} -u <SLACK_URL> -t <TEXT>" | |
exit 1 | |
} | |
send_slack() { | |
echo "Sending msg to slack" | |
curl -k -X POST -H 'Content-type: application/json' --data "{ \"text\": \"${text}\" }" "${slack_url}" | |
} | |
while getopts ":u:t:" opt; do | |
case $opt in | |
u) slack_url="$OPTARG" | |
;; | |
t) text="$OPTARG -From GitHub Action" | |
;; | |
\?) echo "Invalid option -$OPTARG" >&2 && usage | |
;; | |
esac | |
done | |
if [ -z "$slack_url" ] || [ -z "$text" ] | |
then | |
usage | |
fi | |
send_slack | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment