flosell / lambdacd

a library to define a continuous delivery pipeline in code
https://www.lambda.cd/
Apache License 2.0
677 stars 59 forks source link

It is tricky to write a `deploy.sh` which can invoke a service and finish normally #196

Open humorless opened 5 years ago

humorless commented 5 years ago

I used the bash library in lambacd to invoke my deployment script.

(defn deploy [args ctx]
  (shell/bash ctx (:cwd args) "./deploy.sh"))

I put a very simple deploy.sh with certain code snippet like this:

cd ~/backend && nohup java -Dconf=~/backend/config.edn -jar ~/backend/clj-crm.jar &

exit 0

It looked normal to me, but in practice, the deploy never finished. When I took a look from pstree. I saw something like

--- deploy.sh --- java ...

It looked like deploy.sh was blocked by my service. However, I have used nohup command and &.

I changed my code snippet to something like:

tmux new-session -d -s backend 'cd ~/backend && java -Dconf=~/backend/config.edn -jar ~/backend/clj-crm.jar'

exit 0

This time, the deploy could successfully finish, was not blocked by java service anymore. However, I still do not understand why.

flosell commented 5 years ago

Hi, I'm not an expert on this but I think this has something to do with how output streams are handled: nohup fails to detect a terminal when running inside LambdaCD so it doesn't redirect the output to nohup.out as it usually does. This seems to block the shell from finishing. Also, for some reason, it only worked for me when I didn't link cd and nohup with &&, no idea why.

But this seems to work:

cd ~/backend
nohup java -Dconf=~/backend/config.edn -jar ~/backend/clj-crm.jar > logfile.txt 2>&1 &