1. 程式人生 > 其它 >Azure DevOps Server 的連線源(Artifacts):八、npm釋出和安裝包

Azure DevOps Server 的連線源(Artifacts):八、npm釋出和安裝包

1. 概述

本文主要介紹如何基於Azure DevOps Server的Artifacts連線源,實現nodejs依賴包的下載、釋出管理;使用Azure DevOps Server的Artifacts模組作為企業內網的npm私服。

2. 前期準備

2.1 更改連線源

  • 方法一:
    安裝了node後,系統預設的連線源是npmjs.org ,需要我們手動將本地計算機的連線源設定為DevOps Server中的一個連線源。
    我們在這篇wiki《NPM 基礎知識》中介紹過如何修改開發人員本地計算機的npm連線源。只需要參考下面的命令更改連線源即可
C:\Users\zhang>npm config get registry
http://registry.npmjs.org/

C:\Users\zhang>npm config set registry http://ads2/DefaultCollection/_packaging/a1%40Local/npm/registry/

C:\Users\zhang>
  • 方法二:
    也可以使用文字編輯器,直接編輯使用者配置檔案.npmrc中的內容,這個檔案的路徑一般是C:\Users\zhang\.npmrc

2.2 配置連結源的許可權

  • 開啟DevOps Server中的連線源,並導航到npm的配置頁面,如下圖
  • 參考上面截圖中的指導,將身份驗證令牌的部分複製到本地計算機的使用者配置檔案中,檔案路徑一般是C:\Users\zhang.npmrc
  • 在DevOps Server中新建一個令牌
  • 參考連線源截圖中第二個箭頭指示的操作方法,將令牌轉換為base64編碼(主要目的是加強安全),並將編碼後的base64字元替換使用者配置檔案.npmrc中的[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]

通過本章節中的操作,以及配置好了本地計算機的連線源,並且配置了連線DevOps Server的認證資訊,下面就可以從DevOps下載依賴包,或者將依賴包釋出到DevOps Server。

3. 從Artifacts下載依賴包

3.1 將依賴包安裝到全域性目錄

例如,我們要從DevOps Server的連線源()中安裝express,並將其安裝到全域性目錄,使用命令

C:\Users\zhang>npm config get registry
http://ads2/DefaultCollection/_packaging/a1/npm/registry/

C:\Users\zhang>npm install express -g
+ [email protected]
updated 1 package in 1.733s


C:\Users\zhang>

當用戶從Azure DevOps Server中下載依賴包的過程中,如果DevOps Server中沒有對應版本的依賴包,DevOps Server會自動從上游源(http://registry.npmjs.org )中下載依賴包,並快取在DevOps Server的這個連線源中,如下圖:

注意,從上游源下載依賴包的前提,是DevOps Server的應用層可以通過網際網路連線到registry.npmjs.org

4. 將依賴包釋出到Artifacts

下面通過示例,建立一個簡單的npm包檔案,並將其釋出到DevOps Server的連線源中。

  1. 新建npm包
  • 新建npm包對應的資料夾D:\temp\bjgreat-soft
  • 執行npm init命令初始化一個專案
D:\temp\bjgreat-soft>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (bjgreat-soft)
version: (1.0.0) 1.0.1
description: 北京巨集軟科技有限公司(簡稱“巨集軟科技”)是一家專業從事軟體研發運維一體化服務(DevOps)和應用軟體生命週期管 理(ALM)服務的技術方案提供商。
entry point: (index.js)
test command:
git repository:
keywords: 巨集軟科技
author: 張洪君
license: (ISC)
About to write to D:\temp\bjgreat-soft\package.json:

{
  "name": "bjgreat-soft",
  "version": "1.0.1",
  "description": "北京巨集軟科技有限公司",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "巨集軟科技"
  ],
  "author": "張洪君",
  "license": "ISC"
}


Is this OK? (yes)

D:\temp\bjgreat-soft>

上面的命令會在當前目錄下,自動生成一個檔案package.json.

  1. 接下來就是編寫自己的程式,這裡省略;你還可以在依賴包中增加一個README.md的說明檔案,例如我在這裡隨意貼上了一些說明檔案,文字的格式是MarkDown語法;同時需要修改上面的package.json,將README.md新增到包中,檔案的內容如下:
{
  "name": "bjgreat-soft",
  "version": "1.0.1",
  "description": "北京巨集軟科技有限公司",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "巨集軟科技"
  ],
  "author": "張洪君",
  "license": "ISC",
  "files": [
    "README.md"
  ]
}
  1. 執行釋出命令npm publish
D:\temp\bjgreat-soft>npm publish
npm notice
npm notice package: [email protected]
npm notice === Tarball Contents ===
npm notice 320B  package.json
npm notice 1.6kB README.md
npm notice === Tarball Details ===
npm notice name:          bjgreat-soft
npm notice version:       1.0.4
npm notice package size:  1.2 kB
npm notice unpacked size: 1.9 kB
npm notice shasum:        b9658da9382d92ec0d339bfeb93e3153b497a8f4
npm notice integrity:     sha512-Q35+8/NnXyquL[...]dxcg0iTxsoZKQ==
npm notice total files:   2
npm notice
+ [email protected]

當上面的操作正常結束後,我們就可以在DevOps Server的Artifacts連線源中看到剛剛釋出的npm包:

5. 相關文章

如果需要,你還可以從微軟Azure DevOps Server 的線上文件,查詢更多的權威資料,也歡迎通過下面的聯絡方式與我溝通,相互學習,相互提高!


https://www.cnblogs.com/danzhang
Azure DevOps MVP 張洪君