AJN
AJN

Reputation: 1206

Make gitlab job not wait for any return from "script" section

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:

Upvotes: 0

Views: 1254

Answers (1)

Andrew
Andrew

Reputation: 4642

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

Upvotes: 2

Related Questions