简体   繁体   English

从 Jenkins(在 Docker 图像中)访问 BitBucket 存储库

[英]Access from Jenkins(in Docker image) to BitBucket repository

Our team is trying for days to get this accessbut we arre tottally stucked on how todo.我们的团队已经尝试了好几天才能获得此访问权限,但我们完全不知道该怎么做。

Scenario:设想:

  • Jenkins installed in remote AWS machine that has access only through specific VPN address Jenkins 安装在只能通过特定 VPN 地址访问的远程 AWS 机器中
  • Normal BitBucket account with access ro repositories using SSH keys使用 SSH 密钥访问 ro 存储库的普通 BitBucket 帐户

Where I stuck?我卡在哪儿了?

At very initial part to get access to directory, following the steps:在开始访问目录的部分,请按照以下步骤操作:

  1. Created keys with linux ssh keygen command and put public key on Bitbucket repository Security Settings.使用 linux ssh keygen命令创建密钥并将公钥放入 Bitbucket 存储库安全设置。

  2. after logged in Jenkins I start a new task,then Freestyle build and Go登录后 Jenkins 我开始一个新任务,然后是自由式构建和 Go

  3. Inside task I hit "Settings" go to Source code configurations, select git and put the repository as: git@bitbucket.org:my_company/my_repo.git在任务中,我点击“设置”go 到源代码配置,select git 并将存储库设置为:git@bitbucket.org:my_company/my_repo.git

  4. On credentials I click on Add/Jenkins: and configure "Kind" as "SSH Username with private key", write my Username, in Private Key click on Enter Directly and paste my Private SSH key in field, Finish clicking in ADD.在凭据上,我单击添加/詹金斯:并将“Kind”配置为“带私钥的 SSH 用户名”,写入我的用户名,在私钥中单击直接输入并将我的私钥 SSH 粘贴到字段中,完成单击添加。

  5. I choose the new credential created...我选择创建的新凭据...

What's the problem?有什么问题?

get RED error message - and can't access BitBucket repo:收到红色错误消息 - 并且无法访问 BitBucket 存储库:

Failed to connect to repository : Command "git ls-remote -h -- git@bitbucket.org:cryptoblock/cblock1.git HEAD" returned status code 128:
stdout:
stderr: No RSA host key is known for bitbucket.org and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

What I've tried?我试过什么?

  • I tried to change many times the generated keys我试图多次更改生成的密钥

  • I tried to put the private key at machine that hosts docker container with jenkins我试图将私钥放在托管 docker 容器和 jenkins 的机器上

  • I entered on container with jenkins and put private key at /root/.ssh directory我使用 jenkins 进入容器并将私钥放在 /root/.ssh 目录中

  • I entered on container with jenkins logged dave sudo permissions to jenkins user and put private key at ~/.ssh directory我进入容器 jenkins 登录 dave sudo 权限到 jenkins 用户并将私钥放在 ~/.ssh 目录

  • find this answer that says to put command找到这个说放置命令的答案

 jenkins@jenkins_host $ ssh-keyscan -H bitbucket.com >> ~/.ssh/known_hosts

But I receive answer: bash: /var/jenkins_home/.ssh/known_hosts: Permission denied even trying as sudo ( changing sudoers file)但我收到答案: bash: /var/jenkins_home/.ssh/known_hosts: Permission denied even try as sudo (changing sudoers file)

  • some another answeers I found is related to https: access method, and in repo we use at company is only permitted ssh access我发现的其他一些答案与 https 有关:访问方法,在我们在公司使用的 repo 中只允许 ssh 访问

What works?什么有效?

  • On this remote machine where docker is installed, shh keys was rightly accepted as I'm able to git clone this repository normally, same as I do in my private machine and in all cases I've used same ssh private key.在安装了 docker 的这台远程机器上,shh 密钥被正确接受,因为我能够 git 正常克隆这个存储库,就像我在我的私人机器上所做的一样,并且在所有情况下我都使用相同的 ssh 私钥。

After some research on @mzm answer I build out a solution following this steps:在对@mzm 的回答进行一些研究后,我按照以下步骤构建了一个解决方案:

1 - Create ssh key-pair with ssh-keygen 1 - 使用ssh-keygen创建 ssh 密钥对

  • a) private key goes to Jenkins > Manage Credentials >kind: SSH key a) 私钥转到 Jenkins > Manage Credentials >kind: SSH key
  • b) public key (.pub) goes to Bitbucket Repository Security settings b) 公钥 (.pub) 转到 Bitbucket 存储库安全设置

Then:然后:

2 - Install Docker with Jenkins and run its docker 2 - 安装 Docker 和 Jenkins 并运行其 docker

3 - on host (where docker is running), execute: 3 - 在主机上(运行 docker 的地方),执行:

  • a) docker ps ( get container ID) a) docker ps (获取容器 ID)
  • b) docker exec -u 0 -it <ID> /bin/bash b) docker exec -u 0 -it <ID> /bin/bash
  • c) apt update c) apt update
  • d) apt install sudo d) apt install sudo
  • e) apt install nano e) apt install nano
  • f) sudo visudo f) sudo visudo
  • g) in file that will open insert this line: jenkins ALL=(ALL) NOPASSWD:ALL g) 在将打开的文件中插入此行: jenkins ALL=(ALL) NOPASSWD:ALL
  • h) get out of jenkins docker as root: exit h) 以 root 身份退出 jenkins docker: exit

check jenkins docker ID, with docker ps检查 jenkins docker ID, docker ps

4 - Enter in jenkins docker with command exec -it <ID> /bin/bash and execute in order: 4 - 使用命令exec -it <ID> /bin/bash输入 jenkins docker 并按顺序执行:

  • a) copy console output a) 复制控制台 output
  • b) create folder: mkdir /var/jenkins_home/.ssh b) 创建文件夹: mkdir /var/jenkins_home/.ssh
  • c) enter in folder: cd /var/jenkins_home/.ssh c) 进入文件夹: cd /var/jenkins_home/.ssh
  • d) create file: touch known_hosts d) 创建文件: touch known_hosts
  • e) put result from scan into created file: ssh-keyscan -H bitbucket.org > known_hosts e) 将扫描结果放入创建的文件中: ssh-keyscan -H bitbucket.org > known_hosts

After that should be possible to jenkins connect through ssh with Bitbucket.之后应该可以将 jenkins 通过 ssh 与 Bitbucket 连接起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM