Cloud Source Repositories を使用して、リモートの Google Cloudリポジトリをローカルの Git リポジトリに追加できます。
始める前に
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
- Git をインストールします。
-
Google Cloud CLI をインストールします。 インストール後、次のコマンドを実行して Google Cloud CLI を初期化します。
gcloud init
外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。
リポジトリをリモートとして追加する
現在の作業ディレクトリを、ローカル Git リポジトリを含むディレクトリに変更します。
cd [REPO_DIRECTORY]
ここで、
[REPO_DIRECTORY]
はリポジトリへのパスです。Linux または macOS で、以下の認証情報ヘルパー スクリプトを実行します。
Linux または macOS
git config --global credential.'https://source.developers.google.com'.helper gcloud.sh
Windows
git config --global credential.https://source.developers.google.com.helper gcloud.cmd
認証情報ヘルパー スクリプトは、Git が Google アカウント認証情報を使用して Cloud Source Repositories に安全に接続するために必要な情報を提供します。この安全な接続を確立するために追加の構成(
ssh
認証鍵のアップロードなど)を行う必要はありません。Cloud Source Repositories でリポジトリを作成します。
gcloud source repos create [REPO_NAME]
ここで、
[REPO_NAME]
は Google Cloud リポジトリの名前です。リポジトリをリモートとして追加します。
git remote add google \ https://source.developers.google.com/p/[PROJECT_ID]/r/[REPO_NAME]
ここで
[PROJECT_ID]
は Google Cloud プロジェクト ID です。[REPO_NAME]
は Google Cloud リポジトリの名前です。
リポジトリをリモートとして使用する
Google Cloud リポジトリは、多機能の Git リポジトリです。Git コマンドの標準セットを使用して、このようなリポジトリ(push
、pull
、clone
、log
など)を操作できます。
Google Cloud リポジトリに push する
ローカルの Git リポジトリから Google Cloud リポジトリに push するには、次のコマンドを入力します。
git push google master
Google Cloud リポジトリから pull する
Google Cloud リポジトリからローカルの Git リポジトリに pull するには、次のコマンドを入力します。
git pull google master
Google Cloud リポジトリの commit 履歴を表示する
commit の履歴を表示するには、次のコマンドを入力します。
git log google/master
静的認証情報の生成
gcloud CLI にアクセスできない場合があります。たとえば、これらのツールにアクセスできないビルドサーバーで作業する場合などです。このような状況では、静的な認証情報を手動で生成できます。
Google Cloud コンソールで、Cloud Source Repositories を開きます。
クローンを作成するリポジトリの上にポインタを置きます。
[クローン] add_box をクリックします。
認証オプションを含むダイアログが開きます。
[手動で生成した認証情報] タブをクリックし、手順に沿ってリポジトリのクローンを作成します。
次のステップ
ソースファイルを Cloud Source Repositories に push したら、 Google Cloud コンソール内からリポジトリ ファイルを表示できます。