Prevent deployments from different builds from running in parallel

Dear all,

My deploy step is basically a rsync over sftp to my web server.

But when two deploy run in parallel (in two different run of master), based on different code, the final result is a mix of the two deploy.

Is it possible to have an option to wait for the previous deploy to finish before running the next one?

Travis has an option to limit concurrent jobs, that I did activate as a workaround, but that also prevent other branches to be built in parallel, even though they don’t deploy…

Please redirect me if I am not in the correct place :slight_smile:

Thanks for your support and have a nice day all!
Jehon

You can only do that at your server’s side. E.g. https://stackoverflow.com/questions/20140743/using-file-locks-with-rsync suggests specifying a wrapper script on the remote machine with --rsync-path that would run

flock "<lock file>" rsync $@

(Just creating an indicator file instead of flock is unsafe because it’s vulnerable to TOCTOU if the deployments happen to start at the same time.)


You will likely also need additional logic to prevent a deployment from an earlier commit from overwriting one from a later one. E.g. you could do the following in the remote script (be advised that the @Q syntax is new in Bash 4.4):

COMMIT_TIMESTAMP=$1
shift
flock "<lock file>" -c '
  PREV_TIMESTAMP=$(cat <timestamp file>);
  if [[ '$COMMIT_TIMESTAMP' -gt $PREV_TIMESTAMP ]]; then
   echo '$COMMIT_TIMESTAMP' ><timestamp file>
   rsync '"${@@Q}"'
  else
   exit 1
  fi
'

then specify the remote script in your build with --rsync-path="<wrapper> $(git show -s --format=%ct)".

Imprint