Overview:
We use Slack in our organization. You might also be using Slack for communicating information among the team members in your org. Slack is a great tool and it keeps everyone up to date and reduces unnecessary emails. Particularly automated status, build notifications etc which might flood your inbox. I have already shared an article on posting automated test results in Slack on a specific channel here.
Lets see how we could trigger an automated test suite execution in your Jenkins/CI tool.
Jenkins/CI:
The very first assumption is you have been using some CI tool like Jenkins. I also assume that the tool exposes some kind of API to trigger/schedule a build/job. Here I use Jenkins. You can get the high level idea and implement something similar if you use other tools.
The second assumption is you have a job in Jenkins configured properly which runs just fine when you trigger it manually.
Creating New User:
Lets start with creating a new user in Jenkins for slack to use. Make a note of the API token.
The assumption is this slack user has enough access to trigger the job.
Execute the below CURL command to verify if the user is able to invoke the job.
This is the format of the url. Replace the content within [ ] with appropriate information
curl -X POST http://[user:token]@[jenkins-host-with-port]/job/[job-name]/build
In my case, I tried as shown here.
curl -X POST http://slack:c1c54d626f6a11fbc98ed795ec8862bc@10.11.12.13:8080/job/TEST_ATOMATION_GURU_SLACK_JOB_DEMO/build
Slack – Slash Command:
To invoke the above Jenkins job, we would be using Slack’s slash commands. So whenever you type something after a slash – for ex: ‘/weather‘ – Slack will treat it as a command and can respond to the command. So, for that example, Slack can be configured to provide a weather report for your city.
- Go to your Slack configuration to add Slash configuration.
- I am going to create a smoketest command – so my team can trigger a smoke test from slack to check the health of the application.
- Add the integration setting as shown here. Provide the URL which we need to call to trigger the jenkins job.
- Select the auto complete help
- Once the configuration is done, You can verify immediately in your Slack. Just type ‘/s’ – you could see the ‘smoketest’ command
- Invoke the command – Check if Jenkins is able to trigger the job.
- If the Job requires a parameter, Use the below URL format to invoke the job with Default parameters.
curl -X POST http://[username:token]@[jenkins-host-with-port]/job/[job-name]/buildWithParameters
- If you need to pass any specific parameter, Simply append the variable and value in the parameter.
curl -X POST http://[username:token]@[jenkins-host-with-port]/job/[job-name]/buildWithParameters?ENVIRONMENT=PROD
- To get the status/result of the job, job should be configured to post the results back to Slack. You could check an article on that here.
Note:
- To make the above approach work, Your Jenkins server should be accessible by Slack. Ensure that it is not blocked by the firewall.
Summary:
Slack commands are very useful and come in handy to get information or to trigger something which you might do frequently. By doing this, any non-technical user in the team could make you use of your automated test scripts by directly calling the slash command.
Happy Testing & Subscribe 🙂