使用BitBucket管道通过SSH访问部署到VPS [英] Using BitBucket Pipelines to Deploy onto VPS via SSH Access
问题描述
我一直在努力思考如何利用BitBucket的管道将我的(Laravel)应用程序自动部署到Vultr Server实例上.
I have been trying to wrap my head around how to utilise BitBucket's Pipelines to auto-deploy my (Laravel) application onto a Vultr Server instance.
我有以下手动步骤,我正在尝试自动复制这些步骤:
I have the following steps I do manually, which I am trying to replicate autonomously:
- 我
commit
我的更改和push
到BitBucket存储库 - 我使用终端
ssh root@ipaddress
登录到服务器
- 我
cd
到正确的目录:cd /var/www/html/app/
- 然后我从BitBucket存储库中
pull
:git pull origin master
- 然后我运行一些命令:
composer install
,php artisan migrate
等. - 然后我注销:
exit
- I
commit
my changes andpush
to BitBucket repo - I log into my server using Terminal:
ssh root@ipaddress
- I
cd
to the correct directory:cd /var/www/html/app/
- I then
pull
from my BitBucket repo:git pull origin master
- I then run some commands:
composer install
,php artisan migrate
etc.. - I then log out:
exit
我的理解是,您可以使用管道将其自动化,这是真的吗?
My understanding is that you can use Pipelines to automatise this, is this true?
到目前为止,我已经为管道和服务器设置了SSH密钥对,因此服务器的authorized_keys
文件包含来自BitBucket管道的公共密钥.
So far, I have set up a SSH key pair for pipelines and my server, so my server's authorized_keys
file contains the public key from BitBucket Pipelines.
我的管道文件bitbucket-pipelines.yml
如下:
image: atlassian/default-image:latest
pipelines:
default:
- step:
deployment: staging
caches:
- composer
script:
- ssh root@ipaddress
- cd /var/www/html/app/
- git pull origin master
- php artisan down
- composer install --no-dev --prefer-dist
- php artisan cache:clear
- php artisan config:cache
- php artisan route:cache
- php artisan migrate
- php artisan up
- echo 'Deploy finished.'
执行管道时,出现错误:bash: cd: /var/www/html/app/: No such file or directory
.
When the pipeline executes, I get the error: bash: cd: /var/www/html/app/: No such file or directory
.
我读到每个脚本步骤都在其自己的容器中运行.
I read that each script step is run in it's own container.
管道中的每个步骤都将启动一个单独的Docker容器 运行脚本中配置的命令
Each step in your pipeline will start a separate Docker container to run the commands configured in the script
如果使用SSH登录后未在VPS内执行cd /var/www/html/app
,则我得到的错误是有道理的.
The error I get makes sense if it's not executing cd /var/www/html/app
within the VPS after logging into it using SSH.
有人可以指导我正确的方向吗?
Could someone guide me into the correct direction?
谢谢
推荐答案
在script
下定义的命令将在Docker容器中运行,而不是在VPS上运行.
The commands you are defining under script
are going to be run into a Docker container and not on your VPS.
相反,将所有命令放在服务器上的bash文件中.
Instead, put all your commands in a bash file on your server.
1-在VPS上创建bash文件pull.sh
,以执行所有部署任务
1 - Create a bash file pull.sh
on your VPS, to do all your deployment tasks
#/var/www/html
php artisan down
git pull origin master
composer install --no-dev --prefer-dist
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan migrate
php artisan up
echo 'Deploy finished.'
2-像这样在存储库中创建脚本deploy.sh
2 - Create a script deploy.sh
in your repository, like so
echo "Deploy script started"
cd /var/www/html
sh pull.sh
echo "Deploy script finished execution"
3-最后更新您的bitbucket-pipelines.yml
文件
3 - Finally update your bitbucket-pipelines.yml
file
image: atlassian/default-image:latest
pipelines:
default:
- step:
deployment: staging
script:
- cat ./deploy.sh | ssh <user>@<host>
- echo "Deploy step finished"
我建议您已经将您的存储库克隆到/var/www/html
中的VPS上,并首先手动测试pull.sh
文件.
I would recommend to already have your repo cloned on your VPS in /var/www/html
and test your pull.sh
file manually first.
这篇关于使用BitBucket管道通过SSH访问部署到VPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!