I did this as part of my project, but realized it is one of the most convoluted topics which was not discussed properly in any forum in internet. So here it is for anyone to get benefited from a similar requirement.
Prerequisite to understanding this blog:
- You have Jenkins installed in your windows machine.
- You have git bash installed in your machine.
- You know some basic linux commands.
You can find tons of material in the internet to install jenkins and git bash, so go ahead and do it if you don’t have it already.
Step 1: Setup the shell application in jenkins.
Login to Jenkins as administrator , or a user with authority to manage jenkins. Navigate to:
Manage Jenkins > Configure System > Scroll down to Shell
Assuming that you have downloaded GIT BASH, you should find sh.exe under C:\Program Files\Git\bin\sh.exe , now paste that in your shell executable as below:
Go ahead , save it.
Step 2: Create your ssh key and copy it to the remote linux server
- Open you git bash terminal.
- Type this : ssh-keygen -t rsa -b 2048
3. Copy the ssh key into the remote linux server
User@YOUR-REMOTE-SERVER’s password: **********
After the successful copy , you will get this message:
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘User@YOUR-REMOTE-SERVER'” and check to make sure that only the key(s) you wanted were added.
Step 3: Go to Jenkins and create a new Job.
- Create a simple freestyle project in Jenkins
- In the build section, you can write the below:
3. Now click “Build Now” and look at your console output
That’s it, If your company is using Jenkins in windows, but you need to automate stuff in linux, this would be a great way to do it.
I would love to see a comment or like from you if you liked the blog.