This repository is contains a Python Flask application that powers a slash command for Mattermost that allows you to view and run builds on a Jenkins server.
Import Note: This application is not really designed for production use. It is meant only to demonstrate how to connect Mattermost and Jenkins together easily using commonly available tools like Python and Flask (http://flask.pocoo.org/).
In order to use this slash command on your Mattermost server you will need:
- A Mattermost server on which you have permissions to add slash commands;
- A server on which you can install and run Python code that the Mattermost server can talk to;
If you have the two things above you can proceed to the installation steps below:
The following steps
- Log into the machine that will host the Python Flask application;
- Clone this repository to your machine:
git clone https://github.com/cvitter/mattermost-jenkins-slash.git
; - Copy the
config.sample
file toconfig.json
; - Edit
config.json
to set the URL for your Jenkins server and the user name and password of the account you will use to access it; - Run the Flask application - there are a number of ways to run the application but I use the following command that runs the application headlessly and captures output into a log file for troubleshooting:
sudo python jenkins.py >> jenkins.log 2>&1 &
Note: When the application is running you can test it locally by posting data to it using curl like the following example:
curl -X POST -F "text=build mattermost-test-1" http://127.0.0.1:5002/jenkins
Now that the Flask application is running you have to configure your slash command in Mattermost to connect to the application. Inside of Mattermost:
- Click on the Main Menu (located in the left hand side bar next to your team and user name);
- Select Integrations from the drop down menu;
- Click on Slash Commands;
- Click on Add Slash Command;
- Fill in the form (refer to Mattermost help for full explanations of each field):
- Title - your choice (i.e. Jenkins Slash Command)
- Description - your choice again;
- Command Trigger Work - jenkins (unless you want to use something different like 'butler')
- Request URL - the URL of your flask application (i.e. http://something.com:5002/jenkins)
- Response Username - optional
- Response Icon - optional
- Autocomplete - check this so your users know that the slash command exists
- Autocomplete Hint - I have '[list or build] [folder or job]' in mine (you can use whatever works for you)
- Autocomplete Description - I have 'The Butler is at your command!' in mine
- Click Save to create the slash command.
- Test the slash command from within Mattermost and profit!
Once the slash command is set up it is quite easy to use. If you have any questions you can simply
type /jenkins
will return command help as shown in the screenshot below:
If you want to view the list of items (jobs and folders) on your Jenkins server you can type:
/jenkins list
If you leave off the folder/job parameter like in the above example Jenkins will return the list of objects in the server's root like shown below:
If you want to see what is in the Level1 folder you would type:
/jenkins list Level1
To schedule a build (to run immediately) you can type:
/jenkins build mattermost-test-1
If the job succeeds you will see the following response:
If the slash command fails it should return an HTTP error code as shown in the screen shot below if the resource can't be found on the Jenkins server.
Help! If you like this project and want to make it even more awesome please contribute your ideas, code, etc.
If you have any questions, feedback, suggestions, etc. please submit them via issues here: https://github.com/cvitter/mattermost-jenkins-slash/issues
If you find errors please feel to submit pull requests. Any help in improving this resource is appreciated!
The content in this repository is Open Source material released under the MIT License. Please see the LICENSE file for full license details.
The code in this repository is not sponsored or supported by Mattermost, Inc.
- Author: Craig Vitter
Please submit Issues and/or Pull Requests.