solution-NPM私有库搭建

npm管理系统基于 verdaccio(https://verdaccio.org/)

官方提供了docker版本的环境配置https://github.com/verdaccio/docker-examples

1、安装docker docker-compose

运维同学负责安装。(ps:建议去根据个字官网的说明去安装

2、克隆verdaccio的docker配置

git clone https://github.com/verdaccio/docker-examples.git

公司认证是通过ldap设置的,因此采用ldap相关配置

1
2
3
4
5
6
7
8
9
10
11
目录说明:

conf:为verdaccio配置文件目录

docker/ldap/diff_files :ldap相关文件,没有用到

storage:模块存储目录,我们发布的模块都会存储到本文件夹下面

verdaccio-ldap: doker build脚本,docker镜像安装完成后,会执行此脚本,安装启动必要的verdaccio环境

docker-compose.yaml: docker-compose配置文件

进入目录,编辑配置文件

cd ldap-verdaccio

chmod -R 777 conf storage # 更改这俩个目录的权限, 755应该也可以。不给执行写权限的话,在我们进行 npm publish的时候,会返回403

vim 配置文件

3、修改配置文件

a、 docker-compose.yaml 为docker-compose工具的配置文件,由于我们采用公司内部的ldap因此需要删掉该文件ldap相关的配置。

最终配置文件为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
version: '2'

services:
verdaccio:
container_name: verdaccio-ldap-1
build: verdaccio-ldap/
ports:
- "4873:4873"
volumes:
- "./storage:/verdaccio/storage"
- "./conf:/verdaccio/conf"
volumes:
verdaccio:
driver: local

b、conf/config.yaml 文件为verdaccio配置文件

配置文档:https://verdaccio.org/docs/en/configuration

auth字段由运维同学配置

packages字段,将第一个正则匹配改为下面的内容
其中key值为包名正则,access为访问权限,publish为发布权限,proxy为代理服务器

1
2
3
4
5
6
'@scope/*':

# scoped packages
access: marpontes zach leonardo
publish: $authenticated
proxy: npmjs

====>

1
2
3
4
5
6
'@xc/*':

# scoped packages
access: $all
publish: $authenticated
proxy: npmjs

最终配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
storage: /verdaccio/storage

auth:
ldap:
type: ldap
client_options:
url: "ldap://最右的ladap"
# Only required if you need auth to bind
adminDn: "cn=admin,dc=example,dc=org"
adminPassword: "admin"
# Search base for users 用户配置
searchBase: "ou=People,dc=example,dc=org"
searchFilter: "(cn={{username}})"
# If you are using groups, this is also needed
groupDnProperty: 'cn'
groupSearchBase: 'ou=Groups,dc=example,dc=org'
# If you have memberOf support on your ldap
searchAttributes: ['*', 'memberOf']
# Else, if you don't (use one or the other):
# groupSearchFilter: '(memberUid={{dn}})'
#
# Optional, default false.
# If true, then up to 100 credentials at a time will be cached for 5 minutes.
cache: false
# Optional
reconnect: true

# a list of other known repositories we can talk to
uplinks:
npmjs:
url: https://registry.npmjs.org/

packages:
'@xc/*':
# scoped packages
access: $all
publish: $authenticated
proxy: npmjs
'@*/*':
# scoped packages
access: $all
publish: $authenticated
proxy: npmjs
'**':
# allow all users (including non-authenticated users) to read and
# publish all packages
#
# you can specify usernames/groupnames (depending on your auth plugin)
# and three keywords: "$all", "$anonymous", "$authenticated"
access: $all

# allow all known users to publish packages
# (anyone can register by default, remember?)
publish: $authenticated

# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs

# log settings
logs:
- {type: stdout, format: pretty, level: trace}
#- {type: file, path: verdaccio.log, level: info}

listen:
- 0.0.0.0:4873

4、启动docker

docker-compose up –force-recreate –build

docker会自动拉取镜像,进行配置编译。完成之后

访问ip:4873即可看到前端页面

5、重新改动配置文件
改动配置文件之后

docker-compose restart 即可重启