Skip to content

[Cookbook] Custom compile steps on Heroku #5088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions cookbook/deployment/heroku.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,85 @@ You should be seeing your Symfony application in your browser.
AcmeDemoBundle is only loaded in the dev environment (check out your
``AppKernel`` class). Try opening ``/app/example`` from the AppBundle.

Custom Compile Steps
~~~~~~~~~~~~~~~~~~~~

If you wish to execute additional custom commands during a build, you can leverage
Heroku's `custom compile steps`_. Imagine you want to remove the ``dev`` front controller
from your production environment on Heroku in order to avoid a potential vulnerability.
Adding a command to remove ``web/app_dev.php`` to Composer's `post-install-commands`_ would
work, but it also removes the controller in your local development environment on each
``composer install`` or ``composer update`` respectively. Instead, you can add a
`custom Composer command`_ named ``compile`` (this key name is a Heroku convention) to the
``scripts`` section of your ``composer.json``. The listed commands hook into Heroku's deploy
process:

.. code-block:: json

{
"scripts": {
"compile": [
"rm web/app_dev.php"
]
}
}

This is also very useful to build assets on the production system, e.g. with Assetic:

.. code-block:: json

{
"scripts": {
"compile": [
"app/console assetic:dump"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're talking about deploying apps into production, maybe we could add the --env=prod --no-debug options to this command?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prod env is specified by the SYMFONY_ENV env variable in a section above. --no-debug should by set implicitly when using the prod env.

]
}
}

.. sidebar:: Node.js Dependencies

Building assets may depend on node packages, e.g. ``uglifyjs`` or ``uglifycss``
for asset minification. Installing node packages during the deploy requires a node
installation. But currently, Heroku compiles your app using the PHP buildpack, which
is auto-detected by the presence of a ``composer.json`` file, and does not include a
node installation. Because the Node.js buildpack has a higher precedence than the PHP
buildpack (see `Heroku buildpacks`_), adding a ``package.json`` listing your node
dependencies makes Heroku opt for the Node.js buildpack instead:

.. code-block:: json

{
"name": "myApp",
"engines": {
"node": "0.12.x"
},
"dependencies": {
"uglifycss": "*",
"uglify-js": "*"
}
}

With the next deploy, Heroku compiles your app using the Node.js buildpack and
your npm packages become installed. On the other hand, your ``composer.json`` is
now ignored. To compile your app with both buildpacks, Node.js *and* PHP, you can
use a special `multiple buildpack`_. To override buildpack auto-detection, you
need to explicitly set the buildpack URL:

.. code-block:: bash

$ heroku buildpack:set https://github.com/ddollar/heroku-buildpack-multi.git

Next, add a ``.buildpacks`` file to your project, listing the buildpacks you need:

.. code-block:: text

https://github.com/heroku/heroku-buildpack-nodejs.git
https://github.com/heroku/heroku-buildpack-php.git

With the next deploy, you can benefit from both buildpacks. This setup also enables
your Heroku environment to make use of node based automatic build tools like
`Grunt`_ or `gulp`_.

.. _`the original article`: https://devcenter.heroku.com/articles/getting-started-with-symfony2
.. _`signup with Heroku`: https://signup.heroku.com/signup/dc
.. _`Heroku Toolbelt`: https://devcenter.heroku.com/articles/getting-started-with-php#local-workstation-setup
Expand All @@ -244,3 +323,9 @@ You should be seeing your Symfony application in your browser.
.. _`verified that the RSA key fingerprint is correct`: https://devcenter.heroku.com/articles/git-repository-ssh-fingerprints
.. _`post-install-commands`: https://getcomposer.org/doc/articles/scripts.md
.. _`config vars`: https://devcenter.heroku.com/articles/config-vars
.. _`custom compile steps`: https://devcenter.heroku.com/articles/php-support#custom-compile-step
.. _`custom Composer command`: https://getcomposer.org/doc/articles/scripts.md#writing-custom-commands
.. _`Heroku buildpacks`: https://devcenter.heroku.com/articles/buildpacks
.. _`multiple buildpack`: https://github.com/ddollar/heroku-buildpack-multi.git
.. _`Grunt`: http://gruntjs.com
.. _`gulp`: http://gulpjs.com