Drupal on Pantheon (Build a site with Composer)


In this article

Tools

You will need git, composer, php.  Use the links below to install or use UMD Virtual Workspace which has the necessary tools installed.  

Top

The build process on Pantheon is done with Composer, the build prioritizes the composer.json and composer.lock file in the root of the repo, and builds out based on those two files. 

After checking out a site repo from the Pantheon dashboard under the 'connection info' button, composer can be used to to install and uninstall modules via the command line from with the site repo directory, some useful commands are below:

Add a module

composer require drupal/admin_toolbar

Advanced example:

composer --ignore-platform-reqs -W require drupal/admin_toolbar:^3

Top

Remove a module

composer remove drupal/admin_toolbar

Top

Add libraries via composer

Adding libraries is possible via composer, this allows composer to download and install the library files, freeing you from having to manage them.  Adding libraries to the composer.json is a bit more complicated then adding a module, detailed documentation can be found on the Drupal.org page.

Top

Composer update

To update all modules in your composer file to get all new versions for updates and security releases, you can run the update command. You should also run the update command once all needed additions and removals have been done via composer, to ensure all your changes work correctly and have been picked up. The update command should be run via:

composer update

This command will run the composer.json file, and build out the composer.lock file, downloading and installing all specific modules and dependencies that are listed as needed for the site.  If you do not wish to install all the files locally, you can run composer update --no-install.  Any updated versions since you last ran this will also be grabbed.

Top

Commit and push

After installing and/or removing all modules with composer, and running composer update, you are now ready to commit your changes to the site repo and push them back to the Pantheon server to be build.  From your git repo on the command line, or from your git GUI, you should see two files that have been changed, composer.json and composer.lock (if you see other files being changed, please see the pitfalls section below). 

Once you push your changes, Pantheon will run the composer.json on their servers, and the changes will be deployed to the dev version of your site for review.

Updating the Test and Live environments

Once the changes have been pushed to the main branch and built on the Pantheon servers, they will appear on within the dev site environment.  The dev site should be then tested to make sure the changes work as expected, and everything is still correct with the site.  Once this testing is completed, the changes need to be pushed to the test environment, and finally to the live environment so that the public facing site receives the updates.  To deploy the updates, click on the 'Test' tab on the site Pantheon dashboard, the first pane there should be the deploy section.  You will see a list of changes that will be deployed, and a box for deploy messages.  Write a message as desired, and click the 'deploy code' button.  Once the code has been deployed on the test instance, repeat the same process for the live environment via the "Live" tab.

Top

Quick version

Top

Potential pitfalls and ways to avoid

The Pantheon build process can balk anywhere it encounters files that it 'expects' to be from Composer being committed in the git repo itself.  Changing and committing files in directories where Composer puts files, especially force commits with git -f force, can cause problems.  It is best to avoid using git -f force as a general rule.

If you have custom code for themes or modules, they should be added to the directories below:

/web/sites/default/modules/custom
/web/sites/default/themes/custom
/web/sites/default/libraries

Pantheon also does not change its build restrictions based on changes to the .gitignore file.  Removing lines from the .gitignore and committing files in those now tracked directories can still lead to the Pantheon build process issues. We recommand avoiding making changes to the .gitignore file.

Top

Resources

Top