Skip to main content
 首页 » 编程设计

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

2025年04月02日14lvdongjie

问题

我目前正在设置 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 

通缉情况

所以我真正想要的是计划/延迟部署之类的东西。当 Travis 检测到需要运行一个昂贵的操作时,它会安排部署,所有后续构建都会跳过部署,直到使用昂贵脚本的部署完成。该部署应在工作时间以外的任何地方自动启动。


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

请您参考如下方法:

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

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

  2. 让 git 检查过去 24 小时内迁移/索引文件是否有任何更改,如果有则跳过部署。

  3. 检查 travis 事件类型并仅在 cron 运行时执行索引/迁移。

  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"