JaapH
JaapH

Reputation: 391

GCP: how to make sure that gcp project is set in Cloud Shell Editor

I am toying around a bit with GCP cloud shell editor which I start by visiting https://shell.cloud.google.com/?show=ide (which I installed as an PWA) and would like to debug a simple Flask AppEngine app. The app is using Cloud Logging. It runs fine in the terminal window when I first do:

gcloud config set project myproject

However if I want to use the builtin debugging I get errors because gcloud config set project myproject has not been set in that new window. So my question is how do I make sure that gcloud config set project myproject is run at startup of each console? I already tried running a preLaunchTask but since that is run in a separate console it does not help

Update (26 Jan 2021) Also toyed around with trying to change the .bashrc automatically when opening a project by using a task that is triggered by folderOpen and then writes the relevant gcloud command to a script which is called by .bashrcas shown below

{
    "version": "2.0.0",
    "tasks": [
      {
          "label": "Set GCP Project",
          "type": "shell",
          "command": "echo \"set -x; gcloud config set project [MYPROJECT]\" > $HOME/set_gcp_project",
          "runOptions": {
              "runOn": "folderOpen"
          },
          "presentation": {
              "reveal": "never",
              "panel": "shared"
          },
          "problemMatcher": []
      }
    ]
}

However Cloud Shell does not support these automated tasks. It works fine in VSCode. However it is still a nuisance because if in the GCP console I open a terminal or it opens it when you click on a run in cloud shell it sets the project to whatever project I had last opened in the Cloud Shell Editor

Upvotes: 0

Views: 818

Answers (1)

Daniel Ocando
Daniel Ocando

Reputation: 3764

If you are interested in customizing your Cloud Shell experience you can always refer to the following section of the documentation.

I guess the must straightforward to always guarantee that the gcloud config set project myproject is always run when a new window is opened will be to append that gcloud command at the end of the .bashrc file located at your $HOME directory.

Just open the Cloud Shell (make sure you are located at the $HOME directory, which is the default directory that opens up when you open your Cloud Shell) and append the gcloud command by running the following command:


[YOUR-USERNAME]@cloudshell:~ (myproject)$ echo "gcloud config set project myproject" >> .bashrc

Upvotes: 4

Related Questions