在 Artifact Registry 中儲存 Go 模組
設定私人 Artifact Registry Go 存放區,上傳模組至其中,並將模組用做依附元件。
事前準備
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 安裝 Go 1.15 以上版本。
- 安裝
package-go-module
gcloud CLI 外掛程式:
gcloud components install package-go-module
建立存放區
如要建立及設定新的存放區,請按照下列步驟操作:
執行下列指令來建立新的存放區。
gcloud artifacts repositories create REPOSITORY \ --repository-format=go \ --location=LOCATION \ --description=DESCRIPTION
更改下列內容:
執行下列指令,查看存放區詳細資料。
gcloud artifacts repositories describe --location=LOCATION REPOSITORY
輸出結果會與下列內容相似:
Encryption: Google-managed key Repository Size: 0.000MB createTime: '2022-06-03T20:20:01.644813Z' format: GO mode: STANDARD_REPOSITORY name: projects/my-project/locations/us-west1/repositories/my-repo updateTime: '2022-06-03T20:20:01.644813Z'
設定 gcloud 指令的預設值
您可以設定預設值,簡化專案、存放區和位置值的 gcloud CLI
指令。設定預設值後,就不需要使用 --project
、--location
和 --repository
標記。
封裝及上傳 Go 模組
package-go-module
gcloud CLI 外掛程式會為您的 Go 模組建立套件,讓您使用 gcloud
指令進行版本化,並上傳至 Artifact Registry。
建立 Go 模組
首先,建立簡單的 Go 模組,並上傳至存放區。
在主目錄中,為模組建立名為「foo」的目錄
mkdir foo
將目錄變更為模組的目錄,然後執行
go mod init
,為模組建立 go.mod 檔案。cd foo \ go mod init example.com/foo
將
example.com/foo
替換為模組路徑。詳情請參閱 Go 模組參考資料。在 foo 目錄中建立含有以下內容的
foo.go
檔案:package foo const HelloWorld = "Hello World!"
封裝及上傳模組
封裝模組並上傳至存放區:
gcloud artifacts go upload --project=PROJECT \ --repository=REPOSITORY \ --location=LOCATION \ --module-path=example.com/foo \ --version=VERSION \ --source=SOURCE_LOCATION
更改下列內容:
- PROJECT 替換為您的 Google Cloud 專案 ID。
- REPOSITORY 與儲存套件的存放區名稱。
- LOCATION 與存放區的區域或多區域位置。
example.com/foo
與模組路徑。詳情請參閱 Go 模組參考資料。- VERSION 與模組的語意版本,格式為
vX.Y.Z
,其中X
為主要版本,Y
為次要版本,Z
為修補版本。 - SOURCE_LOCATION 與 Go 模組根目錄的路徑。如果省略
--source
旗標,預設會是目前的目錄。
模組已上傳至 Artifact Registry。
如要進一步瞭解如何建立 Go 模組,請參閱這份教學課程。
列出模組
執行下列指令,在設定預設值時,檢查預設專案、存放區和位置中的上傳 Go 模組:
gcloud artifacts packages list
輸出結果會與下列內容相似:
Listing items under project my-project, location us-west1, repository my-repo. PACKAGE CREATE_TIME UPDATE_TIME example.com/foo 2022-06-03T20:43:39 2022-06-20T20:37:40
查看模組版本詳細資料
執行下列指令,即可在設定預設值時,查看預設專案、存放區和位置中的模組版本:
gcloud artifacts versions list --package=MODULE_PATH
輸出結果會與下列內容相似:
Listing items under project my-project, location us-west1, repository my-repo, package example.com/foo. VERSION DESCRIPTION CREATE_TIME UPDATE_TIME v0.1.0 2022-06-03T20:43:39 2022-06-03T20:43:39 v0.1.1 2022-06-20T20:37:40 2022-06-20T20:37:40
下載模組
如要匯入儲存在 Artifact Registry 中的模組,您必須指示 Go 從 Artifact Registry 尋找依附元件,並略過總和檢查資料庫。
設定 Go 環境
指示 Go 從 Artifact Registry、公開的 Go 模組 Proxy 和來源依序下載模組:
export GOPROXY=https://LOCATION-go.pkg.dev/PROJECT/REPOSITORY,https://proxy.golang.org,direct
更改下列內容:
排除模組,以免使用公開檢查碼資料庫進行檢查:
export GONOSUMDB=MODULE_PATH_REGEX
如果您想排除多個模組,請將 MODULE_PATH_REGEX 替換為模組路徑或規則運算式。
如要排除使用公開檢查和總和檢查資料庫檢查的模組
example.com/foo
,請執行下列指令:export GONOSUMDB=example.com/foo
如果您希望所有模組路徑以
example.com
開頭的模組都不會使用公開的總和檢查碼資料庫進行檢查,請執行下列指令:export GONOSUMDB=example.com/*
向 Artifact Registry 進行驗證
當您下載已封裝的 Go 模組,以便從 Artifact Registry 做為依附元件使用時,Go 二進位檔會使用 netrc 檔案中的憑證,向 Artifact Registry 進行驗證。為簡化驗證程序,您可以使用 Go 憑證輔助程式,重新整理 netrc 檔案中的符記,以便向 Artifact Registry 進行驗證。
您可以使用 netrc 環境變數設定 netrc 檔案的位置。如果未設定 NETRC
變數,go
指令會在類 UNIX 平台上讀取 $HOME/.netrc
,在 Windows 上讀取 %USERPROFILE%\_netrc
。
Artifact Registry 支援下列驗證方法。
- 短期憑證 (建議)
- 使用 Artifact Registry Go 憑證輔助程式工具更新 netrc 檔案中的驗證權杖。
- 使用服務帳戶金鑰
如果無法在環境中使用憑證進行驗證,請使用這個選項。將未加密的服務帳戶金鑰新增至 netrc 檔案。
將 Go 憑證輔助程式新增至 GONOPROXY
使用 Go 憑證輔助程式前,您必須將其新增至 GONOPROXY
清單,強制 Go 直接從 GitHub 下載。如果您有其他模組想直接從來源下載,可以使用逗號分隔的清單加入這些模組,如以下範例所示:
export GONOPROXY=MODULE_PATH1, MODULE_PATH2
其中 MODULE_PATH1 和 MODULE_PATH2 是從來源下載的模組模組路徑。
如要將 Go 憑證輔助工具新增至 GONOPROXY
清單,並執行該工具來設定憑證:
將 Go 憑證輔助程式新增至
GONOPROXY
export GONOPROXY=github.com/GoogleCloudPlatform/artifact-registry-go-tools
執行下列指令,使用 Go 模組套件工具將 Artifact Registry 憑證新增至 netrc 檔案:
GOPROXY=proxy.golang.org \ go run github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@v0.4.0 \ add-locations --locations=LOCATION \ [--json_key=path/to/service/account/key.json]
其中 LOCATION 是存放區的地區或多地區位置。如要新增多個地點,請以半形逗號分隔輸入清單。
Go 憑證輔助程式會在 netrc 檔案中新增設定,以便向 Artifact Registry 進行驗證。如果您傳遞
--json_key
標記,系統就會將金鑰加入 netrc 檔案,以便進行密碼驗證。
將模組用做依附元件
如果您使用短效憑證向 Artifact Registry 進行驗證,就必須執行下列指令來重新整理 OAuth 權杖:
GOPROXY=proxy.golang.org \ go run github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@v0.4.0 refresh
在主目錄中,建立名為「bar」的目錄
mkdir bar
將目錄變更為模組的目錄,然後執行
go mod init
,為套件建立go.mod
檔案。cd bar \ go mod init example.com/bar
將
example.com/bar
替換為模組路徑。詳情請參閱 Go 模組參考資料。如要要求儲存在 Artifact Registry 中的 foo 版本,請將
go.mod
檔案編輯為類似以下內容:module example.com/bar go 1.19 require example.com/foo v0.1.0
更改下列內容:
example.com/foo
是必要模組的模組路徑v0.1.0
是儲存在 Artifact Registry 中的版本
在
bar
目錄中建立含有下列內容的main.go
檔案:package main import ( "fmt" foo "example.com/foo" ) func main() { fmt.Println(foo.HelloWorld) }
執行 go mod tidy 下載依附元件,包括 foo 套件:
go mod tidy
執行酒吧模組:
go run .
輸出結果會與下列內容相似:
Hello World!
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取您在本頁所用資源的費用,請按照下列步驟操作。
如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。移除存放區之前,請先確認要保留的模組均已存放於其他位置。
如要刪除存放區,請按照下列步驟操作:
gcloud artifacts repositories delete \ --location=LOCATION \ --project=PROJECT \ REPOSITORY
更改下列內容:
如要移除您為使用中的 gcloud 設定建立的預設存放區和位置設定,請執行下列指令:
gcloud config unset artifacts/repository gcloud config unset artifacts/location
後續步驟
- 進一步瞭解如何設定驗證方法。
- 瞭解如何管理存放區。
- 瞭解如何管理 Go 模組。
- 參閱DevOps相關資源,並探索開發運作研究和評估研究計畫。