deploy/deploy-revision

Deploy new code revision by performing operations from YAML playbooks.

dev-master 2017-02-11 11:07 UTC

This package is auto-updated.

Last update: 2024-04-10 20:37:01 UTC


README

Create a YAML playbook, define a code revision, specify upgrade path as commands for performing and distribute them between environments.

Build Status Code coverage Quality Score Coding standards Total Downloads Latest Stable Version License

Installation

composer require deploy/deploy-revision:1.*

Usage

A view of deployment playbook.

commands:
  # Commands below will be executed only if environment ID will match.
  lush_website_at:
    140:
      - "drush updb"
  # It's predefined namespace for commands which should be run everywhere.
  global:
    89:
      - "drush cc all"
    121:
      - "drush cc drush"
      - "print bla bla"

# The order of commands for execution will looks like (only in case if current code version is lower than defined):
# - For "lush_website_at" environment:
#   - drush cc all - will be removed because we have "drush updb" (if logic like in "filter()" below will be implemented).
#   - drush cc drush
#   - print bla bla
#   - drush updb
#
# - For "global" environment:
#   - drush cc all
#   - drush cc drush
#   - print bla bla

Initialize the library at start.

require_once 'vendor/autoload.php';

use DeployRevision\DeployRevision;

$deploy = new DeployRevision();

Use own YAML parser (if don't like Symfony).

class SpycYaml implements YamlInterface
{
    /**
     * {@inheritdoc}
     */
    public function isAvailable()
    {
        return class_exists('Spyc');
    }
    
    /**
     * {@inheritdoc}
     */
    public function parse($content)
    {
        return \Spyc::YAMLLoadString($content);
    }

    /**
     * {@inheritdoc}
     */
    public function dump(array $content)
    {
        return \Spyc::YAMLDump($content);
    }
}

$deploy
    ->getContainer()
    ->getDefinition('deploy_revision.yaml')
    ->setClass(SpycYaml::class);

Look for *.yml playbooks inside a directory and for tasks in particular file.

$deployment = $deploy->getWorker();
// Read particular playbook.
$deployment->read('../lush_deploy.yml');
// Read playbooks within directory.
$deployment->read('../lush_deploy');

Set an environment ID and/or path to file where revision ID should be stored (or duplicated, from DB for instance).

$deployment = $deploy->getWorker('lush_website_at', 'private://revisions/revision');

Filter commands. Callback should return the command for deletion.

$deployment->filter(function ($command, array $commands, callable $resolver) {
    // Remove "drush cc css-js" since "drush updb" will do the job for it.
    if ('drush cc css-js' === $command && isset($commands['drush updb'])) {
        return $command; 
    }

    // Remove all previously added "drush cc all" from the list if "drush updb" exists.
    return $resolver(true, ['drush updb'], ['drush cc all'])
        // Remove newly added "drush cc all" if "drush updb" in the list.
        ?: $resolver(false, ['drush cc all'], ['drush updb']); 
});

Run deployment.

$deployment->deploy(function ($command) {
    $arguments = explode(' ', $command);
    $program = array_shift($arguments);

    switch ($program) {
        case 'drush':
            drush_invoke($program, $arguments);
            break;

        default:
            printf('No handler found for the "%s" command.', $command);
  }
});

Save new revision ID.

$deployment->commit();

Notes

  • All tasks from playbooks must be handled inside of deployment callback. This means that an implementation for recognizing the commands and for executing them should be done. Otherwise you'll not have any effect placing commands inside playbooks.

  • Tasks collected in order you running the ->read() method. If you are reading test1.yml and test2.yml and both files have the same revision number inside, then commands from first file will be located above ones from second.

    Reading of the directory will be done in alphabetical order. If multiple playbooks have the same revision numbers, then the only way you can affect on ordering - is to set file names in correct order.

Testing

Run PHPUnit tests locally.

./bin/phpunit --coverage-text

Scrutinizer CI

Used for generating tests coverage.

Travis CI

Used for running tests on various PHP versions.

Style CI

Used for verifying coding standards. Actually Scrutinizer doing this as well.