jenkins shell build step with variable containing a single quote - jenkins

I'm trying to use jenkins to schedule a repeated job to rsync from a remote directory. Unfortunately I only have ssh password access (no key) so I'm using sshpass to authenticate. The password contains a single quote, and no matter what I do, jenkins always puts a backslash in front of the single quote.
Details:
jenkins is running on centos, installed via yum
build step is a shell
command in shell is basically `sshpass -p my'pass rsync -avc me#remotehost.com: /my/dest/dir/
variations I've tried:
put the password in a file then use sshpass -f
reports authentication fails
put the above command in a script file, have the jenkins build run that script
use jenkins credentials / associated variables
all variations of double quoting / string concatenation
Note that all of the things I've tried work fine on the command line and/or via crontab (I'm trying to use jenkins instead of crontab though...)
Any ideas?

Try to use Groovy multiline string literal for the shell block:
stage{
sh """
sshpass -p my'pass rsync -avc me#remotehost.com: /my/dest/dir/
"""
}

Related

Passing env/params values to the Shell Service plugin of Jenkins

Shell Service of Jenkins plugin is like in the picture that is below. I need to pass params/env variable to it but couldn't make it.
Is there any way to pass values into "Execute shell script on remote host using ssh"?
If this plugin works as a usual ssh command, you can escape $DOCKER_REGISTRY quotes
ssh jenkins#192.168.13.156:22 "echo \"$DOCKER_REGISTRY\""
IMHO: Get rid of the ssh plugins in Jenkins, just use shell, is way more flexible.

User Interactive shell script not running in Jenkinsfile

I have a user interactive shell script that runs successfully on my Linux server. But when I try to run it via jenkins, it doesn't run.
I have created a Jenkinsfile.
Jenkinsfile
node('slaves') {
try
{
def app
stage('Remmove Docker service') {
sh 'sshpass ssh docusr#10.26.13.12 "/path/to/shell/script"'
}
}
}
Shell Script
#!/bin/bash
read -p "Hi kindly enter the api name : " api
docker service logs $api --raw
The shell Scipt runs successfully on my local server, when I try to run it on Jenkins using Jenkinsfile, it doesn't accept $api variable in my shell script which is user interactive.
What you are trying to achieve doesn't serve any purpose of automating your job by jenkins, if I correctly understood. So, your job is actually seeking a user input and it's in best interest to have a parameterized jenkins build in this case.
For your case, you can still give an argument to the sshpass command $api and have it read from the jenkins environment itself Or, better make your jenkins build parameterized and use your user input $api as the parameter.

Jenkins pipeline job gets triggered as anonymous but not as an user/Admin

Jenkins Pipeline job doesn't trigger pipeline job using jenkins cli. When i run jenkins as anaonymous this works, but when i create a user/admin it fails.
I have a job A which has parameters and passes the same to Pipeline Job. This is a Master-slave setup. This is how i run:
sudo java -jar /home/user/jenkins-cli.jar -s $JENKINS_URL build pipeline_job -p parameter_Name="$parameter_Name" -p parameter_Name2="$parameter2_Name"
1.) I tried using options, "-auth" , "-username -password" but doesn't work.
errors:
No such command: -auth
No such command: -ssh
2.) Another option is paste the public key in SSH section http://jenkin_url/me/configure , but still it fails
error:
java.io.IOException: Invalid PEM structure, '-----BEGIN...' missing
Is there i am missing anything ?
I Found the solution,
1.) used SSH CLI.
In my case i was using master-slave environment, connection was made using SSH keys vice-versa. In order to trigger the build using Jenkins CLI, place the SSH keys both public & private and place them in http://jenkinsURL/user/username/configure
Here username= the one used to connect the nodes.
Trigger the job as below:
java -jar /home/username/jenkins-cli.jar -s $JENKINS_URL -i /home/username/.ssh/id_rsa build JOBNAME
Note: This is one way but cloudbees doesn't encourage this approach.
2.) There is new approach i.e., using API token authentication.
Go to http://jenkinsURL/user/username/configure
Copy the API token
trigger the build as below:
java -jar /home/username/jenkins-cli.jar -s $JENKINS_URL -auth username:apitoken /home/username/.ssh/id_rsa build JOBNAME
Note: For using API token option, download the latest jar file

Can Jenkins source .bashrc of associated user?

My Jenkins runs inside Tomcat which runs under user buildman, therefore all the jobs run under that user (in CentOS). Some of my jobs depend on environment variables, which I set in .bashrc. However, the Jenkins jobs do not see any of the variables set in that file even though that script is supposed to be sourced for non-login shells, such as what I would think Jenkins should be (?).
The workaround is simple: I just copy and paste all the variables from my .bashrc into the build command script in Jenkins. But that is not very elegant. Is there any way to get Jenkins to source the .bashrc of the user it runs under so that it gets its usual configuration without having to set it separately in each job?
Jenkins creates a temporary sh-script for each script section (at least when using a "classical" project - for the Pipeline approach I'm not sure). This temporary script is executed with sh. On most Linux systems this is a symlink to bash, this SO-post gives some insights.
Also according to the man-pages of bash invoking bash with sh "tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well."
This means the .bashrc is not interpreted at all. However you can try to source the bashrc for each shell-invocation...
So, I tried a few things and the only solutions that seems to work are:
have a shell script in your repo that uses bash
write a file, chmod it via sh and then run it
In both case, there needs to be an executable file with content like:
#!/usr/bin/env bash
...
Using sh """ bash -c "...." """" doesn't seem to work
When my Jenkins agent launches by SSH on redhat linux, I see it does print environment variables defined in .bashrc.
My problem with not seeing changes to .bashrc was because I needed to relaunch the agent, so it picked up the change.
I have found a command that works for me
In .profile, .bashrc, etc.:
export MY_BASH_VAR=123
In Execute Shell:
VAR=$(bash -c "source ~/.profile && echo \$MY_BASH_VAR")
echo $VAR
Will print 123 in the output console when the job builds

Clone Bitbucket repository without starting ssh-agent

I've set up Jenkins on my VPS and created a job which is set to execute a shell script containing the following command: ssh -T git#bitbucket.org
It turns out that i get a "Permission denied (publickey)." response because ssh-agent is not started. This can be solved by adding these two lines to the shell script:
eval `ssh-agent -s`
ssh-add ~/.ssh/bitbucket_key
However, I don't like to add these tho lines to every Jenkins item when I need to clone a repository. I would expect that if I log in via ssh to my VPS and change to the Jenkins user to execute the two lines myself, this would no longer be necessary. Unfortunately, this is not the case. I can successfully run ssh -T git#bitbucket.org myself, but the Jenkins job still fails without the two extra lines.
Is there a way to avoid this behavior, i.e. a way in which I only have to start the ssh-agent and add my key to it once instead of every time I want to clone a repository? I cannot imagine that it would be a good practice to start (a new) ssh-agent every time I want to clone and build my code.
Use ssh_config in your ~/.ssh/config. It has simple syntax as you can read in manual page for ssh_config. For your case should be enough
Host bitbucket.org
IdentityFile ~/.ssh/bitbucket_key

Resources