Osamah Aldoaiss
Osamah Aldoaiss

Reputation: 328

How can I use the package.json to pull private git-repository from a certain branch when using Jenkins?

We have a main project, that loads sub-projects and then bundles them into one file with npm. Here is the package.json:

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "angular": "^1.5.7"
  },
  "devDependencies": {
    "project-a": "git+ssh://[email protected]/project-a.git",
    "project-b": "git+ssh://[email protected]/project-b.git"
  },
  "author": "",
  "license": "ISC"
}

We want to build the project on multiple servers, e.g. master and develop and those are the names of the branches. Now if we build the develop-branch npm should pull from git+ssh://[email protected]/project-a.git#develop and git+ssh://[email protected]/project-b.git#develop. But I cant find a way to tell npm to pull those branches and not the master.

Any help is appreciated.

Upvotes: 0

Views: 320

Answers (1)

rasjani
rasjani

Reputation: 7970

First off, npm will pull what ever branch you tell it to pull. Format is like this:

"devDependencies": {
    "project-a": "git+ssh://[email protected]/project-a.git#branchName" 
}

And if there's no branch defined, it will default to master.

So, this gives a starting point that you need to modify your dependency at runtime by appending the name of the branch to the url. On possibility to archive this that before you run npm install, you process the package.json by a tool that changes keyword (for example #branchName) into what you want it to be. Very easy to archive w/ tool like sed

However, that will leave your git repo "dirty" (as in, unmodified changes). Another alternative is to write a cli tool which will read your package.json, extracts the dependencies and installs them manually by calling npm install $reporul and postfixing the url repo with the branch you want .. Maybe this repo provides a tool that you can use if you are not up to writing one yourself: https://github.com/CrowdArchitects/node-rebranch-dependencies

Upvotes: 2

Related Questions