Jenkins with Slack and POSTMAN integration



This article will help people who want to test their API's end to end using a automated process. We will set up the entire flow using the mostly used tools and creating a way to automate the entire API testing process using CI/ CD tool. 

Let us go through the below steps and see how it works


Setting up POSTMAN 

1. Install POSTMAN tool: https://www.postman.com/downloads/
2. Create a new Collection with simple GET calls
Eg: https://reqres.in/api/login?email=eve.holt@reqres.in&password=cityslicka
3. Go to 'Test' tab and add a  snippet from right side for a 200ok test
4. Hit the endpoint and save the request
5. Click three dots on left side of collection and click 'Share collection'
6. Click Get public link and create a URL

Install Node
1. Download Node JS: https://nodejs.org/en/download/ 
2. Check if node successfully installed > Open CMD and run:   'node -v'
3. Install Newman (npm install -g newman)
4.  Now Copy the POSTMAN collection link from step 4 above
5. Run the collection: newman run <collection name.json>
6. To create HTML -Install HTML generator (npm install -g newman-reporter-htmlextra)
7. Create HTML : newman run Search.postman_collection.json -r htmlextra

Install JENKINS

1. Go to https://www.jenkins.io/download/
2. Download the .war file
3. Open https://localhost:8080   (default port)
NOTE: IF you are NOT able to install on 8080 post then use below command to install on another port: java -jar jenkins.war --httpPort=XXXX
4. Click 'New Item' from the list
5. Select 'Free Style project'
6. Enter description and scroll down to 'Build Section
7. Select 'Execute Shell' from the drop down
8. Go to cmd line and copy the Newman .json collection file and paste in 'Command' and SAVE it
9. SET UP SYSTEM PATH 
export PATH=sbin:/usr/sbin:/usr/bin:/usr/local/bin < For MAC>
PATH=/usr/local/bin:$PATH newman -v < For WINDOWS>
newman run https://www.getpostman.com/collections/98493394839b095uio-r html
10. Go to slack notifications section and enter below( FOLLOW STEPS IN SLACK SECTIONS)
slack workspace name (as you created in your slack)
Click on 'Add button' on credentials then Add 
Click on 'Kind' drop down and select ' secret text'
Enter 'token' copied from SLACK and paste in 'SECRET'
Enter ID and Description and Save
10. Test connection and SAVE it
11. Build Now > The project should build successfully and results can be chekced in console
12. Go to Configs>  'POLL SCM' 
13. Add time when you want to run your build( or through scheduler) and save it


START/RESTART JENKINS SERVER COMMAND

For Windows: java -jar jenkins.war 
Linux: brew services restart jenkins-lts
Linux: brew services start jenkins-lts

INTEGRATION WITH SLACK:

1. LOGIN TO YOUR SLACK
2. Create new channel
3. Click on info icon on right and more details
4. Click 'Add Apps' 
5. Find Jenkins plugin
6. Enter config settings 
7. Follow Next steps on slack 
NOTE: Copy the TOKEN/ SECRET KEY THAT WILL BE USED IN JENKINS CONFIG SETTINGS


Troubleshooting:
Facing errors when installing newman package on MAC

INSTALLING HOMEBREW

1. Open https://brew.sh
2. Copy the command: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
3. Paste it in terminal and run it to complete
4. run:     brew install node
5. You will receive warning: post install steps did not complete
6. Change user location:   sudo chown -R $USER /usr/local
7. run:    brew update
8. run:   brew doctor
9. run :    brew link node
10: run :   brew cleanup
11. run : brew uninstall node
12. run:  brew install node
13. run:   rm 'usr/local/bin/node'
14. run: brew install node( you will get warning, ignore it)
15. run: brew uninstall node
16.run: brew install node
17. run: brew link --overwrite node
18. run: node --version
19. Now you can install any package
20. brew install newman

HTML Reports not displaying correctly in JENKINS

Go to Manage Jenkins > Then Script Console section > Then run the below Groovy script to fix the display:

System.clearProperty("hudson.model.DirectoryBrowserSupport.CSP");
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "sandbox allow-scripts; default-src 'self'; script-src * 'unsafe-eval'; img-src *; style-src * 'unsafe-inline'; font-src *");

Comments