0

I am using cloudbuild.yml file.

I am trying to grab the build output from inside the cloud build and push it to a file. This is how my step looks like:

- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
  args: ['gcloud', 'builds', 'log', '$BUILD_ID', '>buildlog.log']
  id: 'fetch-build-log'

This throws me an error saying ERROR: (gcloud.builds.log) unrecognized arguments: >buildlog.log

If I execute that command in cloud shell, it works fine: gcloud builds log xxxxx-xxxx-xxxx-xxxx-xxxxxxx >guildlog.log

I am not sure why cloud build considers >buildlog.log an argument when it is to redirect the output to the file.

Am I missing something here or is there another way of doing it?

Asdfg
  • 11,362
  • 24
  • 98
  • 175

2 Answers2

1

At the shell prompt, when you run the command:

gcloud builds logs XXXX > buildlog.log

The actual command you are running is

gcloud builds log XXXX

with the additional instruction to the shell that you want any output from the command to be re-directed to a local file. The gcloud binary (the application that you are actually running) isn't passed the trailing > buildlog.log and, if it were, would give you the error message you reported. It is the shell that is interpreting the file output redirection.

What I think you want to do is simply remove the > buildlog.log from your arguments.

Since you are no longer passing in this parameter, the next question becomes "Where is the output from the command going?" ... the answer to that should be GCP Cloud Logging where you should be able to see the output from the commands.

Should you really want to create a local file of output, consider using the Cloud Build entrypoint:

https://cloud.google.com/cloud-build/docs/build-config#entrypoint

and specify a value for that of bash. This should start a shell and then pass your parameters to the shell rather than a raw fork/exec.

Finally, it appears that your question may be a variant of:

How can I save google cloud build step text output to file

Kolban
  • 13,794
  • 3
  • 38
  • 60
1

In Cloud Build each builder has a default entrypoint, which typically correlates to that builder’s purpose.

In your example, you are using cloud-sdk default entrypoint and the positional args syntax, so each index should be a single argument.

That's why you receive the error: ERROR: (gcloud.builds.log) unrecognized arguments: >buildlog.log

I put together a working example changing the entrypoint to /bin/bash:

steps:
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
  entrypoint: '/bin/bash'
  args: ['-c',
         'gcloud builds log $BUILD_ID > buildlog.log']
  id: 'fetch-build-log'
- name: 'alpine'
  id: 'OUTPUT_LOG'
  args: ['sh',
         '-c',
         'cat /workspace/buildlog.log']

In that example I'm using the -c command, in case you want to understand why:

Quoting from man bash:

   -c string If the -c option is present,  then  commands  are  read  from
             string.   If  there  are arguments after the string, they are
             assigned to the positional parameters, starting with $0.

Let me know if it works for you.

mesmacosta
  • 466
  • 3
  • 10