简体   繁体   English

Jenkinsfile / Docker:指向“npm”私有注册表

[英]Jenkinsfile / Docker : pointing to a "npm" private registry

I'm using a Jenkinsfile to build and publish node packages.我正在使用 Jenkinsfile 来构建和发布节点包。 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 ).它适用于默认注册表 ( https://registry.npmjs.org )。 But if a package contains a .npmrc file pointing to a private registry (reachable through a browser) then it fails.但是,如果一个包包含一个指向私有注册表(可通过浏览器访问)的.npmrc文件,那么它将失败。

How can I point to a private register through the docker container generated by the Jenkinsfile?如何通过 Jenkinsfile 生成的 docker 容器指向私有寄存器?

Thanks a lot:)非常感谢:)

Solution here: https://docs.npmjs.com/docker-and-private-modules解决方案在这里: 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 .基本上,您需要配置 npm 环境才能在npm install之前调用私有注册表。 On your machine you would do something like npm login which is interactive and not suited for docker builds!在你的机器上你会做一些像npm login这样的交互,但不适合 docker 构建!

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.如果上述代码也不起作用,您始终可以在根级别的 docker 容器中创建自己的 custom.npmrc 文件。 This will definitely make sure that all the other npm registries are accessible inside the docker container.这肯定会确保所有其他 npm 注册表都可以在 docker 容器内访问。

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"运行 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.到你的 docker 文件中,如果它也需要安装私有包,它将安装。

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.添加传递你的 GIT_ACCESS_TOKEN(你可以在你的 github 设置帐户中设置适当的权限)在你构建图像的地方。 Like docker build --build-arg GIT_ACCESS_TOKEN=yourtoken -t imageNameAndTag.像 docker build --build-arg GIT_ACCESS_TOKEN=yourtoken -t imageNameAndTag。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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