I have a problem with variables in sshexec task.
My build.xml ant script looks like:
<?xml version="1.0" encoding="UTF-8"?>
<project name="sshexecproject" basedir="." default="build">
<target name="build">
<sshexec
host="192.168.2.106"
username="root"
password="xxx"
commandResource="${basedir}/to_run.sh"
trust="true"
verbose="true"
failonerror="true"
/>
</target>
In the to_run script I have two variables:
#!/bin/bash
name="Pink Panther"
export name2="Panther Pink"
echo "Name: "
echo $name
echo "Name with export: "
echo $name2
If I run the script on terminal i get the following output:
$ ./to_run.sh
Name:
Pink Panther
Name with export:
Panther Pink
We can see that all works fine. But if I start the build.xml script from ant i get the following output:
...
[sshexec] Authentication succeeded (password).
[sshexec] cmd : #!/bin/bash
[sshexec] cmd :
[sshexec] cmd : name="Pink Panther"
[sshexec] cmd : export name2="Panther Pink"
[sshexec] cmd :
[sshexec] cmd : echo "Name: "
[sshexec] Name:
[sshexec] cmd : echo $name
[sshexec]
[sshexec] cmd : echo "Name with export: "
[sshexec] Name with export:
[sshexec] cmd : echo $name2
[sshexec]
[sshexec] Disconnecting from ...
We can see that on the remote server this script create an empty echo. The variable name and name2 is not filled. Why?
Changing this line:
commandResource="${basedir}/to_run.sh"
to
command="${basedir}/to_run.sh"
results in the following:
[sshexec] Authentication succeeded (password).
[sshexec] cmd : /data/tmp/anttest/to_run.sh
[sshexec] Name:
[sshexec] Pink Panther
[sshexec] Name with export:
[sshexec] Panther Pink
commandResource takes a resource file with a list of commands and executes each line individually with bash -c $LINE so any variables defined are only valid on the same line. command executes the whole script in the same shell.
Related
I have the following Docker File (from ubuntu image):
...
WORKDIR ${DJANGO_BASE_DIR} //--> /opt/django
COPY --chown=${USERNAME}:${USERNAME} /deployment/entrypoint.sh ${DJANGO_BASE_DIR}
USER ${USERNAME}
RUN echo ${DJANGO_BASE_DIR} //--> /opt/django
CMD ["bash","entrypoint.sh"] // I also tried ENTRYPOINT
My entrypoint is the following:
...
echo "Waiting for postgres..."
while ! nc -z $DB_HOST $DB_PORT; do
sleep 0.1
done
cd
echo "PostgreSQL started"
echo $DJANGO_DEBUG
echo $(pwd) // display /home/django
....
I don't understand why my entrypoint is running on /home/django directory; I expect it to run on the WORKDIR, which is /opt/django
The cd command in your entrypoint script sets the working directory to the current user's home directory. Remove that and it should work fine.
I have a Jenkins stage as:
stage("Deploy on Server") {
steps {
script {
sh 'sshpass -p "password" ssh -o "StrictHostKeyChecking=no" username#server "cd ../../to/app/path; sh redeploy.sh && exit;"'
}
}
}
and some scripts on my server (centos):
redeploy.sh:
declare -i result=0
...
sh restart.sh
result+=$?
echo "Step 6: result = " $result
# 7. if restart fail, restart /versions/*.jar "sh restart-previous.sh"
if [ $result != "0" ]
then
sh restart-previous.sh
result+=$?
fi
echo "Deploy done. Final result: " $result
restart.sh
nohup java -Xms8g -Xmx8g -jar app-name-1.0-allinone.jar &
Because I execute the redeploy.sh script from the Jenkins, the problem is that it will cling on Jenkins console and will log all application events there, instead to create a nohup file in the patch where my app is deployed.
In some examples I found that it is recommended to use nohup directly in ssh command, but I can't do this because I need to execute a script (with all the steps, nohup can't doing that) and not directly a command.
exit cmd will be ignored because the previous command will never be closed.
thanks
Finally, I found the solution. One problem was in restart.sh, because is needed to force from cmd to specify the log file. So, nohup is ignored/unused, and the command become:
java -Xms8g -Xmx8g -jar app-name-1.0-allinone.jar </dev/null>> logfile.log 2>&1 &
Another problem was with killing the previous jar process. Be very carrefour, because using project name as path in jenkins script, this will create a new process for your user and will be accidentally killed when you will want to stop your application:
def statusCode = sh returnStatus: true, script: 'sshpass -p "password" ssh -o "StrictHostKeyChecking=no" username#server "cd ../../to/app/path/app-folder; sh redeploy.sh;"'
if (statusCode != 0) {
currentBuild.result = 'FAILURE'
echo "FAILURE"
}
stop.sh
if pgrep -u username -f app-name
then
pkill -u username -f app-name
fi
# (app-name is a string, some words from the running cmd to open tha application)
Because app-folder from Jenkins script and app-name from stop.sh are equals (or even app-folder contains app-name value), when you'll try to kill app-name process, accidentally you'll kill the ssh connection and Jenkins will get 255 status code, but the redeploy.sh script from server will be done successfully because it will be executed independently.
The solution is so simple, but hard to be discovered. You should be sure that you give an explicit name for your search command which will find only and only the process id of your application.
Finally, stop.sh must be as:
if pgrep -u username -f my-app-v1.0.jar
then
pkill -u username -f my-app-v1.0.jar
fi
Building docker image.......
/usr/bin/env: ‘sh\r’: No such file or directory
execution failed
complete script
#!/bin/bash
echo "Building docker image......."
. gradle.properties
IMAGE="$dockerRegistry/$1"
IMAGE_TAG="$releaseVersion-$(git log -1 --pretty=%h)"
if ./gradlew clean :$1:jibDockerBuild -x test; then
echo "Pushing docker image...."
docker tag $IMAGE $IMAGE:$IMAGE_TAG
docker push $IMAGE:$IMAGE_TAG
else
echo "execution failed"
exit 1
fi
exit 0
Try to adjust your file endings for gradlew file:
dos2unix .\gradlew
i would like to change the default path from Jenkins under /var/lib/jenkins to /data
I try a few things like change the path under /etc/default/jenkins the JENKINS_HOME or add a file under /etc/profile.d/jenkins.sh with the following content : export JENKINS_HOME=/data i already copy all files under /var/lib/jenkins with cp -R /var/lib/jenkins/* /data But everytime i become the following Error in the Jenkins GUI :
---- Debugging information ----
message : com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy
class : hudson.model.Hudson
required-type : hudson.model.Hudson
converter-type : hudson.util.RobustReflectionConverter
path : /hudson/authorizationStrategy
line number : 14
version : not available
-------------------------------
env :
root#ubuntu:/home/ubuntu# env
SHELL=/bin/bash
TERM=xterm
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd =40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;4 4:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01; 31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7 z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=0 1;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz =01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:* .rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;3 1:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm =01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35: *.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=0 1;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:* .m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01; 35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl= 01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.o gv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36 :*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=0 0;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
SUDO_USER=ubuntu
SUDO_UID=1000
USERNAME=root
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us r/local/games
PWD=/home/ubuntu
LANG=de_DE.UTF-8
SHLVL=1
SUDO_COMMAND=/bin/su
HOME=/root
LOGNAME=root
LESSOPEN=| /usr/bin/lesspipe %s
SUDO_GID=1000
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/env
root#ubuntu:/home/ubuntu#
root .profile File
~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n || true
export JENKINS_HOME=/data
So your JENKINS_HOME environment variable is not correctly set.
For Ubuntu, you have to update this profile file ~/.profile and add this line export JENKINS_HOME=/data.
Next, you have to logout/login to take into account this change.
If Jenkins is running with a system account, open a session with this account and launch this command: . ~/.profile
I have a simple docker command
$ docker rm 1234
> Error response from daemon: No such container: 1234
Integration this into ant gives:
<target name="demo">
<exec executable="docker">
<arg value="rm 1234" />
</exec>
</target>
> ant -v demo
> demo:
[exec] Current OS is Linux
[exec] Executing 'docker' with arguments:
[exec] 'rm 1234'
[exec]
[exec] The ' characters around the executable and arguments are
[exec] not part of the command.
[exec] docker: 'rm 1234' is not a docker command. See 'docker --help'.
[exec] Result: 1
Can someone explain to me (a) where this different behavior comes from and (b) how to fix it (getting docker run from ant-target)?
You are passing a single argument through, your ant is the equivalent of saying
$ docker 'rm 1234'
Try
<target name="demo">
<exec executable="docker">
<arg value="rm" />
<arg value="1234" />
</exec>
</target>