Skip to content

Commit

Permalink
feat: Added the slack notifications for Github actions (twilio#304)
Browse files Browse the repository at this point in the history
* feat: Added the slack notifications for Github actions

* Added single success notifiction.

* Update slack-notification.yml

* Update oclif-release.yml

* Converted the community action into script

* Update slack-notification.yml

* Update slack-notification.yml
  • Loading branch information
ravali-rimmalapudi committed Oct 12, 2021
1 parent b2b12e8 commit 8a14fdb
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 20 deletions.
152 changes: 152 additions & 0 deletions .github/scripts/trigger-and-wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/bin/sh

#Functionality from convictional/trigger-workflow-and-wait.
#Link: https://github.com/convictional/trigger-workflow-and-wait

usage_docs() {
echo ""
echo " owner: twilio"
echo " repo: twilio-cli"
echo " github_token: \${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }}"
echo " workflow_file_name: main.yaml"
}

validate_args() {
wait_interval=10 # Waits for 10 seconds
if [ "${INPUT_WAITING_INTERVAL}" ]
then
wait_interval=${INPUT_WAITING_INTERVAL}
fi

propagate_failure=true
if [ -n "${INPUT_PROPAGATE_FAILURE}" ]
then
propagate_failure=${INPUT_PROPAGATE_FAILURE}
fi

trigger_workflow=true
if [ -n "${INPUT_TRIGGER_WORKFLOW}" ]
then
trigger_workflow=${INPUT_TRIGGER_WORKFLOW}
fi

wait_workflow=true
if [ -n "${INPUT_WAIT_WORKFLOW}" ]
then
wait_workflow=${INPUT_WAIT_WORKFLOW}
fi

if [ -z "${INPUT_OWNER}" ]
then
echo "Error: Owner is a required argument."
usage_docs
exit 1
fi

if [ -z "${INPUT_REPO}" ]
then
echo "Error: Repo is a required argument."
usage_docs
exit 1
fi

if [ -z "${INPUT_GITHUB_TOKEN}" ]
then
echo "Error: Github token is required. You can head over settings and"
echo "under developer, you can create a personal access tokens. The"
echo "token requires repo access."
usage_docs
exit 1
fi

if [ -z "${INPUT_WORKFLOW_FILE_NAME}" ]
then
echo "Error: Workflow File Name is required"
usage_docs
exit 1
fi

inputs=$(echo '{}' | jq)
if [ "${INPUT_INPUTS}" ]
then
inputs=$(echo "${INPUT_INPUTS}" | jq)
fi

ref="main"
if [ "$INPUT_REF" ]
then
ref="${INPUT_REF}"
fi
}

trigger_workflow() {
echo "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches"

curl -X POST "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/dispatches" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" \
--data "{\"ref\":\"${ref}\",\"inputs\":${inputs}}"
}

wait_for_workflow_to_finish() {
# Find the id of the last build
last_workflow=$(curl -X GET "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/runs" \
-H 'Accept: application/vnd.github.antiope-preview+json' \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" | jq '[.workflow_runs[]] | first')
last_workflow_id=$(echo "${last_workflow}" | jq '.id')
last_workflow_url="https://github.com/${INPUT_OWNER}/${INPUT_REPO}/actions/runs/${last_workflow_id}"
echo "The workflow id is [${last_workflow_id}]."
echo "The workflow logs can be found at ${last_workflow_url}"
echo "::set-output name=workflow_id::${last_workflow_id}"
echo "::set-output name=workflow_url::${last_workflow_url}"
echo ""
conclusion=$(echo "${last_workflow}" | jq '.conclusion')
status=$(echo "${last_workflow}" | jq '.status')

while [[ "${conclusion}" == "null" && "${status}" != "\"completed\"" ]]
do
echo "Sleeping for \"${wait_interval}\" seconds"
sleep "${wait_interval}"
workflow=$(curl -X GET "https://api.github.com/repos/${INPUT_OWNER}/${INPUT_REPO}/actions/workflows/${INPUT_WORKFLOW_FILE_NAME}/runs" \
-H 'Accept: application/vnd.github.antiope-preview+json' \
-H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" | jq '.workflow_runs[] | select(.id == '${last_workflow_id}')')
conclusion=$(echo "${workflow}" | jq '.conclusion')
status=$(echo "${workflow}" | jq '.status')
echo "Checking conclusion [${conclusion}]"
echo "Checking status [${status}]"
done

if [[ "${conclusion}" == "\"success\"" && "${status}" == "\"completed\"" ]]
then
echo "Yes, success"
else
# Alternative "failure"
echo "Conclusion is not success, its [${conclusion}]."
if [ "${propagate_failure}" = true ]
then
echo "Propagating failure to upstream job"
exit 1
fi
fi
}

main() {
validate_args

if [ "${trigger_workflow}" = true ]
then
trigger_workflow
else
echo "Skipping triggering the workflow."
fi

if [ "${wait_workflow}" = true ]
then
wait_for_workflow_to_finish
else
echo "Skipping waiting for workflow."
fi
}

main
17 changes: 17 additions & 0 deletions .github/workflows/docker-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,20 @@ jobs:
- name: Image digest
run: |
echo ${{ steps.docker_build.outputs.digest }}
notify-complete-fail:
if: ${{ failure() || cancelled() }}
needs: [ build ]
name: Notify Release Failed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.ALERT_SLACK_WEB_HOOK }}
SLACK_COLOR: "#ff3333"
SLACK_USERNAME: CLI Release Bot
SLACK_ICON_EMOJI: ":ship:"
SLACK_TITLE: "Twilio Cli"
SLACK_MESSAGE: 'Twilio CLI Docker Release Failed'
MSG_MINIMAL: actions url
36 changes: 29 additions & 7 deletions .github/workflows/oclif-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,33 @@ jobs:
runs-on: ubuntu-latest
needs: [oclif-release]
steps:
- name: Checkout cli repo
uses: actions/checkout@v2
- name: Invoke HomeBrew workflow
uses: benc-uk/workflow-dispatch@v1
with:
workflow: Homebrew Release
token: ${{ secrets.REPO_ACCESS_TOKEN }}
repo: twilio/homebrew-brew
ref: ${{ github.event.inputs.home-brew-branch }}
inputs: '{ "formula": "${{github.event.inputs.formula}}", "version": "${{github.event.inputs.tag-name}}", "sha": "${{needs.oclif-release.outputs.sha256}}", "pre-release": "${{github.event.inputs.pre-release}}" }'
run: source .github/scripts/trigger-and-wait.sh
env:
INPUT_OWNER: twilio
INPUT_REPO: homebrew-brew
INPUT_GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
INPUT_WORKFLOW_FILE_NAME: release.yml
INPUT_REF: ${{ github.event.inputs.home-brew-branch }}
INPUT_WAITING_INTERVAL: 10
INPUT_INPUTS: '{ "formula": "${{github.event.inputs.formula}}", "version": "${{github.event.inputs.tag-name}}", "sha": "${{needs.oclif-release.outputs.sha256}}", "pre-release": "${{github.event.inputs.pre-release}}" }'
INPUT_PROPAGATE_FAILURE: true
notify-complete-fail:
if: ${{ failure() || cancelled() }}
needs: [ home-brew-release ]
name: Notify Release Failed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.ALERT_SLACK_WEB_HOOK }}
SLACK_COLOR: "#ff3333"
SLACK_USERNAME: CLI Release Bot
SLACK_ICON_EMOJI: ":ship:"
SLACK_TITLE: "Twilio Cli"
SLACK_MESSAGE: 'Oclif Release Workflow Failed'
MSG_MINIMAL: actions url
17 changes: 17 additions & 0 deletions .github/workflows/platform-executables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ jobs:
ASSET_NAME: ${{ matrix.asset_name }}
TAG_NAME: ${{ needs.get-tag.outputs.TAG_NAME }}
REPO_NAME: twilio/twilio-cli
notify-complete-fail:
if: ${{ failure() || cancelled() }}
needs: [ platform-executables-release ]
name: Notify Release Failed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.ALERT_SLACK_WEB_HOOK }}
SLACK_COLOR: "#ff3333"
SLACK_USERNAME: CLI Release Bot
SLACK_ICON_EMOJI: ":ship:"
SLACK_TITLE: "Twilio Cli"
SLACK_MESSAGE: 'Platform Executable Release Failed'
MSG_MINIMAL: actions url
16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,19 @@ jobs:
REPO_NAME: twilio/twilio-cli
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
BRANCH_NAME: ${{steps.extract_branch.outputs.branch}}
notify-complete-fail:
if: ${{ failure() || cancelled() }}
needs: [ release, oclif-release, platform-executables-release ]
name: Notify Release Failed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.ALERT_SLACK_WEB_HOOK }}
SLACK_COLOR: "#ff3333"
SLACK_USERNAME: CLI Release Bot
SLACK_ICON_EMOJI: ":ship:"
SLACK_TITLE: "Twilio Cli"
SLACK_MESSAGE: 'CLI Release workflow Failed'
40 changes: 27 additions & 13 deletions .github/workflows/rpmbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ name: RPM Build
on:
workflow_dispatch:
workflow_run:
# wait for Cli Release to complete, when it completes, start this workflow
# wait for Cli Release to complete, when it completes, start this workflow
workflows: ["Cli Release"]
branches: [main]
types:
types:
- completed
jobs:

rpmbuild:
runs-on: ubuntu-latest
steps:
- name: checkout cli
uses: actions/checkout@v2

- name: get version
id: get-tag
run: |
echo "::set-output name=TAG_NAME::$(node -p -e "require('./package.json').version")"
- name: generate linux tarball
run: |
run: |
npm cache clear & npm install
npm install -g @oclif/dev-cli
oclif-dev pack --targets=linux-x64
- name: build rpm
uses: ./.github/actions/build-rpm
id: build_rpm
Expand All @@ -40,16 +40,30 @@ jobs:
gpg_signing_key_id: ${{ secrets.GPG_SIGNING_KEY_ID }}
gpg_signing_key_passphrase: ${{ secrets.GPG_SIGNING_KEY_PASSPHRASE }}
gpg_pub_key: .github/rpm/gpg_pub.asc

- name: build_rpm outputs
run: |
echo rpm_package_name: ${{ steps.build_rpm.outputs.rpm_package_name }}
echo rpm_package_path: ${{ steps.build_rpm.outputs.rpm_package_path }}
- name: Upload rpm package to release
- name: Upload rpm package to release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh release upload ${{ steps.get-tag.outputs.TAG_NAME }} ${{ steps.build_rpm.outputs.rpm_package_path }} --clobber



notify-complete-fail:
if: ${{ failure() || cancelled() }}
needs: [ rpmbuild ]
name: Notify Release Failed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.ALERT_SLACK_WEB_HOOK }}
SLACK_COLOR: "#ff3333"
SLACK_USERNAME: CLI Release Bot
SLACK_ICON_EMOJI: ":ship:"
SLACK_TITLE: "Twilio Cli"
SLACK_MESSAGE: 'RPM Build Failed'
MSG_MINIMAL: actions url
60 changes: 60 additions & 0 deletions .github/workflows/slack-notification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Slack Notification

on:
workflow_dispatch:
workflow_run:
workflows:
- Cli Release
branches: [main]
types:
- completed
jobs:
wait-for-releases:
env:
INPUT_OWNER: twilio
INPUT_REPO: twilio-cli
INPUT_GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
INPUT_REF: ${{steps.extract_branch.outputs.branch}}
INPUT_WAITING_INTERVAL: 10
INPUT_PROPAGATE_FAILURE: true
INPUT_TRIGGER_WORKFLOW: false
name: Wait for Docker, Homebrew, RPM and Platform executables Release
runs-on: ubuntu-latest
steps:
- name: Extract branch name
id: extract_branch
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
- name: Checkout cli repo
uses: actions/checkout@v2
- name: Wait for Docker Release
run: source .github/scripts/trigger-and-wait.sh
env:
INPUT_WORKFLOW_FILE_NAME: docker-release.yml
- name: Wait for Oclif Release
run: source .github/scripts/trigger-and-wait.sh
env:
INPUT_WORKFLOW_FILE_NAME: oclif-release.yml
- name: Wait for Platform Executables Release
run: source .github/scripts/trigger-and-wait.sh
env:
INPUT_WORKFLOW_FILE_NAME: platform-executables.yml
- name: Wait for RPM Build
run: source .github/scripts/trigger-and-wait.sh
env:
INPUT_WORKFLOW_FILE_NAME: rpmbuild.yml
notify-complete-success:
needs: [ wait-for-releases ]
name: Notify Release Completed
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEB_HOOK }}
SLACK_COLOR: "#36a64f"
SLACK_USERNAME: CLI Release Bot
SLACK_ICON_EMOJI: ":ship:"
SLACK_TITLE: 'Twilio CLI'
SLACK_MESSAGE: 'Release Completed.'
MSG_MINIMAL: actions url

0 comments on commit 8a14fdb

Please sign in to comment.