Speed up assets:precompile with Rails 3.1/3.2 Capistrano deployment

前端 未结 7 714
野趣味
野趣味 2020-12-04 05:20

My deployments are slow, they take at least 3 minutes. The slow Capistrano task during deploy is assets:precompile. This takes probably 99% of the total deploy time. How can

7条回答
  •  情深已故
    2020-12-04 05:43

    The OP explicitly asked for Capistrano, but in case you are deploying without a dedicated deploy tool (via bash script, Ansible playbook or similar), you may use the following steps to speed up your Rails deploys:

    • Skip bundle installation
      bundle check returns 1 if there are gems to install (1 otherwise) so it's easy to skip bundle installation if not necessary.

    • Skip asset precompilation
      Use git rev-parse HEAD before pulling changes and store the current version's SHA in a variable (say $previous_commit). Then pull changes and find out if assets have changed with the command git diff --name-only $previous_commit HEAD | grep -E "(app|lib|vendor)/assets". If this returns $1 you can safely skip asset precompilation (if you use release-based deploys you may want to copy your assets to your new release's directory).

    • Skip database migrations
      If you are using MySQL, use the command mysql --user=USER --password=PASSWORD --batch --skip-column-names --execute="USE MYAPP; SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1;" from your applcation's root directory to get the name of the latest applied migration. Compare this to the output of the command ls db/migrate | tail -1 | cut -d '_' -f 1 (which returns the latest available migration). If they differ, you need to migrate. If not, you can skip database migrations.

    Rails developers deploying with Ansible can further reduce their deploy time by turning facts gathering off if not needed (gather_facts: no) and use SSH pipelining (export ANSIBLE_SSH_PIPELINING=1).

    If you want more detail, I recently wrote an article about this topic.

提交回复
热议问题