sokil/deploy-bundle

Symfony application deploy bundle

Installs: 92

Dependents: 0

Suggesters: 4

Security: 0

Stars: 3

Watchers: 2

Forks: 1

Open Issues: 24

Type:symfony-bundle

0.8.0 2017-06-24 13:47 UTC

This package is auto-updated.

Last update: 2024-10-28 01:47:02 UTC


README

Task runner for Symfony project

Latest Stable Version Total Downloads Build Status Coverage Status

Installation

Add Composer dependency:

composer.phar require sokil/deploy-bundle

Add bundle to your AppKernel:

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Sokil\DeployBundle\DeployBundle(),
        );
    }
}

Configuration

Configure tasks required to run in your app in app/config/config.yml:

deploy:
  config:
    git: {}
    composer: {}
    npm: {}
    bower: {}
    grunt: {}
    asseticDump: {}
    assetsInstall: {}
  tasks:
    updateBack: [git, composer]
    updateFront: [npm, bower]
    compileAssets: [grunt, asseticDump, assetsInstall]
    release: [updateBack, updateFront, compileAssets]

Section config declared options of every task, able to run. Section tasks declares bundles of tasks, which run sequentially. Tasks may be run by defining task aliases in cli command:

$ ./app/console deploy --git --npm

Also tasks bundles may be defined:

$ ./app/console deploy --compileAssets

If no task specified then default task bundle will be run. This task bundle may be defined in configuration, but if it omitted, then default task consists of all tasks in order of config section. Tasks and task bundles both may be specified in cli options, then tasks will be run in order of first occurrence. Task bundle also may contain other bundles.

To get list of all configured tasks run:

$ ./bin/console deploy --env=prod -h
Usage:
  deploy [options]

Options:
      --composer                   Update composer dependencies
      --composer-update            Update dependencies instead of install it
      --migrate                    Migrate datbase
      --npm                        Updating npm dependencies
      --bower                      Updating bower dependencies
      --grunt                      Run grunt tasks in bundles
      --grunt-tasks[=GRUNT-TASKS]  List of bundles with specified grunt tasks, e.g. "bundle1Name:task1Name,task2Name;bundle2Name;"
      --asseticDump                Dump assetic assets
      --assetsInstall              Install bundle assets
      --clearCache                 Clear cache
      --updateFront                Task bundle for tasks "npm","bower"
      --compileAssets              Task bundle for tasks "grunt","asseticDump","assetsInstall","clearCache"
      --release                    Task bundle for tasks "composer","migrate","updateFront","compileAssets"
      --default                    Task bundle for tasks "composer","migrate","npm","bower","grunt","asseticDump","assetsInstall","clearCache"

Tasks

Git

Configuring git task

Add configuration to your ./app/config/config.yml:

deploy:
  config:
    git:
        defaultRemote: origin       # Optional. Default: origin. Set default remote for all repos
        defaultBranch: master       # Optional. Default: master. Set default branch for all repos
        repos:                      # List of repos
          core:                     # Alias of repo
            path: /var/www/project  # Path to repo
            remote: origin          # Optional. Default: origin. Set remote for this repo
            branch: master          # Optional. Default: master. Set branch for this repo
            tag: false              # Tag release after pull

Private repositories

If repository is private, password will be asked on pull:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

For example web server started under www-data user. To prevent asking password, add ssh keys to /home/$USER/.ssh directory, using ssh key generation tool.

  1. Generate keys:
$ sudo -u www-data ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/www-data/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/www-data/.ssh/id_rsa.
Your public key has been saved in /home/www-data/.ssh/id_rsa.pub.
The key fingerprint is:
...
  1. Add public key to your repository to fetch changes without password prompt.

  2. Test your connection:

$ sudo -H -u www-data git pull origin master

Find out who use this key already:

ssh -T git@github.com
ssh -T git@bitbucket.com

Webpack

You can optionally specify path to webpack in pathToWebpack parameter. If omitted , then webpack will be used. In this case webpack must be installed globally.

deploy:
  config:
    webpack:
      pathToWebpack: assets/node_modules/.bin/webpack # (Optional) Path to webpack
      projects: # list of webpack projects with own webpack.config.js inside
        assets:
          config: "assets/webpack.config.js" # (required) path to config. Context will be set to dirname of config.
          progress: true # (optional) Show build progress
          p: true # (optional) Build for production. For "prod" environment defined automatically
          ...

This will run command, where context value is dirname of config in parameters config:

assets/node_modules/.bin/webpack --config assets/webpack.config.js -p --progress --context assets

Npm

deploy:
  config:
    npm:
      dirs: # Optional list of dirs to search package.json and install dependencies
        - "assets"
      bundles: # Optional list of bundles where to search package.json
        SomeBundle: true
        SomeOtherBundle:
           package: ../ # path to project.json, relative to SomeOtherBundle.php file

Bower

deploy:
  config:
    bower:
      bundles:
        SomeBundle: true
        SomeOtherBundle: true

Grunt

Add task configuration to your deploy config:

deploy:
  config:
    grunt:
      bundles: # bundles where grunt need to be run
        SomeBundle1: true
        SomeBundle2:
          tasks: [task1, task2, task3]
          gruntfile: ../ # dir with Gruntfile.js, relatively to SomeBundle2.php
            
      parallel: true # run grunts from all bundles in parallel. Default: false

Grunt may have few tasks, and you can select which tasks to run.

In config:

deploy:
  config:
    grunt:
      bundles:
        SomeBundle:
          tasks:
            - newer:less 
            - newer:jade
        SomeOtherBundle: true
      parallel: true

As CLI parameter:

./app/console deploy --grunt --grunt-tasks="SomeBundle=newer:less,newer:jade&SomeOtherBundle"

Migrations

Add dependency:

composer.phar require doctrine/migrations
composer.phar require doctrine/doctrine-migrations-bundle

Register bundler in AppKerner:

new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),

First, configure migrations in ./app/config/config.yml:

doctrine_migrations:
    dir_name: %kernel.root_dir%/migrations
    namespace: Migrations
    table_name: migrations
    name: Application Migrations

Then add task to deploy config in ./app/config/config.yml:

deploy:
  config:
    migrate: {}

Install Assets

Then add task to deploy config in ./app/config/config.yml:

deploy:
  config:
    assetsInstall: {}

Dump Assetic

Then add task to deploy config in ./app/config/config.yml:

deploy:
  config:
    asseticDump: {}

Bundles with Assetic assets must be defined in ./app/config/config.yml:

assetic:
  bundles:
    - AcmeBundle

Composer

This task will run composer command:

composer.phar install --optimize-autoloader --no-interaction

File composer.phar must be in path, or you can redefine path to composer by param path.

Add task config to ./app/config/config.yml:

deploy:
  config:
    composer:
      scripts: true # set true if you want to execute scripts. Default: true
      update: true # Optional. If passed, update dependencied instead on install
      path: composer.phar # Optiona. Default: composer.phar, specify path to composer

Clear cache

deploy:
  config:
    clearCache: {}

Sync

Task userd to sync data to production servers. Add configuration:

deploy:
  config:
    parallel: 3
    rules:
      web:
        src: '.'
        dest:
          - user@www1.server.com://var/www/some
          - user@www2.server.com://var/www/some
        delete: true
        verbose: true
        exclude:
          - /var
          - /app/conf/nginx/
          - /.idea
          - /app/config/parameters.yml
        include:
          - /app/conf/nginx/*.conf.sample

Parameter rules define rules to sync files from source to destination. May be configured any number of rules. Every rule consists of parameter src (defaults to "."), parameter dest which may be string or array of hosts. Other rule parameters same to rsync options. See man page of rsync to find description of this options.

Writing own tasks

First, create task class which extends Sokil\DeployBundle\Task\AbstractTask. Then add Symfony's service definition:

acme.deploy.my_task:
  class: Acme\Deploy\Task\MyTask
  abstract: true
  public: false
  tags:
    - {name: "deploy.task", alias: "myTask"}

This service must contain tag with name deploy.task and alias, which will be used as CLI command's option name and configuration section name.

Then, you may add it to bundle's configuration in app/config/config.yml to deploy section in proper place of order, if you want it to be run automatically:

deploy:
  config:
    git: {}
    grunt: {}
    myTask: {}

Now, your task will be calld third after git and grunt by calling deploy command without arguments:

$ ./app/console deploy --env=prod

You also may call your task directly from console:

$ ./app/console deploy --myTask