文章目录
- 基于shell脚本对阿里云npm仓库(https://packages.aliyun.com)登录认证
- 食用人群
- 食用方式
基于shell脚本对阿里云npm仓库(https://packages.aliyun.com)登录认证
食用人群
由于一些安全的原因,某些企业可能会基于阿里云搭建的私有npm仓库,那么,在脱离阿里云体系部署代码的情况下,通过shell脚本进行自动化部署就会存在私有仓库登录认证问题,本文即给出“食用”说明。
食用方式
话不多说,直接上代码:
# 仓库地址
registry="https://packages.aliyun.com/xxx/npm/npm-registry/" #注意这里xxx要根据实际情况改成私有仓库信息
# 用户名
username="" #这里需输入
# 密码
password="" #这里需输入 params='{"name": "'$username'", "password": "'$password'" }'echo "登录认证参数:$params"loginResult=$(curl -s \-H "Accept: application/json" \-H "Content-Type:application/json" \-X PUT --data "$params" \"$registry-/user/org.couchdb.user:$username"
)tokenVal=$loginResult
if [[ $tokenVal == *token* ]]
then
# if [[ $tokenVal == *token* ]]; thentokenVal=${tokenVal#*token\":\"}tokenVal=${tokenVal/\"\}/} #不要怀疑,这里就是这么用的
elseecho "login fail!"echo $loginResult
fiauthRegistryPath=${registry/https:\/\//}
npm set registry $registry
npm set //$authRegistryPath:_authToken=$tokenVal #这里即设置带有登录认证仓库地址,后续会从这个仓库下载各种包echo "token:$tokenVal"
echo "npm仓库及认证地址://$authRegistryPath:_authToken=$tokenVal"
参考:npm/aliyun仓库自动登录脚本shell命令 jenkins 无法交互输入用户名和密码