# Prevent deployments from different builds from running in parallel

**URL:** <https://travis-ci.community/t/prevent-deployments-from-different-builds-from-running-in-parallel/9968>\
**Category:** v2\
**Created:** [September 22, 2020, 7:26am UTC](https://travis-ci.community/t/prevent-deployments-from-different-builds-from-running-in-parallel/9968 "2020-09-22T07:26:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jehon](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@jehon](https://travis-ci.community/u/jehon)\
**Post date:** [September 22, 2020, 7:26am UTC](https://travis-ci.community/t/prevent-deployments-from-different-builds-from-running-in-parallel/9968/1 "2020-09-22T07:26:10Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [September 22, 2020, 9:35am UTC](https://travis-ci.community/t/prevent-deployments-from-different-builds-from-running-in-parallel/9968/3 "2020-09-22T09:35:00Z")

</div>

You can only do that at your server’s side. E.g. [https://stackoverflow.com/questions/20140743/using-file-locks-with-rsync](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

```auto
flock "<lock file>" rsync $@

```

(Just creating an indicator file instead of `flock` is unsafe because it’s vulnerable to [TOCTOU](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use) 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):

```bash
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)"`.
