arrow_back

Create a Go Artifact Registry and Upload Code

ログイン 参加
700 以上のラボとコースにアクセス

Create a Go Artifact Registry and Upload Code

ラボ 30分 universal_currency_alt クレジット: 1 show_chart 入門
info このラボでは、学習をサポートする AI ツールが組み込まれている場合があります。
700 以上のラボとコースにアクセス

gem-artifact-registry-go

Google Cloud self-paced labs logo

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Overview

In this lab, you will learn how to create an Artifact Registry repository for Go packages and upload a sample Go module to it. This allows you to manage and share Go packages within your Google Cloud environment. This lab focuses on using Artifact Registry for container images.

Task 1. Create an Artifact Registry Repository

First, you'll create a new Artifact Registry repository to store your Go packages. Choose a name and region for your repository.

  1. Enable the Artifact Registry API.
gcloud services enable artifactregistry.googleapis.com Note:
This command enables the Artifact Registry API for your project.
  1. Set your Project ID is:
gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
This command sets your active project identity.
  1. Set your default region to
gcloud config set compute/region {{{ project_0.default_region | "REGION" }}} Note:
This command sets your active compute region.
  1. Create a new Artifact Registry repository. Ensure you are using a supported region ().
gcloud artifacts repositories create my-go-repo \ --repository-format=go \ --location={{{ project_0.default_region | "REGION" }}} \ --description="Go repository" Note:
This command creates the Artifact Registry repository.
  1. Verify the repository was created successfully. Replace my-go-repo with your repository name.
gcloud artifacts repositories describe my-go-repo \ --location={{{ project_0.default_region | "REGION" }}} Note:
This command describes the Artifact Registry repository.

Task 2. Configure Go to Use Artifact Registry

Next, you'll configure your Go environment to use the newly created Artifact Registry repository.

  1. Set the GOPRIVATE environment variable. Replace with your Google Cloud project ID.
go env -w GOPRIVATE=cloud.google.com/{{{ project_0.project_id | "PROJECT_ID" }}} Note:
This command sets the GOPRIVATE environment variable.
  1. Configure go to authenticate with Artifact Registry. Replace with your repository's region and with your Google Cloud project ID.
export GONOPROXY=github.com/GoogleCloudPlatform/artifact-registry-go-tools GOPROXY=proxy.golang.org go run github.com/GoogleCloudPlatform/artifact-registry-go-tools/cmd/auth@latest add-locations --locations={{{ project_0.default_region | "REGION" }}} Note:
This command configures go to authenticate with Artifact Registry using application default credentials.

Task 3. Create a Sample Go Module

Now, let's create a simple Go module to upload to the Artifact Registry.

  1. Create a directory for your Go module. Replace hello with desired directory name.
mkdir hello cd hello Note:
This command creates the module directory and navigates into it.
  1. Initialize the Go module.
go mod init labdemo.app/hello Note:
This command initializes the Go module.
  1. Create a hello.go file with the following content:
package main import "fmt" func main() { fmt.Println("Hello, Go module from Artifact Registry!") } Note:
This code defines a simple Go program.
  1. Optional: Verify the go module builds
go build Note:
This command builds the go module.

Task 4. Set up Version control

Add version control information to the environment.

  1. Setup the user email
git config --global user.email {{{ user_0.username | "EMAIL" }}}
  1. Setup the user name
git config --global user.name cls
  1. Set the default branch naming as main
git config --global init.defaultBranch main
  1. Initialize a git repo
git init
  1. Add the files to the repository
git add .
  1. Add a commit message
git commit -m "Initial commit"
  1. Tag the files
git tag v1.0.0

Task 5. Upload the Go Module to Artifact Registry

Finally, you'll upload the Go module to your Artifact Registry repository.

  1. Upload the module to Artifact Registry using the go upload command. Replace with your repository's region, my-go-repo with your repository name.
gcloud artifacts go upload \ --repository=my-go-repo \ --location={{{ project_0.default_region | "REGION" }}} \ --module-path=labdemo.app/hello \ --version=v1.0.0 \ --source=. Note:
This command uploads the Go module to Artifact Registry.
  1. Verify the module has been pushed.
gcloud artifacts packages list --repository=my-go-repo --location={{{ project_0.default_region | "REGION" }}} Note:
This command lists Go packages in the Artifact Registry. You can verify that your package exists.

Congratulations!

You have successfully created an Artifact Registry repository for Go packages, configured your Go environment, and uploaded a sample Go module. This allows you to share and manage Go modules within your Google Cloud environment.

Additional Resources

Manual Last Updated Jun 23, 2025

Lab Last Tested Jun 23, 2025

始める前に

  1. ラボでは、Google Cloud プロジェクトとリソースを一定の時間利用します
  2. ラボには時間制限があり、一時停止機能はありません。ラボを終了した場合は、最初からやり直す必要があります。
  3. 画面左上の [ラボを開始] をクリックして開始します

シークレット ブラウジングを使用する

  1. ラボで使用するユーザー名パスワードをコピーします
  2. プライベート モードで [コンソールを開く] をクリックします

コンソールにログインする

    ラボの認証情報を使用して
  1. ログインします。他の認証情報を使用すると、エラーが発生したり、料金が発生したりする可能性があります。
  2. 利用規約に同意し、再設定用のリソースページをスキップします
  3. ラボを終了する場合や最初からやり直す場合を除き、[ラボを終了] はクリックしないでください。クリックすると、作業内容がクリアされ、プロジェクトが削除されます

このコンテンツは現在ご利用いただけません

利用可能になりましたら、メールでお知らせいたします

ありがとうございます。

利用可能になりましたら、メールでご連絡いたします

1 回に 1 つのラボ

既存のラボをすべて終了して、このラボを開始することを確認してください

シークレット ブラウジングを使用してラボを実行する

このラボの実行には、シークレット モードまたはシークレット ブラウジング ウィンドウを使用してください。これにより、個人アカウントと受講者アカウントの競合を防ぎ、個人アカウントに追加料金が発生することを防ぎます。