Build result notification etc. can already be posted to Slack (plugin is set)
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
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 font>. So to avoid that, get a new build number each time. To do
pip install slacker
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
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!

Recommended Posts