使用 travis 和 heroku 进行持续部署——保留某种状态

Continuous deployment using travis and heroku - preserving some kind of state

问题

我目前正在设置 Travis 以 运行 我们的测试并在测试成功时进行部署。这很简单,但我不想在满足这些条件时进行部署:

 - The time is between 07:00 and 22:00 (workdays)
 - During the deploy the database has to migrate or elastic search has to index

因为迁移或索引可能非常昂贵并且意味着停机。我正在使用 heroku 提供程序并使用 on 参数很简单,但是我遇到以下情况:

 - Git push triggers new build
 - Travis correctly identifies that a deploy is not allowed
 - Deploy is skipped using the heroku provider `on` parameter
 - Build finishes

5 分钟后

 - Git push triggers new build
 - Travis incorrectly identifies a deploy is allowed (this build doesn't need a migration/index but the previous build did)
 - Travis tries to deploy and production breaks

通缉情况

所以我真正想要的是 scheduled/delayed 部署。当 Travis 检测到其中一项昂贵的操作需要 运行 时,它会安排部署,所有后续构建都会跳过部署,直到完成昂贵脚本的部署。该部署应在工作时间以外的任何地方自动启动。


希望我已经描述清楚了,如果需要更多信息,请告诉我!

好的,在 Travis 的人们的大力帮助下,我想出了以下解决方案:

  1. 我已经激活了 CronJobs 并将它们设置为每天 运行。在撰写本文时,您无法指定 crons 运行 的时间,而是在您创建它们的时间左右安排它们。因此,在半夜创建一个 cron,每晚 运行s。

  2. 让git检查migration/index文件在过去24小时内是否有任何变化,如果有则跳过部署。

  3. 检查 travis 事件类型,如果它是 cron 运行ning,则只执行 index/migrations。

  4. 利润。

代码

deploy:
  - provider: heroku
    skip_cleanup: true
    api_key: "${HEROKU_API_KEY}"
    app: cd-test
    on:
      # Condition is: cron job + migration and/or index necessary
      condition: "$TRAVIS_EVENT_TYPE == 'cron' && $(git log --since='yesterday 23:00' --format=oneline -- **/migrations/* **/search.py | wc -l) -gt 0"
    run:
      - "python manage.py migrate"
      - "python manage.py collectstatic --noinput"
      - "python manage.py index"

  - provider: heroku
    skip_cleanup: true
    api_key: "${HEROKU_API_KEY}"
    app: cd-test
    on:
      # Condition is: not cron job + no migration and/or index necessary
      condition: "$TRAVIS_EVENT_TYPE != 'cron' && $(git log --since='yesterday 23:00' --format=oneline -- **/migrations/* **/search.py | wc -l) == 0"
    run:
      - "python manage.py collectstatic --noinput"