Gitlab-ci配置docker runner以使用SSH密钥进行部署

时间:2017-04-13 16:14:33

标签: symfony docker ssh capistrano gitlab

我正在尝试使用gitlab-ci和capistrano来部署我的symfony应用程序。但是我无法通过将密钥注入docker来使用SSH进行部署,该脚本在连接时会一直提示输入密码。我正在使用gitlab的本地实例。

在gitlab的 SSH_PRIVATE_KEY 私有变量中,我添加了git用户的私钥,并在 SSH_SERVER_HOSTKEYS 中添加了ssh-keyscan -H 192.168.0.226命令的结果。
在deploy的.ssh文件夹中的文件 authorized_keys 中,我输入了git用户的公钥。

以下是配置文件:

gitlab-ci.yml:

image: php:7.1

cache:
  paths:
  - vendor/

before_script:
# Install dependencies
- bash ci/docker_install.sh > /dev/null
- bash ci/ssh_inject.sh

stages:
  - deploy

deploy:
  stage: deploy
  script:
  - apt-get install ruby-full -yqq
  - gem install capistrano -v 3.8.0
  - gem install capistrano-symfony
  - cap production deploy
  environment:
    name: production
    url: http://website.com
  only:
  - master

ssh_inject.sh: link

#!/bin/bash

set -xe

# Install ssh-agent if not already installed, it is required by Docker.
# (change apt-get to yum if you use a CentOS-based image)
which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )

# Run ssh-agent (inside the build environment)
eval $(ssh-agent -s)

# Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
ssh-add <(echo "$SSH_PRIVATE_KEY")

mkdir -p ~/.ssh
[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts

deploy.rb:

# config valid only for current version of Capistrano
lock '3.8.0'

set :application, 'symfony'
set :repo_url, 'git@gitlab.local:symfony.git'

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/home/symfony'

set :symfony_env,  "prod"

set :composer_install_flags, '--no-dev --prefer-dist --no-interaction --optimize-autoloader'

set :symfony_directory_structure, 3
set :sensio_distribution_version, 5

# symfony-standard edition directories
set :app_path, "app"
set :web_path, "web"
set :var_path, "var"
set :bin_path, "bin"

set :app_config_path, "app/config"
set :log_path, "var/logs"
set :cache_path, "var/cache"

set :symfony_console_path, "bin/console"
set :symfony_console_flags, "--no-debug"

# asset management
set :assets_install_path, "web"
set :assets_install_flags,  '--symlink'

# Share files/directories between releases
set :linked_files, %w(app/config/parameters.yml)
set :linked_dirs, %w(web/uploads)

# Set correct permissions between releases, this is turned off by default
set :permission_method, false
set :file_permissions_paths, ["var/logs", "var/cache"]
set :file_permissions_users, ["apache"]

before "deploy:updated", "deploy:set_permissions:acl"
after "deploy:updated", "symfony:assetic:dump"

production.rb:

server '192.168.0.226', user: 'deploy', roles: %w{app db web}

可能有什么不对?我试图将forward_agent设置为true,但它不能正常工作。

如果我手动构建docker容器并安装所有依赖项,则可以建立ssh连接而无需输入密码......

这是错误: password error

修改

在跑步者配置中是否有添加内容?这是:

concurrent = 1
check_interval = 0

[[runners]]
  name = "Docker runner"
  url = "http://gitlab.local/ci"
  token = "mytoken"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "php:7.1"
    privileged = false
    disable_cache = false
    volumes = ["/cache"]
  [runners.cache]

3 个答案:

答案 0 :(得分:0)

您需要使用gitlab-runner用户在gitlab runner上创建一个ssh密钥。

然后在authorized_keys文件中将此密钥的pubkey添加到您的服务器。

答案 1 :(得分:0)

通过将 ssh_inject.sh 内容移动到 gitlab-ci.yml 来解决。

如果有人知道为什么它需要在 gitlab-ci.yml 中,我想了解。

答案 2 :(得分:0)

bash ci/ssh_inject.sh不起作用,因为它在不同的Shell中运行。使用:

source ./ci/ssh_inject.sh

相反。