I'm trying to launch Datadog agent in Jenkins pipeline, but I'm getting below errors:
line 7: warning: here-document at line 2 delimited by end-of-file (wanted `EOF').
EOF: Command not found Error.
stage('Install Datadog agent'){
when {
environment(name: "DATADOG_REQ", value: "enable")
}
steps {
script {
echo "Installing Datadog Agent"
sh '''#!/bin/bash
ssh -o ConnectTimeout=30 -t ABC#1234 /bin/bash << EOF || error_exit "creating based folder failed for $service_name"
sudo chmod u+x ./${JOB_NAME}/enableDatadogAgent.sh
sudo chown jenkins:jenkins ./${JOB_NAME}/enableDatadogAgent.sh
echo ${DATADOG_REQ} ${JOB_NAME}
./${JOB_NAME}/enableDatadogAgent.sh ${DATADOG_REQ}
EOF'''
}
}
}
Any help would be very helpful.
I changed the << EOF with <<- _EOF_, and now it's working.
script {
echo "Installing Datadog Agent"
sh '''#!/bin/bash
ssh -o ConnectTimeout=30 -t ABC#1234 /bin/bash <<- _EOF_ || error_exit "Failed to execute while doing SSH"
sudo chmod u+x ./${JOB_NAME}/enableDatadogAgent.sh
sudo chown jenkins:jenkins ./${JOB_NAME}/enableDatadogAgent.sh
echo ${DATADOG_REQ} ${JOB_NAME}
./${JOB_NAME}/enableDatadogAgent.sh ${DATADOG_REQ}
_EOF_'''
}
Related
While using parallel pipeline getting exit code 3 in shell script.
stage('Core:Restart')
{ agent { label "master" }
steps {
wrap([$class: 'MaskPasswordsBuildWrapper', varPasswordPairs: [[password: "${password}"]]])
{ sh label: '',
script: '''#!/bin/bash
if [[ -z ${CORE_BRANCH} ]] then
echo "You have entered empty branch for CORE Module"
else
for ip in ${CORE_SERVER_IP//,/ }; do
echo "$ip"
sshpass -p $password ssh -o StrictHostKeyChecking=no -tt ${USER}#${ip} <<EOF
set -e
sudo -i
set -e
/etc/init.d/tomcat status
exit 0
exit 0
EOF
done
fi'''
}
}
}
sharing below snapshot as additional reference.
Jenkins option
I want to run " docker exec -it bash -c './tmp/script.sh' " this command using gradle from build.gradle. But this command does not work though "docker ps -a" command works fine
I tried in different way, but no luck
1) "docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'".execute().waitFor()
2) CommandLine "docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'"
3) exec{
executable = 'sh'
args "docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'"
}
Full code on build.gradle are as bellow
task dockerDeploy(dependsOn: 'tarArtifact') {
group = "dev"
description = 'Create exploded-war, tar it and deploy to docker'
doLast {
if (devEnvironment) {
if ("docker inspect -f '{{.State.Running}}' resin_container".execute().text.contains('true')) {
println "SHUTTING DOWN Resin Service"
"docker-compose ps".execute().waitFor()
//"docker-compose exec resin_container bash -c './resin/bin/resin-shutdown.sh'".execute().waitFor()
def resin_shutdown = "docker exec -it resin_container bash -c \"./resin/bin/resin-shutdown.sh\""
println "${resin_shutdown}"
exec{
//commandLine = 'docker ps'
commandLine = resin_shutdown
}
println "RESTARTING resin_container"
"docker-compose restart resin_container".execute().waitFor()
} else {
println "STARTING services"
"docker-compose up -d".execute()
}
}
}
}
Expected result like bellow . In here I run the command from shell terminal, but I want to run the same command using gradle build
uzzal$ docker exec -it resin_container bash -c "./resin/bin/resin-
shutdown.sh"
+ GREEN='\033[1;32m'
+ NC='\033[0m'
+ echo 'NODE_NAME : '
NODE_NAME :
+ pushd .
+ echo 'Waiting for server to stop'
Waiting for server to stop
+ sleep 3
+ pgrep -fl -U 999 resin.jar
+ awk '{ print $1 }'
+ xargs kill -SIGTERM
+ sleep 3
+ echo -e '\033[1;32m DONE SHUTTING DOWN \033[0m '
DONE SHUTTING DOWN
+ popd
uzzal$
But Output comes as
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:shutResin'.
A problem occurred starting process 'command 'docker exec -it resin_container bash -c "./resin/bin/resin-shutdown.sh"''
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
I am using below apache groovy script in jenkins pipeline to deploy my artifact(dev.ear) to server. I have embedded shell script in groovy to securely copy dev.ear from jenkins slave to target server(unix server).
node('linux') {
stage('Checkout/Download/Deploy') {
timeout(time: 30, unit: 'MINUTES') {
def ziptmp = '.ziptmp'
output = sh returnStdout: true, script:"/bin/rm -rf ${ziptmp}; /bin/mkdir ${ziptmp}; cd ${ziptmp}; /usr/bin/unzip -qq ${tempdir}/${artifactFilename}; ls -ltr; echo *;
if [ -e dev.ear ]
then
scp dev.ear lsfi#${serverName57}:/apps/wls/dev/applications;
echo "COPIED DEV ARTIFACT TO SERVER"
else
echo "DEPLOYMENT PACKAGE DOESNT CONTAIN DEV ARTIFACT"
fi"
echo "RESULT::: ${output}"
}
}
}
I am getting the below error when I trigger Jenkins job
WorkflowScript: 54: expecting anything but ''\n''; got it anyway # line 54, column 171.
ctFilename}; ls -ltr; echo *;
I removed new lines in the shell script and updated code as below :
def ziptmp = '.ziptmp'
output = sh returnStdout: true, script:"/bin/rm -rf ${ziptmp}; /bin/mkdir ${ziptmp}; cd ${ziptmp}; /usr/bin/unzip -qq ${tempdir}/${artifactFilename}; ls -ltr; echo *; if [ -e dev.ear ] then scp dev.ear lsfi#${serverName57}:/apps/wls/dev/applications; fi;"
echo "RESULT::: ${output}"
But I am getting the below error :
line 2: syntax error near unexpected token `fi'
How to resolve this error.
Groovy doesn't like a newline in a GString. According to the Grails cookbook you can make multiline Strings using either '''Your multiline String''' or """Your multiline ${GString}""".
I'm not very sure on bash syntax, but you also seem to be missing a semicolon after if [ -e dev.ear ] according to these docs.
Putting it all together:
output = sh returnStdout: true, script: """/bin/rm -rf ${ziptmp}; /bin/mkdir ${ziptmp}; cd ${ziptmp}; /usr/bin/unzip -qq ${tempdir}/${artifactFilename}; ls -ltr; echo *;
if [ -e dev.ear ];
then
scp dev.ear lsfi#${serverName57}:/apps/wls/dev/applications;
echo "COPIED DEV ARTIFACT TO SERVER"
else
echo "DEPLOYMENT PACKAGE DOESNT CONTAIN DEV ARTIFACT"
fi"
echo "RESULT::: ${output}"""
I have the following problem. I need to create system-wide JDK_HOME and JAVA_HOME variables. First I want to create /etc/profile.d/java.sh and add
JDK_HOME to it. Then I want to append JAVA_HOME to this file. So far I have this code.
#!/bin/bash
create_env_var()
{
local varname="$1"
local varvalue="$2"
local filename="/etc/profile.d/$3"
if [ -e "$filename" ]; then
echo "**ERROR: file $filename already exists"
else
sh -c 'echo "$varname=$varvalue" > $filename'
chmod +x "$filename"
fi
}
append_env_var()
{
local varname="$1"
local varvalue="$2"
local filename="/etc/profile.d/$3"
if [ ! -e "$filename" ]; then
echo "**ERROR: file $filename not found"
else
sh -c 'echo "$varname=$varvalue" >> $filename'
chmod +x "$filename"
fi
}
create_env_var "JDK_HOME" "/usr/lib/jvm/java-7-openjdk-i386" "java.sh"
append_env_var "JAVA_HOME" "/usr/lib/jvm/java-7-openjdk-i386" "java.sh"
exit "$?"
However these lines don't work and I see the following errors:
sh: 1: cannot create : Directory nonexistent
chmod: cannot access ‘/etc/profile.d/java.sh’: No such file or directory
Would you please show me where everything goes wrong?
While you can remove the single quotes, there's no reason to create a subprocess and use sh -c.
change sh -c 'echo "$varname=$varvalue" > $filename'
to echo "$varname=$varvalue" > $filename
and sh -c 'echo "$varname=$varvalue" >> $filename'
to echo "$varname=$varvalue" >> $filename
I have a user "chad"
I am logging into linux machine with user "server"
I am creating a script and try to add below command
sudo su -s /bin/bash chad
so I can get into user chad and execute rest of my commands from this script
How do I do that?
Below Is My script
#!/bin/bash
exec /bin/su -s /bin/bash chad - << 'EOF'
echo "pwd"
pwd
cd /home/chad/py2.0/test/py_it/
echo "git branch"
git branch
read -p "Are you sure about branch? (yes/no)" reply
choice=$(echo $reply|sed 's/\(.*\)/\L\1/')
if [ "$choice" = 'yes' ]
then
echo "Welcome you selected yes, wait for 3 seconds to get output";
sleep 3;
echo "git pull for py2.0";
"git pull origin integration/py2.0";
elif [ "$choice" = 'no' ]
then
echo "You selected 'no', hence exiting in 3 seconds";
sleep 3
exit 0
else
echo "invalid answer, type yes or no";
fi
EOF
Below is Output
git branch
* integration/py2.0
master
invalid answer, type yes or no
You have to add the following line in your script:
exec sudo -u chad /bin/sh - << 'EOF'
All commands after this line will be under chad user
Check it with this:
id
exec sudo -u chad /bin/sh - << 'EOF'
id
id
id
EOF