从Azure CI访问私有Bitbucket仓库

huangapple go评论79阅读模式
英文:

Accessing private bitbucket repo from Azure CI

问题

在我的 package.json 文件中,我有一些依赖项来自私有的 Bitbucket 存储库。当我使用 Azure CI 运行 npm install 时,我会收到以下错误信息。

> npm ERR! An unknown git error occurred npm ERR! command git
> --no-replace-objects ls-remote ssh://git@bitbucket.org/projkl/class-rr-icons.git npm ERR!
> git@bitbucket.org: Permission denied (publickey). npm ERR! fatal:
> Could not read from remote repository.

这是因为我们没有提供访问私有 Bitbucket 存储库的权限。

所以我所做的是:

我使用以下命令生成了一个 SSH 令牌:ssh-keygen -t rsa -b 4096 -C "email@email.com"

id_rsa.pub 文件的内容已添加到 Bitbucket 存储库的 SSH keys 中。

然后,我将秘密密钥添加到 Azure CI 作为一个安全变量。

我的 Azure-pipeline.yaml 如下所示。

trigger:
- mybranch

variables:
  sshkey: $(key)

jobs:
- job: Job_1
  pool:
    vmImage: windows-latest
  steps:


  - script: |
      echo "$(sshkey)" > "$(Agent.HomeDirectory)\\.ssh\\id_rsa"
      echo "Host bitbucket.org" > "$(Agent.HomeDirectory)\\.ssh\\config"
      echo "  StrictHostKeyChecking no" >> "$(Agent.HomeDirectory)\\.ssh\\config"      

  - script: choco install git -y

  - script: npm install

我收到的错误是 The system cannot find the path specified.,我认为它无法在 $(Agent.HomeDirectory) 中找到安装 SSH 文件。如何解决这个问题?

英文:

In my package.json file, I have few dependency comes from a private bitbucket repo. When I use azure CI to npm install, i get the following error.

> npm ERR! An unknown git error occurred npm ERR! command git
> --no-replace-objects ls-remote ssh://git@bitbucket.org/projkl/class-rr-icons.git npm ERR!
> git@bitbucket.org: Permission denied (publickey). npm ERR! fatal:
> Could not read from remote repository.

This is due to the fact that we are not providing any permissions to access the private bitbucket repo.

So what I did is:

I generated a SSH tokens using the command ssh-keygen -t rsa -b 4096 -C "email@email.com"

The content of the id_rsa.pub was added in SSH keys in Bitbucket repository.

Then I added the secret key in Azure CI, as a secure variable.

My Azure-pipeline.yaml looks like the following.

trigger:
- mybranch

variables:
  sshkey: $(key)

jobs:
- job: Job_1
  pool:
    vmImage: windows-latest
  steps:


  - script: |
      echo "$(sshkey)" > "$(Agent.HomeDirectory)\\.ssh\\id_rsa"
      echo "Host bitbucket.org" > "$(Agent.HomeDirectory)\\.ssh\\config"
      echo "  StrictHostKeyChecking no" >> "$(Agent.HomeDirectory)\\.ssh\\config"

  - script: choco install git -y

  - script: npm install

The error I get is The system cannot find the path specified. and I think it can;t install the SSH file at $(Agent.HomeDirectory). How can I solve this issue ?

答案1

得分: 1

我尝试了一种替代方法来运行从私有Bitbucket存储库导入的package.jsonnpm install命令,请参考以下内容:

我创建了一个Bitbucket私有存储库,并在其中添加了如下所示的package.json文件:

从Azure CI访问私有Bitbucket仓库

从Azure CI访问私有Bitbucket仓库

我的package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

现在,我参考了Hugh Lin在Stack Overflow上的SO主题答案git - clone private BitBucket repo in Azure DevOps - Stack Overflow,以将私有Bitbucket存储库导入到我的Azure Devops存储库中。

我访问了这个链接https://bitbucket.org/account/settings/app-passwords/,并创建了一个具有存储库和项目的完全权限的应用程序密码。在同一设置中,我复制了我的用户名,如下所示:

从Azure CI访问私有Bitbucket仓库

生成App密码后,复制它并保存以供以后参考。

从Azure CI访问私有Bitbucket仓库

现在,我将Bitbucket私有存储库导入到Azure Devops中,如下所示:

从下面复制Https克隆URL:

从Azure CI访问私有Bitbucket仓库

在用户名中添加Bitbucket用户名(来自帐户设置)和App密码,然后克隆存储库。

从Azure CI访问私有Bitbucket仓库

在成功克隆Bitbucket存储库到Azure Devops存储库后,创建一个启动管道并运行包含npm install的yaml脚本,如下所示:

代码1:

trigger:
- master

pool:
  vmImage: windows-latest

steps:
- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
    npm install 
    npm audit fix --force    

输出:

从Azure CI访问私有Bitbucket仓库

通过安装Node工具的代码2:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'windows-latest'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '14.x'
  displayName: 'Install Node.js'

- script: |
        npm install
  displayName: 'Install dependencies'

输出:

从Azure CI访问私有Bitbucket仓库

至于您的错误消息,它指示Azure CI无法访问私有Bitbucket存储库,除了在Bitbucket私有存储库中添加SSH密钥之外,还需要在运行管道时在Azure Devops变量中添加该密钥。

此外,检查$(Agent.HomeDirectory)在您的计算机上是否存在,因为$(Agent.HomeDirectory)是一个预定义的路径,根据此文档,它位于c:\agent中。

还要修改您的脚本以正确且完整的路径指向SSH密钥,如下所示:

echo "$(sshkey)" > "$HOME/.ssh/id_rsa"

确保SSH密钥也设置为Azure Devops管道中的变量。

您的完整代码应如下所示:

trigger:
- mybranch

variables:
  sshkey: $(key)

jobs:
- job: Job_1
  pool:
    vmImage: windows-latest
  steps:
  - script: |
      echo "$(sshkey)" > "$HOME/.ssh/id_rsa"
      echo "Host bitbucket.org" >> "$HOME/.ssh/config"
      echo "  StrictHostKeyChecking no" >> "$HOME/.ssh/config"      

  - script: choco install git -y

  - script: npm install
英文:

I tried an alternative method to run npm install on the package.json imported from private bitbucket repository, Refer below:-

I created one bitbucket private repository and added package.json file inside it like below:-

从Azure CI访问私有Bitbucket仓库

从Azure CI访问私有Bitbucket仓库

My package.json:-


{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-scripts": "5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Now, I referred this SO thread answer by Hugh Lin git - clone private BitBucket repo in Azure DevOps - Stack Overflow to import a private bitbucket repository in my Azure Devops repo.

I went to this link https://bitbucket.org/account/settings/app-passwords/ and created one app password with full permissions given on repository and project. And in the same settings, I copied my Username from Account settings section like below:-

从Azure CI访问私有Bitbucket仓库

Copy the App password after it is generated and save it for later reference.

从Azure CI访问私有Bitbucket仓库

Now, I imported the bitbucket private repository to Azure Devops like below:-

Copy the Https clone url from below:-

从Azure CI访问私有Bitbucket仓库

In Username add the Bitbucket Username from Account settings and Password from App password and clone the repository.

从Azure CI访问私有Bitbucket仓库

After the bitbucket repository is cloned successfully in azure Devops repo, Create a starter pipeline and run this yaml script with npm install like below:-

Code 1:-

trigger:
- master

pool:
  vmImage: windows-latest

steps:
- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
    npm install 
    npm audit fix --force    

Output:-

从Azure CI访问私有Bitbucket仓库

Code 2 by installing node tool:-

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'windows-latest'

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '14.x'
  displayName: 'Install Node.js'

- script: |
        npm install
  displayName: 'Install dependencies'

Output:-

从Azure CI访问私有Bitbucket仓库

Coming to Your error message indicates that Azure CI does not have access to private Bitbucket repository, Along with adding ssh key in bitbucket private repository, You also need to add the key in Azure Devops variable while running the pipeline.

Also, Check the path for $(Agent.HomeDirectory) exist in your machine, As $(Agent.HomeDirectory) is a predefined path, It is located in c:\agent as per this documentation

Also, modify your script to the correct and full path to the ssh key like below:-

echo  "$(sshkey)" > "$HOME/.ssh/id_rsa"

Make sure the ssh key is also set as a variable in azure devops pipeline.

Your complete code should look like below:-

trigger:
- mybranch

variables:
  sshkey: $(key)

jobs:
- job: Job_1
  pool:
    vmImage: windows-latest
  steps:
  - script: |
      echo "$(sshkey)" > "$HOME/.ssh/id_rsa"
      echo "Host bitbucket.org" > "$HOME/.ssh/config"
      echo "  StrictHostKeyChecking no" >> "$HOME/.ssh/config"      

  - script: choco install git -y

  - script: npm install

huangapple
  • 本文由 发表于 2023年7月10日 15:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定