13

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

导轨:4.2

卡皮斯特拉诺:3.4.0

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

现在,我拥有通​​过终端部署开发、登台和生产的工作流程。我还连接了在这些分支上运行良好的 Circle CI。

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

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

4

2 回答 2

9

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

然后,将您的私钥粘贴到 CircleCI 中,如此Project Settings -> SSH Permissions所述。您需要从您将其公钥添加到服务器上用户的密钥对中从本地计算机复制私钥。然后,CircleCI 将拥有对您服务器的 SSH 访问权限。deploy

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

于 2015-09-09T02:28:56.627 回答
4

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 的后端,然后查看我们稍后需要的结果指纹。将公钥添加到/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

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

于 2017-05-18T11:26:23.777 回答