Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Best practices with composer and git for Drupal...

Best practices with composer and git for Drupal 7 and Drupal 8

https://2018.drupalcamp.pl/en/sessions/best-practices-composer-and-git-drupal-7-and-drupal-8/

In this session, I'll be presenting some of the best practices in building Drupal 7 and 8 websites using composer and git.

If you're still committing the complete Drupal source code into your repo, or are still stuck in 'drush make' it's time to learn how to build your website in a maintainable way. Some of us are already using composer in our Drupal 8 sites, but we still use drush make for our Drupal 7 sites. In this session I will show how you can easily create composer.json files for your Drupal projects. For those using a make file, I will show you how to easily convert it into composer.

This session will also introduce the best practices in deploying Drupal sites, in git management of branches and pull requests, and also how to use git to simplify your community contributions.

Topics covered
* Semantic versioning
* Drupal build systems
** Drush make
** Composer for Drupal 7
** Composer for Drupal 8
* Leveraging git
** Git branches and tags
** .gitignore file
** Git diff, apply, revert
** Creating a drupal.org patch + interdiff with git
** Pull requests and peer review

https://www.youtube.com/watch?v=OABt4C4Brjk&ab_channel=DrupalCampPoland

João Ventura

June 09, 2018
Tweet

More Decks by João Ventura

Other Decks in Programming

Transcript

  1. Drush make is better than nothing. Composer is the best.

    Summary • Drupal build systems • Drush make • Composer • git
  2. Drush make (http://docs.drush.org/en/7.x/make/) Part of drush versions 5 to 8

    Two flavours: ini or yaml (!) Site built with exact versions of: • Core • Projects (modules, themes) • Libraries Support for just-in-time patching. Support for git, svn, bzr, copy, etc downloads. drush make-generate project.make
  3. ini format core = 7.x api = 2 defaults[projects][subdir] =

    "contrib" projects[drupal][version] = "7.59" projects[admin_menu][version] = "3.0-rc5" projects[views][version] = "3.20" projects[views][patch][1986306] = "https://www.drupal.org/files/issues/illegal_choice_0_in-1986306-36.patch" projects[zen][version] = "6.4"
  4. yaml format core: 7.x api: 2 defaults: projects: subdir: 'contrib'

    projects: drupal: version: '7.59' admin_menu: version: 3.0-rc5 views: version: '3.20' patch: - 'https://www.drupal.org/files/issues/illegal_choice_0_in-1986306-36.patch' zen: version: '6.4'
  5. Composer (https://getcomposer.org) Dependency manager for PHP Works well with semantic

    versioning Package repositories: • https://packagist.org/ - default •https://packages.drupal.org/8 - Drupal 8 •https://packages.drupal.org/7 - Drupal 7
  6. Semantic versioning (https://semver.org) x.y.z • x - major version •

    lots of new features • not backwards compatible • y - minor version • some new features • backwards compatible • z - patch version • bugfixes only Drupal core: 8.5.3
  7. Composer versions • Exact: 1.2.3 • Range i: >=1.0 <1.1

    || >=1.2 • Range ii: 1.0 - 2.0 (i.e. >=1.0.0 <2.1) • Wildcard: 1.0.* (i.e. >=1.0 <1.1) • Tilde: • ~1.2 (i.e. >=1.2 <2.0.0) • ~1.2.3 (i.e. >=1.2.3 <1.3.0) • Caret: • ^1.2 (i.e. >=1.2 <2.0.0) • ^1.2.3 (i.e. >=1.2.3 <2.0.0)
  8. Workflow • First time, or major update: composer update •

    During deployment: composer install • Add new modules (do not manually edit composer.json) composer require drupal/project “^1.0” --with-dependencies • Finally commit BOTH composer.json and composer.lock
  9. drupal-project (https://github.com/drupal-composer/drupal-project) Composer template for D7 or D8 sites Provides:

    •Drupal packages.drupal.org •Directory installers (web/modules/contrib, etc.) •Patches •Drush, etc. •Some scaffold
  10. drupal-project for Drupal 7 Location of Drupal 7 packages Drupal

    7 PHP extensions requirements Composer patches library + patch to preserve-paths Composer preserve paths library (to preserve sites/all/*) Correct installer paths configuration Correct preserve-paths configuration
  11. Create a project Drupal 8: composer create-project drupal-composer/drupal-project:8.x-dev some- dir

    --stability dev —no-interaction Drupal 7: composer create-project drupal-composer/drupal-project:7.x-dev some- dir --stability dev —no-interaction Word of caution: "config": { "platform": {"php": "5.5.9"} },
  12. composer format "repositories": [ { "type": "composer", "url": "https://packages.drupal.org/7" }

    ], "require": { "composer/installers": "^1.2", "cweagans/composer-patches": "^1.6", "drupal-composer/preserve-paths": "^0.1", "drupal/admin_menu": "^3.0-rc5", "drupal/drupal": "^7.59", "drupal/views": "^3.20", "drupal/zen": "^6.4", "drush/drush": "~8.0" },
  13. Convert a makefile to composer drush make-convert makefile —format=composer >

    composer.json Does not convert libraries. See the README.md of drupal-project/ 7.x on how to configure. Some post-convert enhancements (from d-p/7.x composer.json): • Add scaffolding (autoload and scripts section from) + composer/ semver, drupal/composer_autoloader, symfony/filesystem and webflo/drupal-finder requires. • Add the patch to cweagans/composer-patches
  14. git workflow • master/prod: live site • develop/stage: staging site

    • Pull requests / merge to master, on release • feature/ticket#-desc: ticket branches • checkout -b from stage • Pull requests / merge after peer review https://nvie.com/posts/a-successful-git-branching-model/
  15. git + composer • DO • configure .gitignore • commit

    composer.json AND composer.lock • use semantic versions • use composer as a development tool • DON’T • run “composer update” naked, unless you plan to test everything that is updated • commit /vendor nor any 3rd party code (from drupal.org or libraries) • use composer as a deployment tool
  16. git workflow • Create feature branch: git checkout -b feature/TKT-42-answer-life

    • Do the work • Rebase to latest version of parent branch git rebase branch • Export config drush cex • Check changes git diff • Add files and commit git add file1 … filen ; git commit -m “TKT-42: 6 times 9 in base 13” • Create pull request in github/gitlab/bitbucket • Merge the feature branch
  17. .gitignore # Ignore directories generated by Composer /vendor/ /web/sites/all/drush/ /web/sites/all/modules/contrib/

    /web/sites/all/themes/contrib/ /web/sites/all/libraries/ # Ignore Drupal 7 core files. /web/* !/web/*/ /web/includes/ # Ignore configuration files that may contain sensitive information. /web/sites/*/*.settings.php /web/sites/*/settings*.php # Ignore paths that contain user-generated content. /web/sites/*/files /web/sites/*/private
  18. Let’s make it better • Add composer.json to Drupal 7

    core. • drush composer-generate composer.json.
  19. basic git commands i • git init: Create an empty

    Git repository • git clone user@host:project.git : Clone a repository into a new directory • git add file: Add files to the index • git commit -m “Message”: Record changes to the repository • git tag tag: Create, list, delete or verify a tag • git push: Update remote refs • git checkout branch: Switch branches • git branch : List, create, or delete branches • git pull: Fetch from another repository • git rebase branch: Reapply commits on top of branch • git merge branch: Join two or more branches
  20. basic git commands ii • git log: Show commit logs

    • git show commit: Shows the log message and textual diff • git diff: Show changes between commits, commit and working tree, etc • git blame file: Show what revision and author last modified each line of a file • git revert commit: Revert some existing commits • git status: Show the working tree status • git rm file: Remove files from the working tree and from the index
  21. Drush Issue Queue commands (https://www.drupal.org/project/drush_iq) Install: drush pm-download grn drush_iq;

    cd ~/.drush/drush_iq; composer install Create branch for issue: drush iqa issue# Create interdiff: git diff > interdiff.txt Create complete patch: drush iqd > [project]-[short-description]-[issue#]-[comment#].patch