0

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 ?

Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140

1 Answers1

1

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:-

enter image description here

enter image description here

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:-

enter image description here

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

enter image description here

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

Copy the Https clone url from below:-

enter image description here

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

enter image description here

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:-

enter image description here

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:-

enter image description here

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
SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11