Google team comes with this intro for Google Shell:
When you start Cloud Shell, it provisions a e2-small Google Compute Engine virtual machine running a Debian-based Linux operating system. Cloud Shell instances are provisioned on a per-user, per-session basis. The instance persists while your Cloud Shell session is active; after an hour of inactivity, your session terminates and its VM, discarded. For more on usage quotas, refer to the limitations guide.
The tutorial for today solve this simple issue: use Google Shell to create and run a C# project;
Let’s start with this Google webpage.
You can see all your projects.
Google uses some notations into command output.
For example, this PROJECT_ID refers to your project identification.
I will use in the same way these notations to see how these changes appear:
1 2 3 | ACCOUNT_NAME - account name ACCOUNT_EMAIL - account email CLOUD_CONFIGURATION - cloud configuration |
Open your Cloud Shell console by selecting this icon from the top of the open webpage:
You can see in the bottom of this webpage a console interface with this message:
1 2 3 | Welcome to Cloud Shell! Type "help" to get started. To set your Cloud Platform project in this session use “gcloud config set project [PROJECT_ID]” ACCOUNT_NAME@cloudshell:~$ |
Let’s run the first command to show information about your accounts: gcloud auth list
1 2 3 4 5 6 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ gcloud auth list Credentialed Accounts ACTIVE ACCOUNT * ACCOUNT_EMAIL To set the active account, run: $ gcloud config set account `ACCOUNT` |
The next command will set your project, where the PROJECT_ID is your project id: gcloud config set project PROJECT_ID
1 2 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ gcloud config set project PROJECT_ID Updated property [core/project]. |
You can get info about the project with this command: gcloud config list project
1 2 3 4 5 6 7 8 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ gcloud config list project [core] project = PROJECT_ID Your active configuration is: [CLOUD_CONFIGURATION] gcloud config set project PROJECT_ID gcloud services enable run.googleapis.com |
You can unset the project with this command: gcloud config unset project
1 2 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ gcloud config unset project Unset property [core/project]. |
In this case, when you will run gcloud config list project command you will get an error:
1 2 3 | catalinfest@cloudshell:~$ gcloud config list project ERROR: (gcloud.config.list) The project property is set to the empty string, which is invalid. ... |
Let’s set again.
1 2 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ gcloud config set project PROJECT_ID ... |
The next step is to enable the Cloud Run API from Cloud Shell:
1 2 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ gcloud services enable run.googleapis.com ERROR: (gcloud.services.enable) FAILED_PRECONDITION: Billing must be enabled for activation of service '[run.googleapis.com, containerregistry.googleapis.com]' ... |
To enable billing account you need to follow these steps from the Google webpage.
Now, I can enable it:
1 2 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ gcloud services enable run.googleapis.com Operation "operations/..." finished successfully. |
I use dotnet command-line tool in Cloud Shell to create the first cloud project named charp_cloud_001.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ dotnet new web -o charp_cloud_001 Welcome to .NET Core 3.1! --------------------- SDK Version: 3.1.301 Telemetry --------- The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell. Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry ---------------- Explore documentation: https://aka.ms/dotnet-docs Report issues and find source on GitHub: https://github.com/dotnet/core Find out what's new: https://aka.ms/dotnet-whats-new Learn about the installed HTTPS developer cert: https://aka.ms/aspnet-core-https Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs Write your first app: https://aka.ms/first-net-core-app -------------------------------------------------------------------------------------- Getting ready... The template "ASP.NET Core Empty" was created successfully. Processing post-creation actions... Running 'dotnet restore' on charp_cloud_001/charp_cloud_001.csproj... Determining projects to restore... Restored /home/ACCOUNT_NAME/charp_cloud_001/charp_cloud_001.csproj (in 173 ms). Restore succeeded. |
Let’s go to the folder I created to see the project files:
1 2 3 | ACCOUNT_NAME@cloudshell:~ (PROJECT_ID)$ cd charp_cloud_001/ ACCOUNT_NAME@cloudshell:~/charp_cloud_001 (PROJECT_ID)$ls appsettings.Development.json appsettings.json charp_cloud_001.csproj obj Program.cs Properties Startup.cs |
These default project source code can be view with an editor tool.
I use the vim tool to see the Program.cs file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace charp_cloud_001 { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } } |
The next C# source code is from Startup.cs file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace charp_cloud_001 { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); } } } |
Now I can add and change these and other C# files to finish my project.
I f you run these source code with dotnet watch run or dotnet run commands you will get warn error.
I can see on my browser the Hello World! when I click on the output of the dotnet watch run command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | catalinfest@cloudshell:~/charp_cloud_001 (total-yen-281919)$ dotnet watch run watch : Started warn: Microsoft.AspNetCore.Server.Kestrel[0] Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'. warn: Microsoft.AspNetCore.Server.Kestrel[0] Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'. info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: /home/catalinfest/charp_cloud_001 |
This open in my browser a link like this: https://5000-x_____.europe-west4.cloudshell.dev/?authuser=0&environment_name=default
Another simple issue for this tutorial is the tools used on Cloud Shell:
If you don’t like the great vim editor, then you can use the Open Editor button.
Also, you can use mc command to open it and use it, see the next screenshot: