Adrien Lemaire
Adrien Lemaire

Reputation: 1894

How to access log files created by GCP cloud build steps?

My Cloud build fails with a timeout on npm test, and no useful information is sent to stdout. A complete log can be found in a file, but I couldn't find a way to ssh in the cloud build environment.

Already have image: node

> [email protected] test
> jest

ts-jest[versions] (WARN) Version 4.1.0-beta of typescript installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=3.8.0 <5.0.0-0). Please do not report issues in ts-jest if you are using unsupported versions.
npm ERR! path /workspace/v3/client
npm ERR! command failed
npm ERR! signal SIGTERM
npm ERR! command sh -c jest

npm ERR! A complete log of this run can be found in:
npm ERR!     /builder/home/.npm/_logs/2020-11-09T07_56_23_573Z-debug.log

Since I have no problem running the tests locally, I'd like to see the content of that 2020-11-09T07_56_23_573Z-debug.log file to hopefully get a hint at what might be wrong.

Is there a way to retrieve the file content ?

Upvotes: 1

Views: 558

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75745

I had a similar issue with error management on Gitlab CI and my workaround is inspired from there.

The trick is to embed your command in something that exit with a return code 0. Here an example

 - name: node
   entrypoint: "bash"
   args:
     - "-c"
     - |
          RETURN_CODE=$$(jtest > log.stdout 2>log.stderr;echo $${?})
          cat log.stdout
          cat log.stderr
          if [ $${RETURN_CODE} -gt 0 ]; 
          then 
            #Do what you want in case of error, like a cat of the files in the _logs dir
            # Break the build: exit 1
          else 
            #Do what you want in case of success. Nothing to continue to the next step
          fi

Some explanations:

  • echo $${?}: the double $ is to indicate to Cloud Build to not use substitution variables but to ignore it and let Linux command being interpreted.
  • The $? allows you to get the exit code of the previous command
  • Then you test the exit code, if > 0, you can perform actions. At the end, I recommend to break the build to not continue with erroneous sources.
  • You can parse the log.stderr file to get useful info in it (the log file for example)

Upvotes: 2

Related Questions