deployer部署
by Bryan Lee
通过李恩
There are many deployment solutions out there with deploying Laravel, ranging from SSH’ing into your machine and git pull
ing the files () to time-savers like Envoyer or rolling your own with Deployer. I personally love Deployer as it is free, very flexible and can support your projects from infancy to when you scale to multiple machines.
有许多部署Laravel的部署解决方案,包括SSH进入您的计算机并git pull
文件()到省时之类的Envoyer或使用Deployer滚动。 我个人喜欢Deployer,因为它是免费的,非常灵活的,并且可以支持从初期到扩展到多台机器的项目。
Deployer works by running your deployment scripts locally and we can make this even more convenient by integrating Deployer into your CI pipeline. For a recent project, I used CircleCI to run all my tests and automatically deploy.
Deployer通过在本地运行部署脚本来工作,并且通过将Deployer集成到您的CI管道中,我们可以使此操作更加方便。 对于最近的项目,我使用CircleCI来运行所有测试并自动部署。
To save time, I will highly recommend that you have Deployer installed locally and configured and working for your app. This saves a lot of time in having to do debugging on your Deployer config when your CircleCI build fails.
为了节省时间,我强烈建议您在本地安装Deployer ,并对其进行配置和使用。 当CircleCI构建失败时,这可以节省大量时间,不必在Deployer配置上进行调试。
If you already have CircleCI setup for your Laravel project tests, skip to the next section. Otherwise, I assume that you at least already have a CircleCI account and a basic project created inside.
如果您已经为Laravel项目测试安装了CircleCI设置,请跳至下一部分。 否则,我假设您至少已经拥有一个CircleCI帐户和一个内部创建的基本项目。
Create a .circleci/config.yml
and with the below
创建一个.circleci/config.yml
并使用以下内容
version: 2
jobs:
build:
docker:
# Specify the version you desire here
- image: circleci/php:7.1-browsers
- image: circleci/mysql:5.7
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_USER: root
MYSQL_ROOT_PASSWORD: ''
MYSQL_DATABASE: laravel
working_directory: ~/laravel
steps:
- checkout
- run:
name: Install PHP exts
command: |
sudo docker-php-ext-install zip
sudo docker-php-ext-install pdo_mysql
sudo apt install -y mysql-client
- run: sudo composer self-update
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "composer.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: composer install -n --prefer-dist
- save_cache:
paths:
- ./vendor
key: v1-dependencies-{{ checksum "composer.json" }}
- run:
name: Setup Laravel stuffs
command: |
php artisan migrate --force
- run: ./vendor/bin/phpunit
workflows:
version: 2
notify_deploy:
jobs:
- build
This is a very basic config.yml
for Laravel. It will install PHP, MySQL and run PHPunit tests. Feel free to modify this to suit your needs.
这是config.yml
的非常基本的config.yml。 它将安装PHP,MySQL并运行PHPunit测试。 可以随意修改它以满足您的需求。
After our tests are run, we want CircleCI to automatically start deploying to our server. Remember that Deployer uses SSH, so in our remote server, we want to create a SSH keypair ssh-keygen -t rsa -b 4096 -C “your@email.com"
. We want to add this key we created into CircleCI as a deployment key. Take note of the fingerprint, as we will need to add this into our config file later.
在运行测试之后,我们希望CircleCI自动开始部署到我们的服务器。 请记住,Deployer使用SSH,因此在我们的远程服务器中,我们想创建一个SSH密钥对ssh-keygen -t rsa -b 4096 -C “your@email.com"
。 部署密钥 ,请注意指纹,因为我们稍后需要将其添加到配置文件中。
Let’s create a new job in our config file:
让我们在配置文件中创建一个新作业:
jobs:
build: ... # from above
deploy:
docker:
- image: circleci/php:7.2-browsers
working_directory: ~/laravel
steps:
- checkout
This tells CircleCI that we have a new job called deploy
and to build it based on the php-7.2-browsers docker image.
这告诉CircleCI我们有一个名为deploy
的新工作,并根据php-7.2-browsers码头工人镜像来构建它。
Remember the fingerprint that we took note of when we added it into CircleCI? We need to reference this in in our config so that the SSH key will be present in our container.
还记得我们将其添加到CircleCI时注意到的指纹吗? 我们需要在配置中引用它,以便SSH密钥出现在我们的容器中。
jobs:
build: ... # from above
deploy:
docker:
- image: circleci/php:7.2-browsers
working_directory: ~/laravel
steps:
- checkout
- add_ssh_keys:
fingerprints:
- "YOUR_FINGERPRINT"
So now, CircleCI knows to add this key via the fingerprint into the docker container and this will allow us to run Deployer and SSH into our remote server to deploy.
因此,现在,CircleCI知道可以通过指纹将此密钥添加到docker容器中,这将使我们能够将Deployer和SSH运行到要部署的远程服务器中。
jobs:
build: ... # from above
deploy:
docker:
- image: circleci/php:7.2-browsers
working_directory: ~/laravel
steps:
- checkout
- add_ssh_keys:
fingerprints:
- "YOUR_FINGERPRINT"
- run:
name: Install Deployer
command: |
curl -LO https://deployer.org/deployer.phar
sudo mv deployer.phar /usr/local/bin/dep
sudo chmod +x /usr/local/bin/dep
- run:
name: Deploy
command: |
dep deploy www.your_server.com
With the deploy
job defined, the last step would be to tell CircleCI to run it after our tests pass. That’s simple, we already have a basic workflow named notify_deploy
that we can build upon.
定义了deploy
作业后,最后一步是告诉我们的CircleCI在测试通过后运行它。 很简单,我们已经有了一个可以建立的基本工作流,名为notify_deploy
。
workflows:
version: 2
notify_deploy:
jobs:
- build
- deploy:
requires:
- build
filters:
branches:
only: master
This tells CircleCI that in our notify_deploy
workflow, aside from running build
, we want to run deploy
after it is finished, and to only do it on the master
branch.
这告诉CircleCI,在notify_deploy
工作流中,除了运行build
,我们还想在完成后运行deploy
,并且只在master
分支上执行。
With everything done, we can now push the config file to our git repository and watch it test and deploy on its own. Do you use a different way of deploying Laravel? Let me know, I’d love to hear about it!
完成所有操作后,我们现在可以将配置文件推送到我们的git存储库,并观看它的测试和部署。 您是否使用其他方式部署Laravel? 让我知道,我很想听听!
deployer部署