Halaman ini menjelaskan cara mengelola modul Go yang dikemas dan disimpan di Artifact Registry.
Sebelum memulai
- Jika repositori target tidak ada, buat repositori baru. Pilih Go sebagai format repositori.
- Verifikasi bahwa Anda memiliki izin yang diperlukan untuk repositori.
- (Opsional) Konfigurasi default untuk perintah gcloud.
- Instal Go 1.15 atau yang lebih baru.
Instal add-on gcloud CLI package-go-module:
gcloud components install package-go-module
Konfigurasi Go untuk mengautentikasi dengan Artifact Registry.
Peran yang Diperlukan
Untuk mendapatkan izin yang diperlukan untuk mengelola modul, minta administrator untuk memberi Anda peran IAM berikut di repositori:
-
Melihat modul:
Pembaca Artifact Registry (
roles/artifactregistry.reader) -
Mendownload atau menginstal modul:
Pembaca Artifact Registry (
roles/artifactregistry.reader) -
Menambahkan modul ke repositori:
Penulis Artifact Registry (
roles/artifactregistry.writer) -
Menghapus modul:
Administrator Repositori Artifact Registry (
roles/artifactregistry.repoAdmin)
Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.
Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran kustom atau peran yang telah ditentukan lainnya.
Mengupload modul
Mode repositori: standar
Untuk mengemas dan mengupload modul ke repositori Anda, jalankan perintah berikut:
gcloud artifacts go upload --project=PROJECT \
--repository=REPOSITORY \
--location=LOCATION \
--module-path=MODULE_PATH \
--version=VERSION \
--source=SOURCE_LOCATION
Ganti kode berikut:
- PROJECT dengan Google Cloud project ID Anda.
- REPOSITORY dengan nama repositori tempat paket disimpan.
- LOCATION dengan lokasi regional atau multi-regional repositori.
- MODULE_PATH dengan jalur modul. Misalnya:
example.com/fooLihat Referensi Modul Go untuk mengetahui informasi selengkapnya. - VERSION dengan
versi semantik
modul dalam bentuk
vX.Y.ZdenganXadalah versi utama,Yadalah versi minor, danZadalah versi patch. - SOURCE_LOCATION dengan jalur ke direktori root modul Go Anda. Jika Anda menghilangkan tanda
--source, defaultnya adalah direktori saat ini.
Modul diupload ke Artifact Registry.
Untuk mengetahui informasi selengkapnya tentang cara membuat modul Go, lihat tutorial ini.
Mengupload versi modul baru
Mode repositori: standar
Untuk mengupload versi baru modul ke project, repositori, dan lokasi default saat nilai default dikonfigurasi, jalankan perintah berikut dengan nomor versi baru:
gcloud artifacts go upload \
--module-path=MODULE_PATH \
--version=VERSION \
--source=SOURCE_LOCATION
Ganti VERSION dengan versi modul yang diperbarui. Misalnya, untuk mengupload modul versi 0.1.1 dengan jalur example.com/foo, jalankan perintah berikut:
gcloud artifacts go upload \
--module-path=example.com/foo \
--version=v0.1.1 \
--source=SOURCE_LOCATION
Untuk menandai modul sebagai versi pra-rilis, tambahkan tanda hubung setelah VERSION dan tambahkan ID pra-rilis modul Anda:
gcloud artifacts go upload \
--module-path=MODULE_PATH \
--version=VERSION-PRE_RELEASE_IDENTIFIERS \
--source=SOURCE_LOCATION
Ganti PRE_RELEASE_IDENTIFIERS dengan karakter alfanumerik ASCII dan tanda hubung yang dipisahkan dengan titik. Misalnya, untuk mengupload pra-rilis modul dengan
jalur modul example.com/foo yang diidentifikasi oleh alpha.x.12m.5, jalankan
perintah berikut:
gcloud artifacts go upload \
--module-path=example.com/foo \
--version=v1.0.0-alpha.x.12m.5 \
--source=SOURCE_LOCATION
Mengupload versi utama baru
Mode repositori: standar
Versi utama tidak kompatibel dengan versi sebelumnya. Untuk mencegah
pengguna mengimpor perubahan yang menyebabkan gangguan, versi utama setelah v1 harus memiliki
jalur modul yang berbeda dari versi sebelumnya. Mulai dari v2, versi utama ditambahkan ke akhir jalur modul.
Misalnya, jalur modul untuk v2.0.0 dari example.com/foo adalah
example.com/foo/v2.
Praktik terbaik yang direkomendasikan adalah mengembangkan versi utama setelah v1 di
direktori terpisah yang dinamai sesuai dengan akhiran versi utamanya.
Untuk mengupload versi utama baru 2.0.0 modul dengan jalur
example.com/foo ke project, repositori, dan lokasi default saat
nilai default dikonfigurasi:
gcloud artifacts go upload --module-path=example.com/foo/v2 --version=v2.0.0
Mencantumkan modul
Mode repositori: standar, jarak jauh, virtual
Jalankan perintah berikut untuk memeriksa modul Go yang diupload di project, repositori, dan lokasi default saat nilai default dikonfigurasi:
gcloud artifacts packages list
Outputnya akan terlihat seperti berikut:
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
Melihat detail versi modul
Mode repositori: standar, jarak jauh, virtual
Jalankan perintah berikut untuk melihat versi modul di project, repositori, dan lokasi default saat nilai default dikonfigurasi:
gcloud artifacts versions list --package=MODULE_PATH
Outputnya akan terlihat seperti berikut:
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
Menggunakan modul sebagai dependensi
Mode repositori: standar, jarak jauh, virtual
Untuk mengimpor modul yang disimpan di Artifact Registry, Anda perlu menginstruksikan Go untuk mencari dependensi dari Artifact Registry dan melewati database checksum. Ikuti petunjuk untuk menyiapkan autentikasi dan lingkungan Go di Menyiapkan autentikasi untuk Go.
Jika Anda menggunakan kredensial berumur pendek untuk mengautentikasi ke Artifact Registry, Anda harus memperbarui token OAuth dengan menjalankan perintah berikut:
GOPROXY=proxy.golang.org \ go run github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@latest refresh
Jika modul Anda belum memiliki file
go.mod, ubah direktori ke folder modul Anda dan jalankango mod inituntuk membuat filego.modbagi paket Anda.go mod init MODULE_PATH
Ganti MODULE_PATH dengan jalur modul untuk modul yang disimpan di Artifact Registry. Lihat Referensi Modul Go untuk mengetahui informasi selengkapnya.
Untuk mewajibkan versi modul Anda yang disimpan di Artifact Registry, edit file
go.modAnda agar menyerupai berikut ini:module example.com/bar go 1.19 require example.com/foo v0.1.0Ganti kode berikut:
example.com/fooadalah jalur modul dari modul yang diperlukanv0.1.0adalah versi yang disimpan di Artifact Registry
Sertakan jalur modul seperti biasa di bagian
importpada filemain.goAnda.Misalnya, untuk mengimpor modul dengan jalur
example.com/fooyang disimpan di Artifact Registry, bagian impor Anda mungkin menyerupai berikut:package main import ( foo "example.com/foo" ) func main() { ... }Jalankan
go mod tidyuntuk mendownload dependensi:go mod tidy
Jalankan modul seperti biasa:
go run .
Modul yang disimpan di Artifact Registry akan didownload dan digunakan sebagai dependensi.
Menghapus modul Go yang dipaketkan
Mode repositori: standar, jarak jauh
Anda dapat menghapus paket dan semua versinya, atau menghapus versi tertentu.
- Setelah menghapus paket, Anda tidak dapat mengurungkan tindakan tersebut.
Before you delete a package or package version, verify that any you have communicated or addressed any important dependencies on it.
To delete a package:
Console
Open the Repositories page in the Google Cloud console.
In the repository list, click the appropriate repository.
The Packages page lists the packages in the repository.
Select the package that you want to delete.
Click DELETE.
In the confirmation dialog box, click DELETE.
gcloud
Run the following command:
gcloud artifacts packages delete PACKAGE \
[--repository=REPOSITORY] [--location=LOCATION] [--async]
Replace the following:
PACKAGEis the name of the package in the repository.REPOSITORYis the name of the repository. If you configured a default repository, then you can omit this flag to use the default.-
LOCATIONis the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.
The --async flag causes the command to return immediately,
without waiting for the operation in progress to complete.
To delete versions of a package:
Console
Open the Repositories page in the Google Cloud console.
In the repository list, click the appropriate repository.
The Packages page lists the packages in the repository.
Click a package to view versions of that package.
Select versions that you want to delete.
Click DELETE.
In the confirmation dialog box, click DELETE.
gcloud
Run the following command:
gcloud artifacts versions delete VERSION \
--package=PACKAGE \
[--repository=REPOSITORY] [--location=LOCATION] \
[--async]
Replace the following:
VERSIONis the name of the version to delete.PACKAGEis the name of the package in the repository.REPOSITORYis the name of the repository. If you configured a default repository, then you can omit this flag to use the default.-
LOCATIONis the regional or multi-regional location of the repository. Use this flag to view repositories in a specific location. If you configured a default location, then you can omit this flag to use the default.
The --async flag causes the command to return immediately,
without waiting for the operation in progress to complete.
Langkah berikutnya
- Coba Panduan Memulai Go.
- Mendownload file individual yang terkait dengan modul atau versi tanpa menggunakan perintah Go.