sokil / deploy-bundle
Symfony 应用部署包
Requires
- php: ^5.5 || ^7.0
Requires (Dev)
- phpunit/phpunit: >=3.7.38 <6.0
- satooshi/php-coveralls: >=0.7.1 <2.0
- squizlabs/php_codesniffer: ^2.3
- symfony/console: ~2.0|~3.0
- symfony/event-dispatcher: ~2.1|~3.0
- symfony/framework-bundle: ~2.3|~3.0
- symfony/process: ~2.1|~3.0
README
用于 Symfony 项目的任务执行器
安装
添加 Composer 依赖
composer.phar require sokil/deploy-bundle
将包添加到您的 AppKernel
<?php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( new Sokil\DeployBundle\DeployBundle(), ); } }
配置
在 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]
在 config
部分,声明了每个任务的选项,可以运行。在 tasks
部分,声明了任务包,这些包按顺序运行。可以通过在 CLI 命令中定义任务别名来运行任务。也可以定义任务包。
$ ./app/console deploy --git --npm
也可以定义任务包
$ ./app/console deploy --compileAssets
如果没有指定任务,则默认任务包将被运行。此任务包可以在配置中定义,但如果省略,则默认任务由 config
部分中的所有任务按顺序组成。任务和任务包都可以在 CLI 选项中指定,然后按首次出现的顺序运行任务。任务包也可以包含其他包。
要获取所有配置任务的列表,请运行
$ ./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"
任务
Git
配置 Git 任务
将配置添加到您的 ./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
私有仓库
如果仓库是私有的,则在拉取时会要求输入密码
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
例如,Web 服务器在 www-data 用户下启动。为了防止要求输入密码,请将 SSH 密钥添加到 /home/$USER/.ssh
目录,使用 SSH 密钥生成工具。
- 生成密钥
$ 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:
...
-
将公钥添加到您的仓库,以获取更改而不提示密码。
-
测试您的连接
$ sudo -H -u www-data git pull origin master
找出谁已经使用此密钥
ssh -T git@github.com
ssh -T git@bitbucket.com
Webpack
您可以选择在 pathToWebpack
参数中指定 webpack 的路径。如果省略,则使用 webpack
。在这种情况下,webpack 必须全局安装。
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 ...
这将运行命令,其中 context
的值是 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
将任务配置添加到您的部署配置中
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 可能有几个任务,您可以选择要运行的任务。
在配置中
deploy: config: grunt: bundles: SomeBundle: tasks: - newer:less - newer:jade SomeOtherBundle: true parallel: true
作为 CLI 参数
./app/console deploy --grunt --grunt-tasks="SomeBundle=newer:less,newer:jade&SomeOtherBundle"
迁移
添加依赖
composer.phar require doctrine/migrations
composer.phar require doctrine/doctrine-migrations-bundle
在 AppKerner
中注册打包器
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
首先,在 ./app/config/config.yml
中配置迁移
doctrine_migrations: dir_name: %kernel.root_dir%/migrations namespace: Migrations table_name: migrations name: Application Migrations
然后,在 ./app/config/config.yml
中的部署配置中添加任务
deploy: config: migrate: {}
安装资产
然后,在 ./app/config/config.yml
中的部署配置中添加任务
deploy: config: assetsInstall: {}
导出 Assetic
然后,在 ./app/config/config.yml
中的部署配置中添加任务
deploy: config: asseticDump: {}
必须定义包含 Assetic 资产的分包,在 ./app/config/config.yml
assetic: bundles: - AcmeBundle
Composer
此任务将运行 composer 命令
composer.phar install --optimize-autoloader --no-interaction
必须存在于路径中 composer.phar
文件,或者您可以通过 path
参数重新定义 composer 的路径。
将任务配置添加到 ./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
清除缓存
deploy: config: clearCache: {}
同步
用于将数据同步到生产服务器的任务。添加配置
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
rules
参数定义了从源到目标同步文件的规则。可以配置任意数量的规则。每个规则由参数 src
(默认为".")、参数 dest
(可以是字符串或主机数组)组成。其他规则参数与 rsync
选项相同。请参阅 rsync
的手册页面以找到这些选项的描述。
编写自己的任务
首先,创建一个扩展 Sokil\DeployBundle\Task\AbstractTask
的任务类。然后添加 Symfony 的服务定义
acme.deploy.my_task: class: Acme\Deploy\Task\MyTask abstract: true public: false tags: - {name: "deploy.task", alias: "myTask"}
此服务必须包含名为 deploy.task
的标记和别名,该别名将用作 CLI 命令选项名称和配置部分名称。
然后,您可以在 app/config/config.yml
中的 deploy
部分添加它,如果想要它自动运行
deploy: config: git: {} grunt: {} myTask: {}
现在,您的任务将在调用 deploy
命令时作为第三个参数调用,而不带参数
$ ./app/console deploy --env=prod
您也可以直接从控制台调用您的任务
$ ./app/console deploy --myTask