I want to execute aws-cli command through Jenkins Execute Shell script. But I am facing issue with double quotes and single quotes. Here is the command I want to execute
aws cloudformation create-stack --template-url templates.url --parameters ParameterKey=TagName,ParameterValue='My Test Job'
But when I execute my Jenkins job it removes singles quotes and my command becomes
aws cloudformation create-stack --template-url templates.url --parameters ParameterKey=TagName,ParameterValue=My Test Job
How can I escape single quotes.
you should share a more complete part of the pipeline so we can understand how are you running that command.
I will asume the simple case that your command is in a sh step in a stage. In this case you must escape the characters .
stage('Deploy to Server') {
steps{
sh "aws cloudformation create-stack --template-url templates.url --parameters ParameterKey=TagName,ParameterValue=\'My Test Job\'"
}
}
Bear in mind the the log output will not give you the exact command . you should use an echo or a logging function to actually see the command correctly.
Here is a page that helped me a lot in manipulating the strings
Related
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.
A 'bat' script from my Jenkinsfile is failing for no apparent reason. I already tested it by physically running it on the agent machine, so now I want to run it under Jenkins manually - through the Script Console. How do I go about transforming this line into the exactly equivalent console command?
bat 'set \"ANDROID_HOME=%USERPROFILE%\\AppData\\Local\\Android\\Sdk\" && gradlew.bat assembleDebug'
I tried this, no luck, I probably didn't escape something correctly, perhaps too many inner quotes for the cmd /c command?
println "cmd \\c \"set \"ANDROID_HOME=%USERPROFILE%\\AppData\\Local\\Android\\Sdk\" && gradlew.bat assembleDebug\" ".execute().text
cmd \c should be cmd /c, and you can also return the command's output like so:
"cmd /c \"set \"ANDROID_HOME=%USERPROFILE%\\AppData\\Local\\Android\\Sdk\" && gradlew.bat assembleDebug\"".execute().text
Anyway, the script console is only the first step to determining run-time issues in pipeline steps, as you only get to know whether the command itself works.
Next you want to isolate your problem in a separate pipeline, so you can work out CPS and sand-boxing problems. Fun stuff.
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/
"""
}
I need to ssh to a server from a simple jenkin pipeline and make a deploy which is simply moving to a directory and do a git fetch and some other comands (nmp install among others). Thing is that when jenkin job ssh to the remote server it connects ok but then It gets stucked, I have to stop it. I just now modify the script to simply do a "ssh to server " and a "pwd command" to go to the easiest but it connects to it and it get stuck untill I abort. What Am I missing? here is the simpe pipeline script and the output on an screenshot
pipeline {
agent any
stages {
stage('Connect to server') {
steps {
sh "ssh -t -t jenkins#10.x.x.xx"
sh "pwd"
}
}
stage('branch status') {
steps {
sh "git status"
}
}
}
}
Jenkins executes each "sh" step as a separate shell script. Content is written to a temporary file on Jenkins node and only then executed. Each command is executed in separate session and is not aware of previous one. So neither ssh session or changes in environment variable will persist between the two.
More importantly though, you are forcing pseudo-terminal allocation with -t flag. This is pretty much opposite to what you want to achieve, i.e. run shell commands non-interactively. Simply
sh "ssh jenkins#10.x.x.xx pwd"
is enough for your example to work. Placing the commands on separate lines would not work with regular shell script, regardless of Jenkins. However you still need to have private key available on node, otherwise the job will hang, waiting for you to provide password interactively. Normally, you will want to use SSH Agent Plugin to provide private key at runtime.
script {
sshagent(["your-ssh-credentals"]) {
sh "..."
}
}
For execution on longer commands see What is the cleanest way to ssh and run multiple commands in Bash?
I am using jenkins to build a bunch of legacy code. The legacy code comes with some complex build scripts, written in csh.
The build scripts do not check for or exit on errors. The user is expected to scan the output for error messages. However, this does not work well with Jenkins.
I am executing the csh build scripts in a jenkins "shell execution" build step. For example:
export PATH=`pwd`/ALL/bin:/usr/local/bin:/usr/bin:/bin:$PATH
cd ATLb2.00/expt_02.0
csh 020.com
When I run this from the command line, I can also use the -e option:
csh -e 020.com
In this case, as I expect, the script is run, but when the first error is encountered, the script stops and returns a non-zero code. However, when I try this in Jenkins, the build fails as soon as it gets to the csh -e command, without executing any of the script.
The error I get in Jenkins is:
+ csh -e 020.com
Build step 'Execute shell' marked build as failure
On the command line, the script is run and I see all kinds of output, until something fails, and then the script exits. On Jenkins the script seems to fail without even running. There is no output, and even scripts with no failures will not run for me under jenkins with the -e option.
What's up?
I recommend that you specify csh on a more global level and then execute the commands in a Jenkins build step.
If you want to use csh for all jobs, you can set the default shell using Jenkins > Manage Jenkins > shell executable.
If you want to use csh for only a particular job, begin the Execute shell build step with a shebang, such as:
#!/usr/bin/tcsh -e -x
command1
command2
...
Since I have tested only tcsh, that is what I use in the example.
Beware that a space is not allowed after the #!:
#! /usr/bin/tcsh # Wrong
This will give the error,
java.io.IOException: Cannot run program ""
I tested the above on Jenkins 1.625.3