0

For a bitbucket pipeline I'm using the following after-script:

          after-script:
            - |-
              ssh -tt -p 222 user@example.com "/bin/bash" << EOF
              cd public_html/staging && npm i && pm2 delete my-app || : && VITE_BASE_URL=$VITE_BASE_URL pm2 -f start build/index.js --update-env --name my-app
              EOF

Here I do the following:

  1. connect to the server and use bash
  2. start a multiline command
  3. move to the copied code
  4. install dependencies
  5. delete app if exists, then start it
  6. end the script

Now for step 6 it doesn't work as expected. The process doesn't stop and the pipeline keeps running.

I assume I made some kind of syntax mistake, but I can't figure out what I did wrong.

best regards

EDIT 1: When removing the flag -tt I get the following errors:

/bin/bash: line 1: npm: command not found
/bin/bash: line 1: pm2: command not found

I don't think there is anything I can do about that, since I am not able to change the servers settings

removing the last EOT in the code, causes this error to pop up, while the process doesn't finish:

schwad@dedivirt2847:~/public_html/staging$ printf "\n"

EDIT 2:

replacing |- with | doesn't change anything. The process still doesn't terminate.

Removing the last EOF has the same effect

pgalle
  • 216
  • 3
  • 13
  • For debugging purposes try to use `set -x`, more about the command here https://stackoverflow.com/a/36273740/9935369 – Roberto Manfreda May 23 '23 at 14:15
  • I don't know how pm2 works, maybe the issue here is that the last part `pm2 -f start...` is a blocking operation!? You can try by adding an `&` at the end of your command, in order to send it in background, or using the `nohup`. – Roberto Manfreda May 23 '23 at 14:22
  • `-tt` may be tricking the remote shell into starting interactively. You usually don't need it to run a command over ssh, try removing it. – tjm3772 May 23 '23 at 14:29
  • Why `|-`? That removes the final newline from the script. For all I know, `EOF` needs a succeeding newline to work properly. – flyx May 23 '23 at 14:41
  • You write in your question for 6. that you end your script. Could you please elaborate on that, i.e. which command do you execute to end the after-script, maybe it is just me, but I couldn't find anything for that. The opposite actually: PM2 is a daemon process manager, you're starting a long running process, and it would be the first thing to consider that it just continues running. Last but not least: Why do you do this in an `after-script` ? – hakre May 23 '23 at 15:40
  • EOF, is the last part in this script. That's supposed to end the whole deployment process. There is nothing in the after script. `pm2` only starts the process, I'm not watching it. Is there a better way to do this than in the `after-script`? It's my first time using it. In the `script` part I'm building and uploading the code. – pgalle May 23 '23 at 15:47
  • EOF is the end of the heredoc string, that in itself does not end whole the deployment process. keep it together with the `script`, `after-script` is to react to an exit code of `script`. – hakre May 23 '23 at 16:44
  • Is there a reason why you can't make the script you are executing on the server be it's own script file that you call with the ssh? – Randommm May 24 '23 at 12:30

1 Answers1

0

I could make it work by calling exit 0 manually:

         after-script:
            - |-
              ssh -tt -p 222 user@example.com "/bin/bash" << EOF
              cd public_html/staging && npm i && pm2 delete my-app || : && VITE_BASE_URL=$VITE_BASE_URL pm2 -f start build/index.js --update-env --name my-app
            - exit 0
pgalle
  • 216
  • 3
  • 13