使用 BitBucket Pipelines 通过 SSH 访问部署到 VPS [英] Using BitBucket Pipelines to Deploy onto VPS via SSH Access

查看:22
本文介绍了使用 BitBucket Pipelines 通过 SSH 访问部署到 VPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在思考如何利用 BitBucket 的管道将我的 (Laravel) 应用程序自动部署到 Vultr 服务器实例上.

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:

  • 提交我的更改并push到BitBucket repo
  • 我使用终端登录我的服务器:ssh root@ipaddress
  • cd到正确的目录:cd/var/www/html/app/
  • 然后我从我的 BitBucket 存储库中pull:git pull origin master
  • 然后我运行一些命令:composer installphp artisan migrate 等等.
  • 然后我退出:exit
  • I commit my changes and push 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

我的理解是你可以使用 Pipelines 来自动化这个,这是真的吗?

My understanding is that you can use Pipelines to automatise this, is this true?

到目前为止,我已经为管道和我的服务器设置了一个 SSH 密钥对,所以我服务器的 authorized_keys 文件包含来自 BitBucket Pipelines 的公钥.

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 如下:

My pipelines file bitbucket-pipelines.yml is as follows:

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 后它没有在 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 Pipelines 通过 SSH 访问部署到 VPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆