Arun Gupta
Arun Gupta

Reputation: 4035

Create a new Google Cloud project using gcloud

As per the documentation at https://cloud.google.com/sdk/gcloud/reference/init gcloud init myproject command does not work.

google-cloud> gcloud init myproject Initialized gcloud directory in [/Users/arungupta/workspaces/google-cloud/myproject/.gcloud]. Cloning [https://source.developers.google.com/p/myproject/r/default] into [default]. Cloning into '/Users/arungupta/workspaces/google-cloud/myproject/default'... fatal: remote error: Repository not found. You may need to create a repository for this project using the Source Code tab at https://console.developers.google.com ERROR: Command '['git', 'clone', 'https://source.developers.google.com/p/myproject/r/default', '/Users/arungupta/workspaces/google-cloud/myproject/default', '--config', 'credential.helper=gcloud.sh']' returned non-zero exit status 128 ERROR: Unable to initialize project [myproject], cleaning up [/Users/arungupta/workspaces/google-cloud/myproject]. ERROR: (gcloud.init) Unable to initialize project [myproject].

Creating a project using gcloud init minecraft-server --project minecraft-server-183 creates the project with the name minecraft-server-183.

The project so created is then not visible at https://console.developers.google.com/project.

What is the correct gcloud command to create a new project, without going to the console?

Upvotes: 11

Views: 6241

Answers (4)

Eagnir
Eagnir

Reputation: 489

Just wanted to complete the circle here.

Google Cloud CLI tool 'gcloud' supports creating of projects without the need for the 'alpha' component installed from the version 147.0.0 (March 15, 2017) onwards.

Official Reference Link: https://cloud.google.com/sdk/gcloud/reference/projects/create

Release Notes for v147.0.0: https://cloud.google.com/sdk/docs/release-notes#14700_2017-03-15

It is mentioned under subheading of Google Cloud Resource Manager

For quick reference

Synopsis

gcloud projects create [PROJECT_ID] [--no-enable-cloud-apis] [--folder=FOLDER_ID] [--labels=[KEY=VALUE,…]] [--name=NAME] [--organization=ORGANIZATION_ID] [--set-as-default] [GCLOUD_WIDE_FLAG …]

Description

Creates a new project with the given project ID. By default, projects are not created under a parent resource. To do so, use either the --organization or --folder flag.

Sample Code

gcloud projects create example-foo-bar-1 --name="Happy project" --labels=type=happy

Upvotes: 2

Lak
Lak

Reputation: 4166

Here's a script that will create a project that is editable by a user (for many reasons, such as for auditability of service accounts, you might want to create per-user projects):

#!/bin/bash
if [ "$#" -lt 3 ]; then
   echo "Usage:  ./create_projects.sh billingid project-prefix  email1 [email2 [email3 ...]]]"
   echo "   eg:  ./create_projects.sh 0X0X0X-0X0X0X-0X0X0X learnml-20170106  [email protected] [email protected]"
   exit
fi
ACCOUNT_ID=$1
shift
PROJECT_PREFIX=$1
shift
EMAILS=$@
gcloud components update
gcloud components install alpha
for EMAIL in $EMAILS; do
   PROJECT_ID=$(echo "${PROJECT_PREFIX}-${EMAIL}" | sed 's/@/-/g' | sed 's/\./-/g' | cut -c 1-30)
   echo "Creating project $PROJECT_ID for $EMAIL ... "
   # Create project
   gcloud alpha projects create $PROJECT_ID
   # Add user to project
   gcloud alpha projects get-iam-policy $PROJECT_ID --format=json > iam.json.orig
   cat iam.json.orig | sed s'/"bindings": \[/"bindings": \[ \{"members": \["user:'$EMAIL'"\],"role": "roles\/editor"\},/g' > iam.json.new
   gcloud alpha projects set-iam-policy $PROJECT_ID iam.json.new
   # Set billing id of project
   gcloud alpha billing accounts projects link $PROJECT_ID --account-id=$ACCOUNT_ID
done

Explanation of the script is on medium: https://medium.com/google-cloud/how-to-automate-project-creation-using-gcloud-4e71d9a70047#.t58mss3co and a github link to the above code (I'll update it to remove the alpha when it goes beta/GA, for example) is here: https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/blogs/gcloudprojects/create_projects.sh

Upvotes: 1

JasonS
JasonS

Reputation: 7733

Update: as of 10/24/2016 @poolie says the gcloud command mentioned in Stephen's answer is now publicly accessable, will leave this answer here as I give some other usage suggestions.


I also have the problem, and was extremely discouraged by @Stephan Weinberg's remark, but I noticed when doing gcloud init that it asks where to put a "default" repository. so I looked at that one's config and see that it's slightly different from what's documented.

try pushing to https://source.developers.google.com/p/YOUR-PROJECT-NAME/r/default instead, it worked for me!

Upvotes: 0

Stephen Weinberg
Stephen Weinberg

Reputation: 53398

It is now possible with the gcloud alpha projects create command.

For more information see: https://cloud.google.com/resource-manager/

Upvotes: 8

Related Questions