GITLAB CI加载密钥“/ dev / fd / 63”时出错:格式无效错误:作业失败:退出代码1

问题描述 投票:1回答:1

这是我的代码giltlab-ci.yml:

 before_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)
  ##
  - '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
  ## 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
  ##
  - mkdir -p ~/.ssh
  #- echo -n "$PROJECT_SSH_KEY" | ssh-add - >/dev/null
  - echo "$PROJECT_SSH_KEY"
  - ssh-add <(echo "$PROJECT_SSH_KEY")
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  - git config --global user.email "[email protected]"
  - git config --global user.name "Walid Mansia"

  ##
  ## Create the SSH directory and give it the right permissions
  ##
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh

  ##
  ## Optionally, if you will be using any Git commands, set the user name and
  ## and email.
  ##
  #- git config --global user.email "[email protected]"
  #- git config --global user.name "User name"

我把它拿出去了

在Allence-Tunisie-docker-runner sH47eTgb上使用gitlab-runner 11.8.0(4745a6f3)运行使用带有图像ntfactory / ci-tool的Docker执行器:0.0.2 ...拉码头图像ntfactory / ci-tool:0.0.2。 ..使用docker image sha256:7fe7b170806f6846271eec23b41c4f79202777f62c0d7a32165dc41722900979 for ntfactory / ci-tool:0.0.2 ...通过a732493b4b94在runner-sH47eTgb-project-11060727-concurrent-0上运行...克隆存储库...克隆到'/ builds / allence-tunisie / e-formation'...将0a6b48ef检出为feat / gitlab-ci ...跳过Git子模块设置检查缓存是否默认...没有提供URL,不会从共享缓存服务器下载缓存。而是将提取本地版本的缓存。成功提取了缓存$ ssh-agent || (apt-get update -y && apt-get install openssh-client -y)/ usr / bin / ssh-agent $ eval $(ssh-agent -s)代理pid 12 $ mkdir -p~ / .ssh $ echo“ $ SSH_PRIVATE_KEY“| tr -d'\ r'| ssh-add - > / dev / null加载密钥错误“(stdin)”:格式错误错误:作业失败:退出代码1

即使我试过 - 回声“$ SSH_PRIVATE_KEY”| tr -d'\ r'| ssh-add - > / dev / null我收到此错误

加载密钥“(stdin)”时出错:格式无效

gitlab-ci
1个回答
4
投票

当$ SSH_PRIVATE_KEY中的私钥格式错误时,会发生此错误,如果您在其中添加一些随机字符,则可以在本地轻松测试它。特别是,当您将私钥复制并粘贴到在线表单中的SSH_PRIVATE_KEY变量时,它会发生在Travis-CI上。它与----- BEGIN RSA私钥-----,----- END RSA私钥-----块之前和之后的新行字符有关。因此,我使用base64编码来确保密钥格式正确。

试试这个:

  • 编码您的私人RSA密钥 cat my_private_key | base64 -w0
  • 将base64字符串添加到项目变量中。
  • 在.gitlab-ci.yml中使用它

ssh-add <(echo“$ SSH_PRIVATE_KEY”| base64 -d)

© www.soinside.com 2019 - 2024. All rights reserved.