Skip to content

Instantly share code, notes, and snippets.

@ans-4175
Last active February 2, 2023 00:01
Show Gist options
  • Save ans-4175/c3d63f22bf3d3bfb5b26551f2f1ea8a7 to your computer and use it in GitHub Desktop.
Save ans-4175/c3d63f22bf3d3bfb5b26551f2f1ea8a7 to your computer and use it in GitHub Desktop.
Share Last Commit to Slack
#!/bin/bash
SLACK_TOKEN=xoxb-your-slack-token
SLACK_DEPLOY_CHANNEL=C0-your-slack-id
REPO_NAME=$(pwd | grep -o "[^/]*$")
LAST_HASH_COMMIT=$(git log --format='%H' HEAD -n1 | cat -)
LAST_AUTHOR_COMMIT=$(git log --format='%an <%ae>' HEAD -n1 | cat -)
LAST_MSG_COMMIT=$(git log -1 --oneline --format=%s | cat -)
SLACK_BLOCK="{'channel':'${SLACK_DEPLOY_CHANNEL}','blocks':[{'type':'section','text':{'type':'mrkdwn','text':'> ${REPO_NAME}'}},{'type':'section','text':{'type':'mrkdwn','text':'Commit: <https://github.com/orgs-name/${REPO_NAME}/commit/${LAST_HASH_COMMIT}|${LAST_HASH_COMMIT}>\n${LAST_MSG_COMMIT}'}},{'type':'context','elements':[{'type':'mrkdwn','text':'${LAST_AUTHOR_COMMIT}'}]}]}"
curl -H "Content-type: application/json" \
--data "${SLACK_BLOCK}" \
-H "Authorization: Bearer ${SLACK_TOKEN}" \
-X POST https://slack.com/api/chat.postMessage
echo "#sent message to slack"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment