Search code examples
google-cloud-platformcommand-line-interfacegcloud

How do I get `gcloud app deploy --format=json` to output _only_ JSON?


When putting together a script that makes a series of calls to the gcloud utility, it's pretty helpful to add the --format=json flag so that the output is easily machine-readable.

Problem is: gcloud app deploy --format=json --quiet is not machine-readable. It outputs a bunch of logging information in plaintext before outputting the JSON result. Clearly, I can parse the output to find the JSON, but I'm hoping there's a way to limit it to just the JSON output.

Here's what I'm getting (expurgated for privacy)

❯ gcloud app deploy --project=$PROJECT --format=json --verbosity=none --quiet
Services to deploy:

descriptor:                  [/path/to/app.yaml]
source:                      [/path/to/project]
target project:              [project-id]
target service:              [default]
target version:              [20240130t124159]
target url:                  [https://project-id.uk.r.appspot.com]
target service account:      [project-id@appspot.gserviceaccount.com]


Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 0 files to Google Cloud Storage                ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [default]...done.                                                                                 
Setting traffic split for service [default]...done.                                                                
Stopping version [project-id/default/20240130t124016].
Sent request to stop version [project-id/default/20240130t124016]. This operation may take some time to complete. If you would like to verify that it succeeded, run:
  $ gcloud app versions describe -s default 20240130t124016
until it shows that the version has stopped.
Deployed service [default] to [https://project-id.uk.r.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse --project=project-id
{
  "configs": [],
  "versions": [
    {
      "environment": null,
      ...

I've played with the --verbosity=none flag, and had no impact. I tried it without the --quiet flag (in which case you need to pipe yes into it), but that had no impact.

Something I'm missing? Some trick that someone knows?


Solution

  • On the output you see the stdout and the stderr. I had this problem before and I found that the useful part is writing in the stdout, and the overhead stuff in the stderr (I didn't remember how I discovered that!)

    So, if you use this command to write the stderr to NULL, you can keep only the useful part

    gcloud app deploy --format=json 2>/dev/null