Skip to content

Instantly share code, notes, and snippets.

@jcandan
Created December 15, 2019 16:55
Show Gist options
  • Save jcandan/37903dfacb2a3f1c83f02feb6fdb0093 to your computer and use it in GitHub Desktop.
Save jcandan/37903dfacb2a3f1c83f02feb6fdb0093 to your computer and use it in GitHub Desktop.
Drupal 8.8+ .gitignore
#
# Copy and rename this file to .env at root of this project.
#
# A common use case is to supply database creds via the environment. Edit settings.php
# like so:
#
# $databases['default']['default'] = [
# 'database' => getenv('MYSQL_DATABASE'),
# 'driver' => 'mysql',
# 'host' => getenv('MYSQL_HOSTNAME'),
# 'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
# 'password' => getenv('MYSQL_PASSWORD'),
# 'port' => getenv('MYSQL_PORT'),
# 'prefix' => '',
# 'username' => getenv('MYSQL_USER'),
# ];
#
# Uncomment and populate as needed.
# MYSQL_DATABASE=
# MYSQL_HOSTNAME=
# MYSQL_PASSWORD=
# MYSQL_PORT=
# MYSQL_USER=
# Another common use case is to set Drush's --uri via environment.
# DRUSH_OPTIONS_URI=http://example.com
# Ignore core when managing all of a project's dependencies with Composer
# including Drupal core.
web/core
# Ignore dependencies that are managed with Composer.
# Generally you should only ignore the root vendor directory. It's important
# that core/assets/vendor and any other vendor directories within contrib or
# custom module, theme, etc., are not ignored unless you purposely do so.
/vendor/
/web/core/
/web/libraries/
/web/modules/contrib/
/web/profiles/contrib/
/web/themes/contrib/
/drush/Commands/contrib/
# Ignore configuration files that may contain sensitive information.
.env
/web/sites/*/settings*.php
/web/sites/*/services*.yml
/drush/sites/self.site.yml
*.sql
*.tgz
*.zip
# Uncomment to keep settings.php for cross-environment configuration.
# WARNING: Do not store sensitive information in this file.
#!/web/sites/defatul/settings.php
# Ignore paths that contain user-generated content.
/web/sites/*/files
/web/sites/*/private
/private/
# These really should be stored in a team member's global .gitignore.
# Ignore files generated by PhpStorm
.DS_Store
.idea
.vscode

Drupal 8.8 Recommended Composer Enhancements

Because, let's face it, Drupal 8.8 core missed some key components that made drupal-composer/drupal-project awesome.

Here, we tackle the .gitignore file and .env.

What to ignore

The Drupal 8.8+ composer-ready project templates nearly shipped with a .gitignore that would ignore composer-managed files (e.g. vendor/, web/modules/contrib). Although Composer documentation recommends that vendored files should not be committed, there are opposing schools of thought about whether vendored files should be a part of one's git repository. That .gitignore was deemed too opinionated for core. This ticket may try to address this shortcoming.

Here we provide an oppinionated .gitignore that follows Composer recommended model of ignoring vendored files.

Environment variables

We also missed out on the very useful dotenv file for environment specific variables.

  1. Copy the autoload property to composer.json.
  2. Uncomment the /web/sites/defatul/settings.php in .gitignore
  3. Follow the directions noted in .env.example
  4. Place load.environment.php in your project root
  5. Run the following:
    composer require vlucas/phpdotenv
    

Drush Policies

Drush is great. Let's grab it, and add policies which protect production from any unwanted drush site-alias database or file overwrites.

  1. Copy PolicyCommands.php to drush/Commands/
  2. Run the following:
    composer require drush/drush
    
...
"autoload": {
"files": ["load.environment.php"]
},
...
<?php
/**
* This file is included very early. See autoload.files in composer.json and
* https://getcomposer.org/doc/04-schema.md#files
*/
use Dotenv\Dotenv;
use Dotenv\Exception\InvalidPathException;
/**
* Load any .env file. See /.env.example.
*/
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
<?php
namespace Drush\Commands;
use Consolidation\AnnotatedCommand\CommandData;
/**
* Edit this file to reflect your organization's needs.
*/
class PolicyCommands extends DrushCommands {
/**
* Prevent catastrophic braino. Note that this file has to be local to the
* machine that initiates the sql:sync command.
*
* @hook validate sql:sync
*
* @throws \Exception
*/
public function sqlSyncValidate(CommandData $commandData) {
if ($commandData->input()->getArgument('target') == '@prod') {
throw new \Exception(dt('Per !file, you may never overwrite the production database.', ['!file' => __FILE__]));
}
}
/**
* Limit rsync operations to production site.
*
* @hook validate core:rsync
*
* @throws \Exception
*/
public function rsyncValidate(CommandData $commandData) {
if (preg_match("/^@prod/", $commandData->input()->getArgument('target'))) {
throw new \Exception(dt('Per !file, you may never rsync to the production site.', ['!file' => __FILE__]));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment