【问题标题】:Gitlab CI/CD issue with SSH config fileSSH 配置文件的 Gitlab CI/CD 问题
【发布时间】:2022-01-17 04:52:58
【问题描述】:

我正在尝试将我的第一个项目部署到我的生产服务器。这是部署阶段的脚本:

deploy_production:
  stage: deploy
  script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "ssh -p 69" "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - ./vendor/bin/envoy run deploy
  environment:
    name: production
  when: manual
  only:
    - main

当我运行舞台时,我得到了这个错误:

[myServer@xxx.xxx.x.x]:  /home/php/.ssh/config: line 1: Bad configuration option: ssh
[myServer@xxx.xxx.x.x]:  /home/php/.ssh/config: terminating, 1 bad configuration options

[âœ-] 此任务未在您的一台服务器上成功完成。

为什么它试图访问此路径上的SSH

/home/php/.ssh/config

【问题讨论】:

    标签: ssh gitlab cicd


    【解决方案1】:

    为什么要尝试访问此路径上的 SSH:

    这应该和gitlab-ci使用的帐号有关:应该是在$HOME/.ssh中寻找SSH设置:先显示$HOME是什么。


    如果您查看official documentation,您会看到 SSH 设置依赖于与 SSH 文件夹/文件关联的适当权限:

    efore_script:
      ##
      ## Install ssh-agent if not already installed, it is required by Docker.
      ## (change apt-get to yum if you use an RPM-based image)
      ##
      - 'command -v ssh-agent >/dev/null || ( 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
      ## We're using tr to fix line endings which makes ed25519 keys work
      ## without extra base64 encoding.
      ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
      ##
      - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    
      ##
      ## Create the SSH directory and give it the right permissions
      ##
      - mkdir -p ~/.ssh
      - chmod 700 ~/.ssh
    

    如果您将密钥存储在 ~/.ssh 中,我会在 chmod 400 my_private_key 之前提到。
    为了安全起见,我会添加一个chmod 600 ~/.ssh/config

    重点是:如果权限被打开,SSH会拒绝运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 2019-07-04
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 2022-10-21
      • 2019-08-07
      相关资源
      最近更新 更多