简体   繁体   中英

Jenkinsfile / Docker : pointing to a "npm" private registry

I'm using a Jenkinsfile to build and publish node packages. After the usual checkout step, I use the following code to install my components.

node {
    docker.image('node').inside {
        sh 'npm install'
    }
}

It works fine with the default registry ( https://registry.npmjs.org ). But if a package contains a .npmrc file pointing to a private registry (reachable through a browser) then it fails.

How can I point to a private register through the docker container generated by the Jenkinsfile?

Thanks a lot:)

Solution here: https://docs.npmjs.com/docker-and-private-modules

Basically, you need to configure your npm environment to be able to call the private registry before npm install . On your machine you would do something like npm login which is interactive and not suited for docker builds!

Basically, you need access to the outside network. And for that please try using the following code -

node {
docker.image('node').inside("--net='bridge' -u root") {
    sh 'npm install'
   }
}

If the above code also doesn't work, you can always create your own custom.npmrc file within the docker container at the root level. This will definitely make sure that all the other npm registries are accessible inside the docker container.

node {
docker.image('node').inside("--net='bridge' -u root") {
       def data = "${npmrc}" //your custom npmrc data
       writeFile(file: '.npmrc', text: data) //create a new file named .npmrc
       sh 'npm install'
   }
}

After spending one week I found some how reasonable way to do. just add

RUN git config --global url."https://${GIT_ACCESS_TOKEN}@github.com".insteadOf "ssh://git@github.com"

into your docker file and it will install if it needs to install private packages as well.

add pass your GIT_ACCESS_TOKEN (you can have it in your github settings account with setting proper permissions) where you are building your image. Like docker build --build-arg GIT_ACCESS_TOKEN=yourtoken -t imageNameAndTag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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