首页 > 解决方案 > 如何使用 SSH 密钥将 Angular 生产版本部署到使用 bitbucket 管道的外部服务器?

问题描述

我们正在尝试使用 Bitbucket Pipelines 将基本的 Angular 应用程序部署到 Google Cloud 上的 VM,但不确定如何使用 SSH 密钥连接到服务器以复制构建文件。寻找示例但找不到。

我们可以使用 Putty/SSH 命令手动复制dist文件。

我们有来自 Google Cloud 虚拟机的公钥/私钥,并将它们添加到 Bitbucket Pipelines > SSH Keys

在此处输入图像描述

我们的 YML 脚本如下:

image: node:6.9.4

pipelines:
  default:
    - step:
       caches:
         - node
       script: # Modify the commands below to build your repository.
         - npm install
         - npm install -g @angular/cli@1.6.4
         - ng build --prod
         - cd dist/
         - ssh -i ???

标签: angularbitbucket-pipelines

解决方案


正如@Chris 所说,那篇文章是正确的起点。步骤是:

  1. 通过 UI 或运行 在 BitBucket 中添加 SSH 密钥ssh-keygen

  2. 通过 UI更新已知主机。

  3. 通过将公钥添加到远程主机

    cat ~/.ssh/my_ssh_key.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"

或通过

`ssh-copy-id -i my_ssh_key username@remote_host`
  1. 然后使用此命令复制文件(应该在您的脚本中):

    scp username@remote_host:/path/to/file /path/to/destination


推荐阅读