Execute local shell script to remote server in Jenkins pipeline - jenkins-pipeline

I have a deployment script checked in the Github which I checkout with the source code in the pipeline. I need to execute this shell script to the remote server to perform the tasks (create backup, start / stop Jboss). I am using ssh agent to ssh into the remote server. How can I execute this within the Jenkins pipeline, and get the output from the commands back to the pipeline?
Thank you

Related

Can Jenkins run my shell script remotely?

A Bash script is CRON-ed to run on server A every day and connects to a remote MySQL database to run a query, dump the results to a file, and send an email.
In my freestyle project, I have set up my build to pull that Bash script from my Github repo and execute it. Jenkins starts it but the script fails. Based on my discovery,
Jenkins runs my script in a clone of my repo on its own server, not on server A where it is CRON-ed
The above explains why my shell script fails to find the mysql client executable
Based on my understanding, this means that my freestyle project needs to connect to server A where my script is CRON-ed to run like so
ssh serverA_username#serverA #using ServerA user key
SCRIPT="$WORKSPACE/shell/bash_script_name.sh"
sh -x $SCRIPT
Am I correct? Any pointers welcome. Thank you!

How to upload and run jmeter script from server

There is a need to upload the script from local to server and then run over there. can someone please let me know how to can achieve this.
Just copy it from local machine to the "server" using SCP for Linux or SMB for Windows, once done you can log into the server over SSH or RDP and execute your JMeter test in command-line non-GUI mode
If you want fully unattended/automated execution consider the following:
Setting up a version control system, i.e. use Github to store your script(s)
Configure the Webhook to trigger an action when you commit the file
Install Jenkins on the server
Configure Jenkins to listen to the Github Webhook and when it happens kick off a build running a JMeter test
This way whenever you add new or update existing script it will automatically trigger the job which will execute the test, check out How to Integrate Your GitHub Repository to Your Jenkins Project article for detailed steps if needed.

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 run a command over SSH automatically on 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.

Resources