0

In a gitlab pipeline (gitlab-ci.yml), I am using SSHPASS to configure a linux based device through a temporary IP for automation purpose.

script:
  - sshpass -p $TARGET_password ssh -o StrictHostKeyChecking=no $TARGET_username@$TARGET_IP 'bash -s' < $configfile

I am sending a script with a bunch of commands, the last one being configure the permanent IP. As soon as the script changes the IP, the job hangs and waits undefinitly for any return because it lost the session with the temporary IP.

Question:

  • Is it possible to send the command that changes the IP and immediately disconnect not waiting for any return.
  • Is there a gitlab-ci instruction to not wait for any return from 'script' section?
    I know there is "on-success" and "on-failure" but these suppose wwe have a stdout or stderr retuen.
AJN
  • 1,196
  • 2
  • 19
  • 47
  • Is it possible to split `$configfile` input to all that is unrelated to changing ip and then a single small script that changes ip? How is the ip changed? – KamilCuk Apr 19 '21 at 17:52

1 Answers1

2

If you know approximate time of execution of your script (i.e. 10 seconds) you can use timeout utility, something like

timeout 10 <your command here>

The neat thing about timeout is that it exits with error code 124 if timeout has been reached, so if your script doesn't use this exit code for anything, you can make something like

timeout 10 <your command here> || { EXITCODE=$?; if [ $EXITCODE != 124 ]; then exit $EXITCODE; fi; }

It will fail your pipeline if your script exited with anything other than 0 or 124

Tested with:

echo sleep 11 > good_night.txt 
timeout 10 ssh some_host 'bash -s' < good_night.txt || { EXITCODE=$?; echo $EXITCODE; if [ $EXITCODE != 124 ]; then exit $EXITCODE; fi;  }
124

Check How to set ssh timeout? for other ssh timeout options

Andrew
  • 3,912
  • 17
  • 28
  • Brilliant @Andrew! Thanks a lot, both timeout & ssh timeout option work like a charm. – AJN Apr 25 '21 at 13:23