Integrate and trigger a Jenkins job from Slack
Nishanth ??? ????♂?? ? ???????
Sr. Software Engineer III @ FIS Global Business Pvt Ltd | Expertise in AWS, Kubernetes, and Generative AI Engineering Specialist - #CloudMasters,#pythondeveloper,#LLM,#CostOptimization,#AIOps,#DL,and #NeuralNetworks
Presented by Nishanth Paypal
Go to Jenkins > Manage Jenkins > Plugin Manager > Search for the “Slack Notification Plugin” and install it.
Create a Slack account (if you do not have one) and then create a private/public channel. In my Scenario, I have created a channel named “sample”.
Then Go to settings > Click on ‘Add an app’.
You will get a Team Domain name and Integration Token Credential ID after adding Jenkins CI Integration.
After a successful installation, go to Jenkins > Click on ‘Manage Jenkins’ > Click on ‘Configure System’.
Find the ‘Global Slack Notifier Settings’ section and add the following values:
Then save these settings
Note: Exposing Integration Token is not secure. Please remember to replace the Integration Token with appropriate credentials (secret text).
Section 2:- Trigger Jenkins Job From Slack
Go to Jenkins > Manage Jenkins > Plugin Manager and search for ‘Build Authorization Token Root’ and install that plugin
After installing the plugin, go to Dashboard > Click on Job name > Click on Job configure > On Build Triggers Click
On ‘Trigger builds remotely’ (e.g., from scripts) add Authentication token (you can generate a random token from ) for eg. “57464576646654”.
Use the following URL to trigger the build:
JENKINS_URL/job/SampleJob/build?token=TOKEN_NAME
OR
JENKINS_URL/job/SampleJob/buildWithParameters?token=TOKEN_NAME
Optionally append ‘&cause=Cause+Text’ to provide text that will be included in the recorded build cause.
Now, login to Slack, add a channel and ‘Add an app’
Provide a request URL that is needed when the slash command runs and performs a Post and Get method to the URL.
Shown below is a sample slash command configuration,
Save your Integration. Now run the /build command from your Slack channel. This will start your Jenkins job from Slack.
Section 3 — Trigger a build for a specific branch or a parameterized Jenkins Job From Slack
领英推荐
In the Above section, I showed how we can trigger a Jenkins Job from within Slack. In this section, I will show how we can trigger a build for a specific branch or pass a parameter through Slack for a Jenkins Job.
For this, We need three plugins:
In the above section, we installed and used the Build Authorization Token Root Plugin which will be useful now.
Go to Slack > Slash command configuration
I have configured my Slack slash command to call:
When I type /build parameter1 parameter2 , the I type after the slash command gets sent with the URL as a parameter (branch_name, dockertag in this case).
- this is the command, the part that tells Slack to treat it as a Slash Command and has specific routing instructions.
feature/2348757 cicd-01 - this is the text portion, it includes everything after the first space following the command. It is treated as a single parameter that is passed to the app that owns the command.
The text contains a whole string (i.e text = branch_name dockertag). Hence we need to split this ‘text’ parameter into multiple environment variables that Jenkins can use for the build process.
To achieve this we need to add two pre-SCM build steps and install the Pre-SCM Build Step plugin.
To do this, go to Jenkins > Manage Jenkins > Plugin Manager and search for the “Pre SCM BuildStep” plugin and install it.
After installation,
Go to the Jenkins job and click on run BuildStep before SCM runs under ‘On Build Environment’. Now click on Add Build Step > Execute shell command which consists of the following script:
build_parameters=($text) echo BRANCH=${build_parameters[0]} > env.properties echo param2=${build_parameters[1]} >> env.properties
‘env.properties’ is a file used to store the parsed strings. This script stores the environment variables in a key-value pair. This script stores a parameter in a string array where the parameters are split by the ‘space’ character. Parameters are read and stored in a variable.
Pre-SCM build step 2 is ‘Inject Environment Variables’. For this, we need to install the “Inject Environment Variables” plugin.
Go to Jenkins > Manage Jenkins > Plugin Manager and search for “Environment Injector” plugin and install it.
In the Run build step before SCM runs section, click on Add Build Step then on Inject Environment Variables and in the Properties File Path > Specify path as $WORKSPACE/env.properties.
I have created the properties file in a Workspace to read the file with my parsed environment variables and inject them in the job environment.
Set the Flag as parameterized > Create a String parameter.
Now, Specify the git repository (note that we’re specifying the branch as $BRANCH, hence it will read the properties file and return a value of Branch (in this case, develop, master or feature). Also specify the docker tag as $PARAM2, to return the value of PARAM2.
The command /build feature/2348757 cicd-01 in Slack starts the Jenkins job and triggers the build for specific branch and sets docker image tag.