如何让 Circle CI SSH 访问我的服务器?

How to give Circle CI SSH access to my server?

我对 CI 和 CD 解决方案有一些疑问和问题。

Rails: 4.2

卡皮斯特拉诺:3.4.0

该应用程序托管在私人服务器上。

现在我的工作流程是通过终端部署开发、暂存和生产。 我还连接了 Circle CI 在这些分支上工作得很好。

我找不到如何设置 Circle CI 以使用 Capistrano 进行部署。 一切都在 Capistrano 配置中使用服务器用户进行配置。

如何让 Circle CI SSH 访问我的 deploy 用户?因为现在我必须为用户提供密码。

使用 SSH 密钥进行身份验证。您也可以将它用于您自己的 SSH 会话,因为它比密码身份验证更方便和安全(很少见!)。查看 this tutorial 了解如何设置。

然后,将您的私钥粘贴到 Project Settings -> SSH Permissions 中的 CircleCI,如 here 所述。您需要从您添加到服务器上 deploy 用户的 public 密钥对中复制本地计算机的私钥。然后 CircleCI 将可以通过 SSH 访问您的服务器。

您可以将主机名设置为指向您的服务器的域或服务器的 IP,或者将其留空以便所有主机都使用此密钥。

CircleCI 版本 2 使用工作流构建和部署

让我们假设以下非常基本的 PHP 应用程序。 Apache 配置指向 /web。以 * 结尾的文件和文件夹被 Git 忽略。

__repo
  |__.circleci
  |  |__config.yml
  |__.git
  |__tests
  |  |__features
  |  |__behat.yml
  |__scripts
  |  |__deploy.sh
  |__web
  |  |__node_modules*
  |  |__index.php
  |  |__styles.scss
  |  |__gulpfile.js
  |  |__styles.css*
  |__.gitignore
  1. 在服务器上创建一个新用户并将其添加到 www-data 组。让它递归地拥有整个仓库。假设此用户名为 repo-boss.

    $ chown -R repo-boss:www-data repo/

  2. 在您的本地计算机上创建一个新的 SSH 密钥对。将私钥添加到 CircleCI 的后端,并查看我们稍后需要的结果指纹。将 public 键添加到 /home/repo-boss/.ssh/authorized_keys.

现在假设 deploy.sh 脚本包含以下非常基本的命令。

#!/usr/bin/env bash

# Set script to exit on errors.
set -e

# Get script's absolute location.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Change to repo root.
cd ${DIR};
cd ..

# Git pull.
git status
git pull

# Run Gulp.
cd web/
gulp sass

现在 CircleCI 的 config.yml 使整个工作正常(当然,在您至少将必要的东西拉到服务器上之后)。 deploy 仅当测试成功完成时才会 运行。

version: 2
jobs:
  build:
    docker:
      - image: circleci/php:7.1-apache-node-browsers

    working_directory: ~/repo-name

    steps:
      - checkout

      - run:
          name: Whatever you need to get your app up and running.
          command: |
            command1 # Have a look at https://github.com/leymannx/drupal-circleci-behat/blob/develop/.circleci/config.yml for a more detailed example.
            command3
            command4

      - run:
          name: Run Tests.
          command: |
            behat --no-snippets -f pretty -o std

  deploy:
    machine:
      enabled: true
    working_directory: ~/repo-name
    steps:
      - checkout
      - run:
          name: Fix ssh Could not resolve hostname
          command: |
            ssh-keyscan 123.45.67.89 >> ~/.ssh/known_hosts # Add live server IP to known hosts.
            ssh-keyscan 555.45.67.89 >> ~/.ssh/known_hosts # Dev server, too.

      - add_ssh_keys: # add private SSH key from CircleCI account based on fingerprint.
          fingerprints:
            - "14:09:a1:b2:b3:c4:d5:e6:f7:g8:h9:81:"

      - run:
          name: Deploy master.
          command: if [ "${CIRCLE_BRANCH}" == "master" ]; then ssh repo-boss@123.45.67.89 'cd /var/www/repo/scripts && . deploy.sh'; else echo "Skipped"; fi
      - run:
          name: Deploy develop.
          command: if [ "${CIRCLE_BRANCH}" == "develop" ]; then ssh repo-boss@555.45.67.89 'cd /var/www/repo/scripts && . deploy.sh'; else echo "Skipped"; fi

workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build:
          filters:
            branches:
              only:
                - develop
                - master
      - deploy:
          requires:
            - build
          filters:
            branches:
              only:
                - develop
                - master

当然,您不需要使用工作流。您也可以在基本瀑布中实现此目的。但我更喜欢将构建和部署这两个部分拆分为不同的协调工作。