Issue
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://[email protected]/projkl/class-rr-icons.git npm ERR! [email protected]: 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 protected]"
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 ?
Solution
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:-
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:-
Copy the App password after it is generated and save it for later reference.
Now, I imported the bitbucket private repository to Azure Devops like below:-
Copy the Https clone url from below:-
In Username add the Bitbucket Username from Account settings and Password from App password and clone the repository.
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:-
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:-
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
Answered By - SiddheshDesai Answer Checked By - Gilberto Lyons (WPSolving Admin)