How to run a command over SSH automatically on Windows - windows

I have two Windows boxes. One is Jenkins build server. Second one is deployment box.
I have installed SSH and created a user on the deployment Windows box. I can ssh to it from my Ubuntu workstation and run commands I need to run.
But I need to do this automatically from Jenkins. How to do it? Basically I need to make this automatic:
ssh richard#myserver
... enter password ...
cd C:/puppet && git pull
"/cygdrive/c/Program Files (x86)/Puppet Labs/Puppet/bin/puppet.bat" apply C:/puppet/manifests/site.pp
That's it. Is there some way to do this from a batch file and just execute the *.bat file from Jenkins?

The publish over ssh plugin allows you to run remote commands.
In Manage Jenkins > Configure System > Publish over SSH you need to configure the remote machine you want to deploy to. Click on Add and configure the connection to the server.
In the Job itself add a Build Step Send files or execute commands over SSH. Select from the drop down the SSH server you want to connect to. In the Exec command you can add all commands you want to have executed on the remote machine.

Related

How to access ssh key file in a Teamcity right in a job without SSH upload

I have a job that ssh into other servers and deploys some configuration with scp, but I can not find any way to access ssh key file used in my project configuration in TeamCity in order to execute shell command in my job - "ssh -I ~/.ssh/password", because TeamCity runs only in job directory. Therefore, I want to ask is there any way to access this SSH private key file that I mentioned in a project settings.
Just to say, I cannot use SSH-EXEC and SSH-UPLOAD as I have Shell script that ssh into many servers one by one reading from a file, therefore it would not be useful to have for each job one separate SSH exec job step in TeamCity project, so I have to somehow access the file without using standard SSH-EXEC and SSH-UPLOAD in a TeamCity
What have I tried?
I only had one idea - somehow to access SSH key that is located outside working directory by a path (I found this in documentation):
<TeamCity Data Directory>/config/projects/<project>/pluginData/ssh_keys
Problem with this, is that I cannot just cd into given path, as job does not want to go outside my working directory where job is executed by TeamCity. Therefore I could not access given directory where ssh_keys for my project is located.
UPD: Find out solution to use build feature SSH, that way you can execute SSH-key right with command line in job

Specify SSH credentials/keys in Windows Git under Jenkins user

I am using Jenkins in our builds
So I am already using the GIT Plugin
This plugin lets me specify Jenkins credentials, where we have already specified and installed/setup SSH keys.
However at the end of the build, I'd like to git tag my repo. I am calling the git.exe command line, and I get this error on a push:
Permission denied (publickey).
fatal: Could not read from remote repository.
Ideally, we don't want to use another plug in, (e.g. Git Publisher), as we are trying to do more of this via our own scripts as there is a good possibility that we may not use Jenkins in the future.
Also, ideally, we don't want to install items on our build server if we don't have to.
So the question is - how can I specify ssh keys/credentials on the command line for the given 'session'?
Thank you.
Put this in a shell script:
ssh -i path-to-your-private-key
Set the path to the shell script in GIT_SSH for Jenkins. git pull will then use that instead of plain ssh to access the remote repository.
Alternately you could configure ssh in $HOME/.ssh for the account under which Jenkins runs, but that can get tricky if your Jenkins runs as a Windows service.

Using the execute shell command in Jenkins to run git commands on a Windows machine

I need to run a bash script that periodically deletes old git branches. I am having trouble finding a way to connect to the git repo via the execute shell option.
Currently I am using cygwin in order to run git commands. Here is what I have in execute shell:
#!c:\cygwin64\bin\bash --login
git ls-remote git#10.1.1.126:/external-web/collette-com.git
This command is throwing the following error.
[Delete Branches] $ c:\cygwin64\bin\bash --login
C:\Users\tbraga\AppData\Local\Temp\hudson5750784484659728632.sh
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Build step 'Execute shell' marked build as failure
I have tried running this command in the command line and am prompted for a password. Could this be the issue?
I have the git plugin configured within Jenkins and the connection works perfectly when using Source Code Management Git.
Any suggestions on how to make this connections work in the execute shell field would be greatly appreciated.
I solved this problem by passing my credentials to my execute shell script through the Credentials Binding Plugin in Jenkins
It's simple enough to create an SSH key associated with your user.
Try here : https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html
Put keys under %userprofile%/.ssh and try running it again.
You can also use the same credentials used in your Jenkins configuration
I use SSH keys for auth to Github and had this same issue. My Jenkins configuration has EC2 slaves, so the default SSH key on the machine wasn't correct for Github.
I fixed it with the SSH Agent Plugin. In the job, enable the "SSH Agent" setting and choose the stored SSH key for Github authentication. It should be the same one selected for the Git-SCM configuration used to clone the repo.

How to use expect for remote deployment using script

I am trying to automate deployment on the remote server.After doing some investigation I came across two approaches:
1.Using expect
2.Using sshpass
I want to run a script file with deployment steps,without manually entering any password or any other input parameter required by my script from command line.
Please suggest me the right approach.
I went through Ref1 and downloaded expect5.45.tar. Can anyone please let me know what should I do next. I am not able to install as I don't have admin rights.
Would Jenkins not suite this questions. Jenkins will allow for script control as an environmental file as well as control over the remote host (via the publish over ssh plugin).
This would allow you to automated your process.

Copying files built on Unix back to Windows Jenkins workspace

I have a jenkins Job in which I build my application on Unix box by making SSH connection to it and run the build script on Unix box. My Jenkins is on windows I want to generate PMD reports on windows so, i want to copy pmd.xml file from Unix box to Windows machine.
Right now my windows Jenkins workspace is empty because I do SVN checkout, Build and generation of binaries on Unix box. for this purpose I am using Jenkins SSH plugin.
Is there any way to copy Unix contents back to Windows Jenkins Workspace?
I have tried Jenkins SCP plugin but confused with source and SCP site. Please suggest.
Thanks in Advance.
You can use plink.exe and pscp.exe to interface from Windows to Linux. These should be part of your PuTTY distribution.
However, if your slave is already connecting to the Linux machine, you can use the same connection to bring the file back to slave's workspace. Then use Copy to Slave plugin (nevermind the name, it does both directions) to move the file from slave to master workspace
I Added Linux machine as slave to windows master and then used Copy-to-Slave plugin to copy generated reports from linux to windows workspace its working fine.

Resources