10
votes

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?

3
gcloud init no longer takes a project-name argument. It just does general setup for the current user including authentication.poolie

3 Answers

1
votes

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  somebody@gmail.com someother@gmail.com"
   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

0
votes

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!