deployer部署

tech2022-09-05  133

deployer部署

This article has updated for the most recent version of Deployer on March 26th, 2017.

本文已于2017年3月26日更新为Deployer的最新版本。



Everybody tries to automate their development process, testing, code formatting, system checks, etc. This is also the case for deploying our applications or pushing a new version to the production server. Some of us do this manually by uploading the code using an FTP client, others prefer Phing, and Laravel users will prefer Envoyer for this process. In this article, I’m going to introduce you to Deployer – a deployment tool for PHP.

每个人都试图使其开发过程,测试,代码格式化,系统检查等自动化。在部署我们的应用程序或将新版本推送到生产服务器时,也是如此。 我们中的一些人通过使用FTP客户端上传代码来手动完成此操作,其他人则喜欢Phing ,而Laravel用户在此过程中会更喜欢Envoyer 。 在本文中,我将向您介绍Deployer -PHP的部署工具。

演示申请 (Demo Application)

I will be using an application from a previous article for the demo. The application will be deployed to a DigitalOcean droplet. To follow along, you can clone the demo application’s source code from GitHub.

我将使用上一篇文章中的应用程序进行演示。 该应用程序将部署到DigitalOcean Droplet。 接下来,您可以从GitHub克隆演示应用程序的源代码。

安装 (Installation)

Deployer is packaged as a PHAR file that we can download to our local machine. We can also move it to the user’s bin directory to make it global. Check the documentation for more details.

Deployer打包为PHAR文件,我们可以将其下载到本地计算机。 我们还可以将其移动到用户的bin目录中,以使其成为全局文件。 查看文档以获取更多详细信息。

mv deployer.phar /usr/local/bin/dep chmod +x /usr/local/bin/dep

定义服务器 (Defining Servers)

After cloning the demo repository, we need to create a new file called deploy.php where we’ll define our deployment steps.

克隆演示存储库后,我们需要创建一个名为deploy.php的新文件,在其中定义部署步骤。

The first step is to define our deployment servers. We can authenticate normally using a username and a password.

第一步是定义我们的部署服务器。 我们可以使用用户名和密码正常进行身份验证。

// deploy.php server('digitalocean', '104.131.27.106') ->user($_ENV['staging_server_user']) ->password($_ENV['staging_server_password']);

We can also define the type of this server (staging, production, etc), which allows us to run tasks (more on tasks later) in a specific server stage.

我们还可以定义此服务器的类型( staging , production等),这使我们可以在特定服务器阶段运行任务(稍后将详细介绍任务)。

// deploy.php use function Deployer\set; use function Deployer\server; set('default_stage', 'staging'); server('digitalocean', '104.131.27.106') ->user($_ENV['staging_server_user']) ->password($_ENV['staging_server_password']) ->stage('staging') ->env('deploy_path', '/var/www');

We need to add the default_stage attribute when using the stage method. Otherwise, we’ll get the You need to specify at least one server or stage. error.

使用stage方法时,我们需要添加default_stage属性。 否则, You need to specify at least one server or stage. 错误。

Note: If you’re using PHP 7, you can group use statements in one line like this (use function Deployer\{set, server}). You can read more about new PHP 7 features here.

注意 :如果您使用的是PHP 7,则可以在一行中将use语句分组( use function Deployer\{set, server} )。 您可以在此处阅读有关PHP 7新功能的更多信息。

SSH验证 (SSH Authentication)

It’s a common practice to use SSH authentication on production servers. If you’re not familiar with using SSH keys for authentication, check out this guide for a detailed walkthrough.

在生产服务器上使用SSH身份验证是一种常见的做法。 如果您不熟悉使用SSH密钥进行身份验证,请参阅本指南以获取详细的演练。

// deploy.php use function Deployer\{set, server}; set('default_stage', 'staging'); server('digitalocean', '104.131.27.106') ->identityFile() ->user($_ENV['staging_server_user']) ->password($_ENV['staging_server_password']) ->stage('staging');

By default, the identityFile method uses the current user’s ~/.ssh/id_rsa identity file. We can change that if needed.

默认情况下, identityFile方法使用当前用户的~/.ssh/id_rsa身份文件。 如果需要,我们可以更改它。

// deploy.php // ... ->identityFile('path/to/id_rsa', 'path/to/id_rsa.pub', 'pass phrase') // ...

Deployer supports multiple types of SSH connections, and it defaults to native which uses native system commands to authenticate.

Deployer支持多种类型的SSH连接,并且默认为native ,它使用本地系统命令进行身份验证。

安装SSH2扩展 (Install SSH2 Extension)

Deployer also supports using the PHP SSH2 extension. After installing the extension and enabling it, we need to require the herzult/php-ssh package which provides an OOP wrapper for the PHP extension, and sets the ssh_type config option to ext-ssh2.

Deployer还支持使用PHP SSH2扩展。 安装扩展并启用它之后,我们需要使用herzult/php-ssh软件包,该软件包为PHP扩展提供了OOP包装,并将ssh_type config选项设置为ext-ssh2 。

// deploy.php set('ssh_type', 'ext-ssh2'); // ...

The only problem here is that Deployer doesn’t have the php-ssh package shipped with the PHAR archive. We’ll need to clone the Deployer repository, require the package, and finally use the build script to generate a new PHAR archive.

唯一的问题是Deployer没有PHAR归档文件随附的php-ssh软件包。 我们将需要克隆Deployer repository ,需要使用该包,最后使用build脚本来生成新的PHAR存档。

使用配置文件 (Using Configuration Files)

We can also define our servers using a YAML configuration file, and pass the file to the serverList method. You can read more about servers in the documentation.

我们还可以使用YAML配置文件定义服务器,并将该文件传递给serverList方法。 您可以在文档中阅读有关服务器的更多信息。

// servers.yml digitalocean: host: 104.131.27.106 user: root identity_file: ~ stage: staging deploy_path: /var/www/

We can now load the file inside our deploy.php file.

现在,我们可以将文件加载到deploy.php文件中。

serverList('servers.yml');

定义任务 (Defining Tasks)

Tasks are commands that can be run through the dep command.

任务是可以通过dep命令运行的命令。

dep deploy:staging

The deploy:staging argument should be a task inside our deploy.php file. We can upload files, run commands on the server, etc.

deploy:staging参数应该是我们deploy.php文件中的任务。 我们可以上传文件,在服务器上运行命令等。

// deploy.php use function Deployer\{server, task, run, set, get, add, before, after, upload}; task('deploy:staging', function() { writeln('<info>Deploying...</info>'); $appFiles = [ 'app', 'bootstrap', 'public', 'composer.json', 'composer.lock', 'artisan', '.env', ]; $deployPath = get('deploy_path'); foreach ($appFiles as $file) { upload($file, "{$deployPath}/{$file}"); } cd($deployPath); run("composer update --no-dev --prefer-dist --optimize-autoloader"); run("chown -R www-data:www-data app/storage"); set('writable_dirs', ['app/storage']); writeln('<info>Deployment is done.</info>'); });

The file looks overwhelming at first, but there’s nothing special here. First, we print a message using the writeln method to notify the user that the deployment process has started. Next, we define the application folders that need to be uploaded.

最初,该文件看起来不堪重负,但是这里没有什么特别的。 首先,我们使用writeln方法打印一条消息,以通知用户部署过程已经开始。 接下来,我们定义需要上载的应用程序文件夹。

The upload method will send our local application files to the server inside the for loop. Check the documentation for the list of available functions.

upload方法会将我们的本地应用程序文件发送到for循环内的服务器。 请查阅文档以获取可用功能列表。

We cd to the server directory and update our Composer dependencies using the run method, which lets us run any shell command on the server.

我们cd到服务器目录中,并使用更新我们的作曲家的依赖run的方法,它可以让我们在服务器上运行任何shell命令。

The next and final step is to make the necessary directories writable by the server user, in this case www-data for Apache. The writable_dirs parameter tells Deployer about our writable directories. We can achieve the same thing using a shell command, too.

下一步也是最后一步,就是使必要的目录可由服务器用户写入,在本例中为Apache的www-data 。 writable_dirs参数告诉Deployer我们的可写目录。 我们也可以使用shell命令来实现相同的目的。

dep deploy:staging task("deploy:staging", function () { // ... })->desc('Deploy application to staging.');

The desc method will add a help message to our task (command).

desc方法将向我们的任务( 命令 )添加帮助消息。

Our current file has one big task to handle the whole deployment process. We’ll split it into small reusable tasks and run them one after the other in the deploy:staging task.

我们当前的文件有一项大任务要处理整个部署过程。 我们将其分成多个可重用的小任务,并在deploy:staging任务中一个接一个地运行它们。

task('deploy:started', function() { writeln('<info>Deploying...</info>'); }); task('deploy:done', function() { writeln('<info>Deployment is done.</info>'); });

The above tasks are just to notify the user of the deployment state. Deployer provides after and before methods (hooks) to run tasks when other tasks are fired.

以上任务只是为了通知用户部署状态。 部署程序提供after其他任务被触发时运行任务的before方法( 挂钩 )。

before('deploy:staging', 'deploy:started'); after('deploy:staging', 'deploy:done'); task('deploy:upload', function() { $appFiles = [ 'app', 'bootstrap', 'public', 'composer.json', 'composer.lock', 'artisan', '.env', ]; $deployPath = get('deploy_path'); foreach ($appFiles as $file) { upload($file, "{$deployPath}/{$file}"); } }); task('deploy:writable_dirs', function() { $deployPath = get('deploy_path'); cd($deployPath); run("chown -R www-data:www-data app/storage"); set('writable_dirs', ['app/storage']); }); task('deploy:composer', function() { $deployPath = get('deploy_path'); cd($deployPath); run("composer update --no-dev --prefer-dist --optimize-autoloader"); });

All that’s left is to run all the tasks in the deploy:staging task.

剩下的就是运行deploy:staging任务中的所有任务。

task('deploy:staging', [ 'deploy:upload', 'deploy:writable_dirs', 'deploy:composer', ]);

Because we split our tasks into meaningful parts, we can re-use them later for another production server, and even for other projects!

因为我们将任务划分为有意义的部分,所以我们以后可以将其重新用于其他生产服务器,甚至用于其他项目!

零停机时间部署 (Zero Downtime Deployment)

Currently, our Apache server root is pointing to the /var/www/public directory. When we’re deploying a new version of our application, we need to put the server in maintenance mode for a few minutes to avoid any downtime for users.

当前,我们的Apache服务器根目录指向/var/www/public目录。 当我们部署应用程序的新版本时,我们需要将服务器置于维护模式几分钟,以避免用户停机。

A simple solution for this problem is to create a list of releases, and point our server’s root to a current directory, which will link to the latest release.

解决此问题的简单方法是创建一个发行列表,并将我们服务器的根目录指向当前目录,该目录将链接到最新发行版。

/current (link pointing to the current release) /releases /realease_1 /realease_2 /realease_3

常见部署任务 (Common Deployment Tasks)

Deployer has a list of common tasks used by most PHP apps, something like we did earlier. We’ll re-factor our deploy.php to re-use those common tasks where possible.

Deployer列出了大多数PHP应用程序使用的常见任务,就像我们之前所做的那样。 我们将deploy.php我们的deploy.php以便在可能的情况下重新使用这些常见任务。

// deploy.php require_once "recipe/common.php"; set('ssh_type', 'ext-ssh2'); set('default_stage', 'staging'); set('deploy_path', '/var/www'); set('copy_dirs', [ 'app/commands', 'app/config', 'app/controllers', 'app/database', 'app/lang', 'app/models', 'app/src', 'app/start', 'app/tests', 'app/views', 'app/filters.php', 'app/routes.php', 'bootstrap', 'public', 'composer.json', 'composer.lock', 'artisan', '.env', ]); set('shared_dirs', [ 'app/storage/cache', 'app/storage/logs', 'app/storage/meta', 'app/storage/sessions', 'app/storage/views', ]); set('writable_dirs', get('shared_dirs')); set('http_user', 'www-data');

First we set the some variables that we’ll be using inside our tasks. shared_dirs, writable_dirs and http_user are all used by the common tasks..

首先,我们设置一些将在任务中使用的变量。 shared_dirs , writable_dirs和http_user都由常见任务使用。

task('deploy:upload', function() { $files = get('copy_dirs'); $releasePath = get('release_path'); foreach ($files as $file) { upload($file, "{$releasePath}/{$file}"); } });

We kept the deploy:upload task in this case, but you can also use the deploy:update_code task to pull your application from remote Git hosts. Don’t forget to set the necessary attributes when using it.

在这种情况下,我们保留了deploy:upload任务,但是您也可以使用deploy:update_code任务从远程Git主机提取应用程序。 使用时请不要忘记设置必要的属性。

set('repository', 'http://github.com/whyounes/500pxAPI_Test.git'); set('branch', 'master'); // deploy.php task('deploy:staging', [ 'deploy:prepare', 'deploy:release', 'deploy:upload', 'deploy:shared', 'deploy:writable', 'deploy:symlink', 'deploy:vendors', 'current', ])->desc('Deploy application to staging.'); after('deploy:staging', 'success');

deploy:prepare: Test the connection, shared folder, releases folder, etc.

deploy:prepare :测试连接,共享文件夹,发布文件夹等。

deploy:release: Create the release directory.

deploy:release :创建发布目录。

deploy:upload: Our task for uploading files.

deploy:upload :我们用于上传文件的任务。

deploy:shared: Create shared folders if they do not exist. Uses the shared_dirs attributes we set earlier.

deploy:shared :如果共享文件夹不存在,则创建它们。 使用我们之前设置的shared_dirs属性。

deploy:writable: Set writable directories.

deploy:writable :设置可写目录。

deploy:symlink: Create a symbolic link from the release to current.

deploy:symlink :创建从发行版到current的符号链接。

deploy:vendors: Run Composer installation.

deploy:vendors :运行Composer安装。

current: Print the current release name.

current :打印当前版本名称。

success: Print a success message.

success :打印成功消息。

部署食谱 (Deployment Recipes)

Because most of us are using frameworks for our projects, Deployer has some pre-configured recipes for well-known frameworks like Laravel, Symfony, Yii, Zend, etc. Check the recipes section in the documentation for more details.

因为我们大多数人都在为项目使用框架,所以Deployer为知名框架(如Laravel,Symfony,Yii,Zend等)提供了一些预配置的配方。有关更多详细信息,请参阅文档中的“ 配方”部分。

结论 (Conclusion)

Deployer has a really nice API to make the deployment process easy and configurable. If you’ve never used a deployment tool to automate your deployment process, we really encourage you to try it!

Deployer有一个非常好的API,可简化部署过程并对其进行配置。 如果您从未使用过部署工具来自动化部署过程,我们强烈建议您尝试一下!

If you’ve used Deployer before we would love to hear what you think! If you have any questions, you can post them below!

如果您在使用Deployer之前,我们很想听听您的想法! 如有任何疑问,可以在下面发布!

翻译自: https://www.sitepoint.com/deploying-php-applications-with-deployer/

deployer部署

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)