Giter Site home page Giter Site logo

grunt-wordpress-deploy's Introduction

Grunt Wordpress Deployments

The project is searching for a new maintainer. If anybody is interested please let us know (@ingo86 or @davidthou). Thanks.

Instructions

Deploy a Wordpress instance without pain using Grunt.

This plugin leverages on Grunt to push and pull a Wordpress instance into the predefined locations. Here's a tour of the features:

  • Multiple environments support: you can define different environments such as development, staging, production and so on, with different access credentials, paths and domains.
  • Adapt the Wordpress database to the destination domain: It replaces all the instances of the source environment domain with the destination environment domain, even into serialized data.
  • Push and pull files with rsync.
  • Completely based on Javascript, leverages only on some common system tools to perform the tasks (mysql, mysqldump, ssh).

Requirements

This plugin requires:

  • Grunt ~0.4.1
  • ssh
  • rsync
  • mysqldump

To be able to use this plugin it's important to have access to the remote machine through ssh, with ssh key authentication to avoid password entering during the tasks. As this is a different topic we will not cover it here but you might like to start by reading Github's own advice.

Getting started

This is a Grunt plugin, so it requires Grunt. It's really easy to install, as explained into the Getting Started guide. Please read the guide to understand how does this works.

When Grunt is installed on your machine, you can install this plugin with the following command:

npm install grunt-wordpress-deploy --save-dev

Once the plugin has been installed, it may be enabled and configured into your Gruntfile.js. Please follow the example Gruntfile to configure your environments.

module.exports = function(grunt) {
  "use strict";

  grunt.initConfig({
    wordpressdeploy: {
      options: {
        backup_dir: "backups/",
        rsync_args: ['--verbose', '--progress', '-rlpt', '--compress', '--omit-dir-times', '--delete'],
        exclusions: ['Gruntfile.js', '.git/', 'tmp/*', 'backups/', 'wp-config.php', 'composer.json', 'composer.lock', 'README.md', '.gitignore', 'package.json', 'node_modules']
      },
      local: {
        "title": "local",
        "database": "database_name",
        "user": "database_username",
        "pass": "database_password",
        "host": "database_host",
        "url": "http://local_url",
        "path": "/local_path"
      },
      staging: {
        "title": "staging",
        "database": "database_name",
        "user": "database_username",
        "pass": "database_password",
        "host": "database_host",
        "url": "http://staging_url",
        "path": "/staging_path",
        "ssh_host": "user@staging_host"
      },
      your_environment: {
        ...
      }
    },
  });

  // Load tasks
  grunt.loadNpmTasks('grunt-wordpress-deploy');

  // Register tasks
  grunt.registerTask('default', [
    'wordpressdeploy'
  ]);
};

In the example above we define two environments, one is mandatory and is always called local, another is optional and can be called the way you want. In this case we have defined a second environment called staging.

Available tasks

The plugin defines a serie of tasks. Here's a brief overview:

  • grunt push_db --target=environment_name: Push the local database to the specified environment.
  • grunt pull_db --target=environment_name: Pull the database on the specified environment into the local environment.
  • grunt push_files --target=environment_name: Push the local files to the specified environment, using rsync.
  • grunt pull_files --target=environment_name: Pull the files from the specified environment to the local environment, using rsync.

Push_db

Example execution: grunt push_db --target=staging

The push_db command moves your local database to a remote database location, specified by the target environment. What happens under the hood is the following:

  • Dump the local database
  • Adapt the local dump to the remote environment executing a search and replace to change the instances of the local domain with the instances of the remote domain, taking care of serialized data
  • Backups the database on the target environment
  • Imports the local adapted dump into the remote database

Pull_db

Example execution: grunt pull_db --target=staging

The pull_db command moves your target environment database to the local database. What happens under the hood is the following:

  • Dump the remote database
  • Adapt the remote dump to the local environment executing a search and replace to change the instances of the remote domain with the instances of the local domain, taking care of serialized data
  • Backups the database on the local environment
  • Imports the remote adapted dump into the local database

Push_files

Example execution: grunt push_files --target=staging

The push_files command moves your local environment files to the target environment using rsync.

This operation is not reversible.

Into Gruntfile.js is possible to set which options rsync will use, and which files should be exluded from the synchronization. More details in the configuration section below.

  grunt.initConfig({
    wordpressdeploy: {
      options: {
        backup_dir: "backups/",
        rsync_args: ['--verbose', '--progress', '-rlpt', '--compress', '--omit-dir-times', '--delete'],
        exclusions: ['Gruntfile.js', '.git/', 'tmp/*', 'backups/', 'wp-config.php', 'composer.json', 'composer.lock', 'README.md', '.gitignore', 'package.json', 'node_modules']
      },
      local: {
        ...

Pull_files

Example execution: grunt pull_files --target=staging

The pull_files command moves your target environment files to the local environment using rsync.

This operation is not reversible.

Into Gruntfile.js is possible to set which options rsync will use, and which files should be exluded from the synchronization.

Configuration

Each target expects a series of configuration options to be provided to enable the task to function correctly. These are detailed below:

title

Type: String

Description: A proper case name for the target. Used to describe the target to humans in console output whilst the task is running.

database

Type: String

Description: the name of the database for this target.

user

Type: String

Description: the database user with permissions to access and modify the database

pass

Type: String

Description: the password for the database user (above)

host

Type: String

Description: the hostname for the location in which the database resides.

url

Type: String

Description: the string to search and replace within the database before it is moved to the target location. This is designed for use with the awful Wordpress implementation which stores the site url into the database and is required to be updated upon migration to a new environment.

path

Type: String

Description: the path of the the installation files on the filesystem. Used by rsync to update the correct folder on synchronization.

ssh_host

Type: String

Description: ssh connection string in the format SSH_USER@SSH_HOST. The task assumes you have ssh keys setup which allow you to remote into your server without requiring the input of a password.

Options

options.backups_dir

Type: String

Default value: backups

A string value that represents the directory path (relative to your Grunt file) to which you want your database backups for source and target to be saved prior to modifications.

You may wish to have your backups reside outside the current working directory of your Gruntfile. In which case simply provide the relative path eg: ../../backups.

options.rsync_args

Type: Array

Default value: ['--verbose', '--progress', '-rlpt', '--compress', '--omit-dir-times', '--delete']

An array representing all parameters passed to the rsync command in order to perform the synchronization operation. The defult value in this example is fine for common usages of this plugin.

options.exclusions

Type: Array

Default value: ['Gruntfile.js', '.git/', 'tmp/*', 'backups/', 'wp-config.php', 'composer.json', 'composer.lock', 'README.md', '.gitignore', 'package.json', 'node_modules']

An array representing all excluded files and directories from the synchronization process.

History

This plugin is an almost complete rewrite of the Grunt-Deployments Plugin. Credits to the original developer for the work on the original plugin.

grunt-wordpress-deploy's People

Contributors

darioghilardi avatar davidthou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-wordpress-deploy's Issues

Database Synch Not Search/Replacing or Serializing Data

I have my local and development environments setup. The files are successfully pushed to the development server and the local database is also pushed to the development server, but the values of the database are not changed (i.e. it doesn't seem like the search and replace nor the serializing data is happening on the database before it gets pushed to the development server). Any thoughts? I am posting my Gruntfile.js code below, but have changed some of the values for security purposes.

wordpressdeploy: {
      options: {
        backup_dir: "backups/",
        rsync_args: ['--verbose', '--progress', '-rlpt', '--compress', '--omit-dir-times', '--delete'],
        exclusions: ['Gruntfile.js', '.git/', 'tmp/*', 'backups/', 'wp-config.php', 'composer.json', 'composer.lock', 'README.md', '.gitignore', 'package.json', 'node_modules', '*.sublime-workspace', '*.sublime-project']
      },
      local: {
        "title": "local",
        "database": "snoringdoc",
        "user": "root",
        "pass": "root",
        "host": "localhost",
        "url": "http://localhost/Snoring-Doc/",
        "path": "/Applications/MAMP/htdocs/Snoring-Doc/"
      },
      dev: {
        "title": "dev",
        "database": "dev_snoringdoc",
        "user": "dev_admin",
        "pass": "notactualpassword",
        "host": "localhost",
        "url": "http://snoringdoc.dev.com",
        "path": "/home/dev/public_html/snoringdoc",
        "ssh_host": "[email protected]"
      }
    },

Error 1064 (SQL syntax) when pulling DB

grunt pull_db --target="staging"

Pulling database from 'staging' into Local
>> Creating DUMP of remote database
Password:
>> Database DUMP succesfully exported to:
>> backups/staging/20150625/16-16-06/db_backup.sql
>> Adapt the database: set the correct urls for the destination in the database.
>> Creating DUMP of local database
>> Database DUMP succesfully exported to:
>> backups/local/20150625/16-16-06/db_backup.sql
>> Importing DUMP into local database
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bash: LW9@@T^x: command not found
Usage: mysqldump [OPTIONS] database [tables]
O' at line 1
>> Database imported succesfully

Operations completed

Done, without errors.

.sql file says:

bash: LW9@@T^x: command not found
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help

utf8mb4_unicode_ci

With the WordPress 4.2, the new table collation is utf8mb4_unicode_ci. However that generates an error using grunt-wordpress-deploy:

ERROR 1273 (HY000) at line 25: Unknown collation: 'utf8mb4_unicode_ci'

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Hey,
I am using MAMP Pro on Yosemite. I have moved the document root folder to Sites.
I am receiving the following error when running the task: grunt pull_db --target=staging

Anyone have any ideas on how to resolve this?

Thanks in advance.

Pulling database from 'staging' into Local
>> Creating DUMP of remote database
>> Database DUMP succesfully exported to:
>> backups/staging/20141205/21-29-25/db_backup.sql
>> Adapt the database: set the correct urls for the destination in the database.
>> Creating DUMP of local database
>> Database DUMP succesfully exported to:
>> backups/local/20141205/21-29-25/db_backup.sql
>> Importing DUMP into local database
Warning: Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
>> Database imported succesfully

Operations completed

pull_db not working properly

running pull_db --target=staging returns:

mysqldump: Got error: 1045: "Access denied for user 'wpdeploy'@'ip.of.our.server' (using password: YES)" when trying to connect

staging: {
"title": "staging",
"database": "MWPdb",
"user": "wpdeploy",
"pass": "somepassword",
"host": "our.server.com",
"url": "http://our.server.com",
"path": "/opt/sites/www/wordpress",
"ssh_host": "[email protected]"
}

But when I run the command:
mysqldump -h our.server.com -u wpdeploy -p MWPdb

It just downloads the SQL. (ofc I entered the password first)

Write to wp-config.php

Just thinking out loud: it might be nice if, instead of ignoring wp-config.php, this tool actually wrote to wp-config.php, updating it with the provided environment's database credentials and URLs (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, WP_HOME) during a file deploy. Since all that information is already present in-the-clear in the Gruntfile.js, why not provide a way to provide that information to the config file during a deployment?

Running tasks

The example gruntfile in the readme runs "wordpressdeploy" which results in:
Warning: Task "wordpressdeploy" not found. Use --force to continue.

I can run the built-in tasks like "grunt push_files --target=staging", but how should they be incorporated into the default task? Maybe a noob question :)

Thanks!

Add 'table_prefix' config var [enhancement]

Hey thanks for releasing this module! I've been looking around a bunch for a better WP deployment flow and this is really helpful.

It'd be great if you could also add a 'table_prefix' var that could dump only tables with that prefix. On my local machine (and sometimes on production, specifically with crappy shared hosts) I'll run a few wp installs and I only want to dump/pull down one of those. I'm new to grunt so I can try my hand at it and submit a PR if I get anywhere on it myself...

Escaping spaces

The script seems to fail in the execution of mysql/mysqldump shell command execution if the password contains spaces.

Warning message in SQL dump

Hi,

I found Warning: Using a password on the command line interface can be insecure.
was appearing on line 1 of my staging sql dump file. This was causing SQL syntax error when trying to import locally.

I didn't use SSH host option to connect to the server not sure if this has something to do with the error message?

I found the same problem referenced here: http://stackoverflow.com/questions/20751352/suppress-warning-messages-using-mysql-from-within-terminal-but-password-written which suggests to use mysql_config_editor http://dev.mysql.com/doc/refman/5.6/en/mysql-config-editor.html.

As a temporary fix I removed the warning message by adding:

var preoutput = exports.replace_urls(old_url, new_url, content);
var output = exports.replace_urls("Warning: Using a password on the command line interface can be insecure.", " ", preoutput);

In util.js https://github.com/webrain/grunt-wordpress-deploy/blob/master/tasks/lib/util.js#L89

Am I authenticating wrong which is causing the warning to appear? Authenticating using the ssh_host option seemed to not allow the sqldump to generate due to -jailshell: FkX: command not found errors appearing when trying to dump on the live server.

Thanks!

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax

When I run the task I get the following error:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bash: -c: line 0: syntax error near unexpected token `)'
bash: -c: line 0: `mysq' at line 1

Not sure if this is due to my system configuration or the plugin.

The error appears in the the staging .sql dumps but the grunt task says everything completes successfully. So it fails silently unless you add the flags --verbose --debug when calling the command.

The local db dump file shows correct SQL information.

Unknown issue with url replacement (solved in pull request)

I have tried to push two different wordpress installations and both have had issues, either losing their widgets, random data or simply not working at all. I think it has something to do with the url replacement. If I leave my staging url identical to my local url and use http://github.com/interconnectit/search-replace-db after pushing the db, it works perfectly.

The difference I see is that "search-replace-db" uses the php @unserialize and then re-serializes which are both built in php functions. The "replace_urls_in_serialized" of course can't leverage these methods but there might be some issue with the regular expression being used.

Wordpress multisite support

Pardon me for asking, because this might be a stupid question, but:

Is there a way to target only a subsite (on a wordpress multisite install) with grunt-wordpress-deploy? We're working with 100+ live sites on the production environment and would like to prevent accidental overwrites.

Also it would be faster to just push and pull only the targeted subsite. Would love to hear from you, and thanks for a wonderful plugin!

db push not working

I run the taks grunt push_db --target=staging
this is the result:

Pushing database from 'Local' to 'staging'
>> Creating DUMP of local database
>> Database DUMP succesfully exported to:
>> backups/local/20141210/02-58-28/db_backup.sql
>> Adapt the database: set the correct urls for the destination in the database.
>> Creating DUMP of remote database
>> Database DUMP succesfully exported to:
>> backups/staging/20141210/02-58-28/db_backup.sql
>> Importing DUMP into remote database
>> Database imported succesfully

Seems ok but when i enter the staging url the database is not updated. I thought the task upload the new db into my server.

am i wrong?

Recommendation for improvement: Target as task argument

Allow the target to be specified as a task argument like "pull_db:prod", in addition to allowing it as an option or command-line argument (--target=).

Doing so allows multiple step tasks like:

grunt.registerTask('createtest', ['db_pull:prod", 'db_push:staging"]);

Add support for Vagrant VM

Could this be as simple as adding the ssh_host option for local environment?

Currently I'd have to manually ssh into vagrant, cd into theme directory where my gruntfile.js is located and run commands to push & pull database.

Move sensible data outside of the Gruntfile.

Sensible data, like database username and password, should be written in a different file than the Gruntfile. This way you can add to version control the Gruntfile without any privacy problem.

pull_files goes in wrong directory when target path is missing trailing slash

I've been trying to pull files from an environment to my local, and the files were not being added to the path that I've set in the local "path":.
I've tried debugging it and figured out that if the target path is missing a trailing slash, it creates another folder in the local path directory, named after the target path's basename folder. Here's what I mean:

target path: "path": "/var/www/target_directory"
local path: "path": "/var/www/local_directory"
would add files in /var/www/local_directory/target_directory

Hope that makes sense. It wasn't mentioned in the docs, and the example is also missing the trailing slash.

Add the ability to alias tasks

I'd like to be able to type "grunt deploy" and have it run my default grunt tasks (concat, minify, etc) then run push_db and push_files

Also would like the opposite when I type "grunt pull" or something similar.

Right now there's no way (that I've found) to register a task and still add the "target" parameter.

Error importing DB due to passwords

This was a problem with grunt-deployments, so was probably not fixed at the time you forked that project.

In at least some versions of MySQL, if you put the DB password on the command line with the -p argument, the first line of the resulting .sql file will be:
"Warning: Using a password on the command line interface can be insecure."

When you then try to load this into another DB, it fails because of invalid SQ syntaxL.

A solution is to change the templates as follows:

mysqldump: "MYSQL_PWD=<%= pass %> mysqldump -h <%= host %> -u<%= user %> <%= database %>",
mysql: "MYSQL_PWD=<%= pass %> mysql -h <%= host %> -u <%= user %> <%= database %>",

Precise ssh port

I changed my ssh port for security reasons. Can i specify a custom port ?

MariaDB

I'm getting the following error when trying to push DB to MariaDB on a production server.

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '/bin/sh: mysqldump: command not found' at line 1

Does it work with MariaDB?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.