Jenkins build passes when ssh deploy fails - deployment

I'm using a here tag in a Jenkins build step to send my deploy commands over ssh, and unfortunately the build is passing even when the commands inside the here tag don't finish successfully:
ssh user#host <<EOF
cd /path/to/app
git pull
bower install
npm install
grunt build
cp -r /path/to/app/dist/* /path/to/dist/
forever restartall
exit
EOF
Is there a better way to approach this problem?

You are not catching any error codes inside your "here document".
Last command is exit and without an exit code, it will default to 0 which is success.
Since the last command of your ssh is a success, the whole command is treated as success, and build is a success.
Easiest way to fix that: chain all commands with && like so:
cd /path/to/app && git pull && bower install && npm install && grunt build && cp -r /path/to/app/dist/* /path/to/dist/ && forever restartall && exit
Best way to fix that: write a proper shell script, with error handling, and execute that. If you are too lazy to error handle every line, you can start the script with set -e which will fail the shell script on any individual error
Edit:
#!/bin/bash
appPath="/path/to/app"
distPath"/path/to/dist"
echo "My great deployment script"
echo "Deploying ${appPath} to ${distPath}
if [[ ! -w "${appPath}" ]]; then
echo "${appPath} is not writable, quitting"
exit 1
else
cd ${appPath} && git pull || { echo "Failed on 'git pull'"; exit 2; }
bower install || { echo "Failed on 'bower install'"; exit 3; }
npm install || { echo "Failed on 'npm install'"; exit 4; }
grunt build || { echo "Failed on 'grunt build'"; exit 5; }
if [[ -w "${distPath}" ]]; then
cp -r ${appPath}/dist/* ${distPath}/ || { echo "Failed on 'copy'"; exit 1 }
forever restartall || { echo "Failed on 'forever restartall'"; exit 6 }
echo "Deployment successful"
exit 0
fi
fi
You then execute it with: ssh user#host 'bash -s' < myfile.sh (if the file is local)
Or if you place the file remotely, then just: ssh user#host '/path/to/remote/myfile.sh'

Related

Getting error "Not Found" when the script is trying to find .sh in Dockerfile

DockerFile - Path is rootDirectory/odm-ondocker/decisionserver/decisionserverconsole.
ARG FROMLIBERTY
FROM maven:3.5.2-jdk-8-alpine AS builder
ARG ODMDOCKERDIR
ENV ODMDOCKERDIR $ODMDOCKERDIR
ENV SCRIPT /script
ENV APPS /config/apps
COPY $ODMDOCKERDIR/welcomepage /welcomepage
COPY $ODMDOCKERDIR/common/script $SCRIPT
COPY $ODMDOCKERDIR/common/drivers /config/resources
COPY $ODMDOCKERDIR/common/features $SCRIPT
COPY $ODMDOCKERDIR/decisionserver/decisionserverconsole/script $SCRIPT
COPY $ODMDOCKERDIR/decisionserver/decisionserverruntime/script $SCRIPT
# Use production liberty if needed
RUN echo $SCRIPT
COPY $ODMDOCKERDIR/resources/* /wlp-embeddable/
RUN chmod a+x $SCRIPT/fixWLPForProduction.sh && sync && $SCRIPT/fixWLPForProduction.sh
# Install missing require package in the alpine builder image
RUN apk add --no-cache bash perl ca-certificates wget
# Build Welcome page
RUN cd /welcomepage; mvn -B clean install | grep -v 'Download.*' && mkdir -p $APPS
I am getting error on line RUN chmod a+x $SCRIPT/fixWLPForProduction.sh && sync && $SCRIPT/fixWLPForProduction.sh as below:-
/bin/sh: /script/fixWLPForProduction.sh: not found
ERROR: Service 'odm-decisionserverconsole' failed to build: The command '/bin/sh -c chmod a+x $SCRIPT/fixWLPForProduction.sh && sync && $SCRIPT/fixWLPForProduction.sh' returned a non-zero code: 127
I am new to docker so not able to figure out why it's coming although the file is present inside root directory/common/scripts folder. I figured out, it's trying to find script folder which is under /odm-ondocker/common/script. I tried giving value of SCRIPT variable as /common/script but still it's giving as Not Found.
RUN chmod a+x $SCRIPT/fixWLPForProduction.sh && sync && $SCRIPT/fixWLPForProduction.sh
is looking for the script /script/fixWLPForProduction.sh as defined by your ENV SCRIPT command. But you said in your comment the script is /common/script/fixWLPForProduction.sh Trying using this instead
ENV SCRIPT /common/script Or use the absolute path for the script. From there, you can know how to properly set your ENV variable

Bluemix: cf push using DEA instead of DIEGO architecture

When deploying an application into dedicated Bluemix it uses DEA architecture by default. How can I force it to use DIEGO architecture instead?
You have to use more steps. Deploy without start, switch to diego, start.
cf push APPLICATION_NAME --no-start
cf disable-diego APPLICATION_NAME
cf start APPLICATION_NAME
Ref Deploying Apps
I built a bash exec to do this, which will use your existing manifest.yml file and pack all of this into a single request. The contents of the bash exec follow:
#!/bin/bash
filename="manifest.yml"
if [ -e $filename ];
then
echo "using manifest.yml file in this directory"
else
echo "no manifest.yml file found. exiting"
exit -2
fi
shopt -s nocasematch
string='name:'
targetName=""
echo "Retrieving name from manifest file"
while read -r line
do
name="$line"
variable=${name%%:*}
if [[ $variable == *"name"* ]]
then
inBound=${name#*:}
targetName="$(echo -e "${inBound}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
fi
done < "$filename"
if [ "$targetName" == "" ];
then
echo "Could not find name of application in manifest.yml file. Cancelling build."
echo "application name is identified by the 'name: ' term in the manifest.yml file"
exit -1
else
echo "starting cf push for $targetName"
cf push --no-start
echo "cf enable-diego $targetName"
cf enable-diego $targetName
echo "cf start $targetName"
cf start $targetName
exit 0
fi
Just put this code into your editor as a new file and then make the file executable. I keep a copy of this exec in each of my repos in the root directory. After doing a copy-paste and executing this exec, you may get the following error:
/bin/bash^M: bad interpreter: No such file or directory
If you do, just run the dos2unix command and it will 'fix up' the line endings to match your os.

Capistrano doesn't obey "within release_path"

I have this task:
namespace :custom do
desc "create a symlink to db config already on the server"
task :symlink_db_config do
on roles(:web) do
within release_path do
execute "pwd"
end
within release_path do
execute "ln -nfs /home/blog/config/database.yml ./database.yml"
end
end
end
end
For some infuriating reason, the pwd command is preceded by a cd to the release path, but the ln command is not. Why is that?
Here's the output, showing the above:
** Invoke custom:symlink_db_config (first_time)
** Execute custom:symlink_db_config
DEBUG[352cc4bb] Running /usr/bin/env if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi on 172.245.32.193
DEBUG[352cc4bb] Command: if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi
DEBUG[352cc4bb] Finished in 0.199 seconds with exit status 0 (successful).
// Here's the `pwd`; note the proper `cd` that occurs first:
INFO[67a83a04] Running /usr/bin/env pwd on 172.245.32.193
DEBUG[67a83a04] Command: cd /home/blog/staging/releases/20141010050707 && /usr/bin/env pwd
DEBUG[67a83a04] /home/blog/staging/releases/20141010050707
INFO[67a83a04] Finished in 0.268 seconds with exit status 0 (successful).
DEBUG[f46f64b3] Running /usr/bin/env if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi on 172.245.32.193
DEBUG[f46f64b3] Command: if test ! -d /home/blog/staging/releases/20141010050707; then echo "Directory does not exist '/home/blog/staging/releases/20141010050707'" 1>&2; false; fi
DEBUG[f46f64b3] Finished in 0.243 seconds with exit status 0 (successful).
//And now here's the `ln`... but where is the `cd`? I said `within release_path`, didn't I?
INFO[afdbd89c] Running /usr/bin/env ln -nfs /home/blog/config/database.yml ./database.yml on 172.245.32.193
DEBUG[afdbd89c] Command: ln -nfs /home/blog/config/database.yml ./database.yml
INFO[afdbd89c] Finished in 0.219 seconds with exit status 0 (successful).
So my ln fails because it's not in the right directory. Why didn't capistrano cd into the release directory like I told it to?
(Capistrano 3.2.1, by the way)
There's useful information in another answer here but briefly it seems the problem arises when there are spaces in the command you want to run.
I followed bricker's suggestion, e.g.
within release_path do
execute *%w[ ln -nfs /home/blog/config/database.yml ./database.yml ]
end

Running last command with sudo in fish only works if it has no arguments?

I'm trying to write a function that does the equivalent of sudo !! in Bash. It works, but only when the last command has no arguments.
So far the function is:
function s --description "Run last command (or specified command) using sudo"
if test $argv
switch $argv[1]
case '!!'
command sudo (echo $history[1])
case '*'
command sudo $argv
end
else
command sudo fish
end
end
Testing the relevant line:
$ command sudo whoami
root
$ whoami
nick
$ command sudo (echo $history[1])
root
So far so good, now lets try a command with a few args:
$ echo hi >> /etc/motd
An error occurred while redirecting file '/etc/motd'
open: Permission denied
$ command sudo (echo $history[1])
sudo: echo hi >> /etc/motd: command not found
Hmm, strange.
Got it working using eval.
function sudo --description 'Run command using sudo (use !! for last command)'
if test (count $argv) -gt 0
switch $argv[1]
case '!!'
if test (count $argv) -gt 1
set cmd "command sudo $history[1] $argv[2..-1]"
else
set cmd "command sudo $history[1]"
end
case '*'
set cmd "command sudo $argv"
end
else
set cmd "command sudo fish"
end
eval $cmd
end
I had the same problem as you, and I fixed it by using oh-my-fish
(it's a plugin manager for fish shell) https://github.com/oh-my-fish/oh-my-fish. You can install it with this command :
curl -L https://get.oh-my.fish | fish
Then install the plugin bang-bang (to allow !! and !$) with this command :
omf install bang-bang

Rails 3 - Script doesn't run after cap:deploy

I want to run few commands when i start application on server.
So, i wrote such script in config/deploy.rb:
desc "Start sphinx"
task :start_sphinx, :roles => :app do
run "cd #{current_path} && bundle install --without development test && bundle install --deployment && chmod 777 -R #{current_path}/tmp/ && rake thinking_sphinx:configure && rake thinking_sphinx:start"
end
But when i print
$ cap deploy
It doesn't work. So, please, tell me what is wrong!
Here is log from terminal:
$ cap deploy
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote git://github.com/Loremaster/sample_app.git master"
command finished in 367ms
* executing "if [ -d /vol/www/apps/ror_tutorial/shared/cached-copy ]; then cd /vol/www/apps/ror_tutorial/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard b5073e9b9aefd98873489bb6e97249593ea1a978 && git clean -q -d -x -f; else git clone -q git://github.com/Loremaster/sample_app.git /vol/www/apps/ror_tutorial/shared/cached-copy && cd /vol/www/apps/ror_tutorial/shared/cached-copy && git checkout -q -b deploy b5073e9b9aefd98873489bb6e97249593ea1a978; fi"
servers: ["188.127.224.136"]
Password:
[188.127.224.136] executing command
command finished in 3070ms
copying the cached version to /vol/www/apps/ror_tutorial/releases/20120125165835
* executing "cp -RPp /vol/www/apps/ror_tutorial/shared/cached-copy /vol/www/apps/ror_tutorial/releases/20120125165835 && (echo b5073e9b9aefd98873489bb6e97249593ea1a978 > /vol/www/apps/ror_tutorial/releases/20120125165835/REVISION)"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 49970ms
* executing `deploy:finalize_update'
* executing "chmod -R g+w /vol/www/apps/ror_tutorial/releases/20120125165835"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 1023ms
* executing "rm -rf /vol/www/apps/ror_tutorial/releases/20120125165835/log /vol/www/apps/ror_tutorial/releases/20120125165835/public/system /vol/www/apps/ror_tutorial/releases/20120125165835/tmp/pids &&\\\n mkdir -p /vol/www/apps/ror_tutorial/releases/20120125165835/public &&\\\n mkdir -p /vol/www/apps/ror_tutorial/releases/20120125165835/tmp &&\\\n ln -s /vol/www/apps/ror_tutorial/shared/log /vol/www/apps/ror_tutorial/releases/20120125165835/log &&\\\n ln -s /vol/www/apps/ror_tutorial/shared/system /vol/www/apps/ror_tutorial/releases/20120125165835/public/system &&\\\n ln -s /vol/www/apps/ror_tutorial/shared/pids /vol/www/apps/ror_tutorial/releases/20120125165835/tmp/pids"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 150ms
* executing "find /vol/www/apps/ror_tutorial/releases/20120125165835/public/images /vol/www/apps/ror_tutorial/releases/20120125165835/public/stylesheets /vol/www/apps/ror_tutorial/releases/20120125165835/public/javascripts -exec touch -t 201201251659.26 {} ';'; true"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
** [out :: 188.127.224.136] find: `/vol/www/apps/ror_tutorial/releases/20120125165835/public/images': No such file or directory
** [out :: 188.127.224.136] find: `/vol/www/apps/ror_tutorial/releases/20120125165835/public/stylesheets': No such file or directory
** [out :: 188.127.224.136] find: `/vol/www/apps/ror_tutorial/releases/20120125165835/public/javascripts': No such file or directory
command finished in 566ms
* executing `deploy:symlink'
* executing "rm -f /vol/www/apps/ror_tutorial/current && ln -s /vol/www/apps/ror_tutorial/releases/20120125165835 /vol/www/apps/ror_tutorial/current"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 56ms
** transaction: commit
* executing `deploy:restart'
* executing "sudo -p 'sudo password: ' touch /vol/www/apps/ror_tutorial/current/tmp/restart.txt"
servers: ["188.127.224.136"]
[188.127.224.136] executing command
command finished in 163ms
It looks like you are missing the callback. For example, if you wanted to run your script after the code has been updated, you can use an after callback like
after "deploy:update_code", "start_sphinx"
For more info on callbacks, check out the callbacks rdoc