Post Jenkins console output to Slack

Premise

Build result notification etc. can already be posted to Slack (plugin is set)

Get Jenkins build number

import os
build_number = os.environ.get('BUILD_NUMBER')

BUILD_NUMBER on the Jenkins side Environment variables are prepared, so use this https://wiki.jenkins.io/display/JA/Building+a+software+project

Why get the build number?

スクリーンショット 2020-05-26 15.02.10.png There is a "build history" on the Jenkins management screen, If you manually define the directory that specifies the console output result, such as "13" or "14" Every time I build, the build number is updated, so I get an error saying there is no such directory . So to avoid that, get a new build number each time. To do

Install Slacker library

pip install slacker

Use SlackAPI to decide which channel to post

post_log.py


from slacker import Slacker
import sys
import os


token = 'Your SLACK token'
channel = 'Please enter the channel name'

build_number = os.environ.get('BUILD_NUMBER')
file = f'~/builds/{build_number}/log'


slacker = Slacker(token)
slacker.files.upload(file_=file, channels=channel)

files.upload official documentation https://api.slack.com/methods/files.upload

Add an execution shell on the Jenkins administration screen

スクリーンショット 2020-05-26 16.00.51.png

Operation check

It is also possible to add comments, so please add parameters there. Once you've confirmed that it's actually posted to Slack, it's OK! スクリーンショット 2020-05-26 15.02.10(2).png

Recommended Posts

Post Jenkins console output to Slack
Post from Python to Slack
Post to vim → Python → Slack
Post to Slack via Subscriber
Post to slack with Python 3
Post to Slack in Python
Post to slack in Go language
How to overwrite the output to the console
I want to output to the console coolly
Output Python log to console with GAE
How to get colored output to the console
Image characters and post to slack (python slackbot)
Output python log to both console and file
POST messages from python to Slack via incoming webhook
Output log to console with Flask + Nginx on Docker
Output to syslog with Loguru
Post to Twitter using Python
Decorator to silence standard output
Post a message from IBM Cloud Functions to Slack in Python