Skip to content

Commit

Permalink
[Slack Notifier] - Build stage will send on nightly the status (#1376)
Browse files Browse the repository at this point in the history
* slack message will be now always be sent if there were errors
  • Loading branch information
bziser authored Jul 30, 2023
1 parent 3fb87c8 commit 5be7a8b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ jobs:
- build
- run:
name: Notify Slack
when: on_fail
when: always
command: |
if [ "<<parameters.slack_notify>>" = "true" ]; then
pipenv run python ./build_utils/slack_notifier.py -s "$SLACK_TOKEN" -u "$CIRCLE_BUILD_URL" -b "$CIRCLE_BUILD_NUM" -c "$CIRCLECI_TOKEN"
pipenv run python ./build_utils/slack_notifier.py -s "$SLACK_TOKEN" -u "$CIRCLE_BUILD_URL" -b "$CIRCLE_BUILD_NUM" -c "$CIRCLECI_TOKEN" -sn "Build"
fi
deploy:
Expand Down Expand Up @@ -145,7 +145,7 @@ jobs:
when: always
command: |
if [ "<<parameters.slack_notify>>" = "true" ]; then
pipenv run python ./build_utils/slack_notifier.py -s "$SLACK_TOKEN" -u "$CIRCLE_BUILD_URL" -b "$CIRCLE_BUILD_NUM" -c "$CIRCLECI_TOKEN"
pipenv run python ./build_utils/slack_notifier.py -s "$SLACK_TOKEN" -u "$CIRCLE_BUILD_URL" -b "$CIRCLE_BUILD_NUM" -c "$CIRCLECI_TOKEN" -sn "Deploy"
fi
workflows:
Expand Down
38 changes: 26 additions & 12 deletions build_utils/slack_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from slack_sdk.errors import SlackApiError

SLACK_CHANNEL = '#dmst-build'
BUILD_STAGE = 'Build'
DEPLOY_STAGE = 'Deploy'


def get_circle_failed_steps(ci_token: str, build_number: int) -> tuple[list[str], list[str]]:
Expand Down Expand Up @@ -50,7 +52,7 @@ def get_circle_failed_steps(ci_token: str, build_number: int) -> tuple[list[str]
return failed_steps_list, failed_docs_list


def create_slack_notifier(slack_token: str, build_url: str, ci_token: str, build_number: int):
def create_slack_notifier(slack_token: str, build_url: str, ci_token: str, build_number: int, stage_name: str):
"""
Sends a build report via slack.
Expand Down Expand Up @@ -78,17 +80,23 @@ def create_slack_notifier(slack_token: str, build_url: str, ci_token: str, build
color = 'good'
workflow_status = 'Success'

slack_client = WebClient(token=slack_token)
slack_client.chat_postMessage(
channel=SLACK_CHANNEL,
username="Content-Docs CircleCI",
attachments=[{
'color': color,
'title': f'Content Docs Nightly Build - {workflow_status}',
'title_link': build_url,
'fields': steps_fields
}]
send_message = bool(
((failed_docs or failed_entities) and stage_name == BUILD_STAGE)
or stage_name == DEPLOY_STAGE
)

if send_message:
slack_client = WebClient(token=slack_token)
slack_client.chat_postMessage(
channel=SLACK_CHANNEL,
username="Content-Docs CircleCI",
attachments=[{
'color': color,
'title': f'Content Docs Nightly {stage_name} - {workflow_status}',
'title_link': build_url,
'fields': steps_fields
}]
)
except SlackApiError as e:
assert e.response["error"]

Expand Down Expand Up @@ -116,6 +124,7 @@ def options_handler():
parser.add_argument('-s', '--slack_token', help='The token for slack', required=True)
parser.add_argument('-b', '--build_number', help='The build number', required=True)
parser.add_argument('-c', '--ci_token', help='The token for circleci/gitlab', required=True)
parser.add_argument('-sn', '--stage_name', help='The name of the stage we are running now: Build or Deploy', required=True)
options = parser.parse_args()

return options
Expand All @@ -127,12 +136,17 @@ def main():
build_url = options.build_url
ci_token = options.ci_token
build_number = options.build_number
stage_name = options.stage_name

if not slack_token:
print('Error: Slack token is not configured')
exit(1)

create_slack_notifier(slack_token=slack_token, build_url=build_url, ci_token=ci_token, build_number=build_number)
create_slack_notifier(slack_token=slack_token,
build_url=build_url,
ci_token=ci_token,
build_number=build_number,
stage_name=stage_name)


if __name__ == '__main__':
Expand Down

0 comments on commit 5be7a8b

Please sign in to comment.