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

时间:2016-06-08 09:09:12

标签: python django heroku travis-ci continuous-deployment

问题

我目前正在设置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检测到需要运行的昂贵操作之一时,它会安排部署,所有后续构建都会跳过部署,直到使用昂贵的脚本完成部署。该部署应该在工作时间以外的任何地方自动启动。

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

1 个答案:

答案 0 :(得分:1)

好的,在特拉维斯的人们的帮助下,我提出了以下解决方案:

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

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

  3. 检查travis事件类型,只有在cron运行时才执行索引/迁移。

  4. 利润。

  5. 代码

    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"
    
相关问题