Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Get error message during build

Attila Berczik May 10, 2022

I am trying to send a Google Chat message with the status of the build once it finishes, particularly I am interested in the error message.

 

I am building in Bitbucket Pipelines, and I have found the afterscript for that. 

 

              script:

                  - echo "run!"

              after-script:

                  - chmod +x build/after.sh

                  - build/after.sh

With the $BITBUCKET_EXIT_CODE environment I can determine whether the build was successful, and I want to access the logs. Based on this answer I was able to build this code. 

 

PIPELINE_UUID=$(echo "${BITBUCKET_PIPELINE_UUID}"| cut -c 2-37)

STEP_UUID=$(echo "${BITBUCKET_STEP_UUID}"| cut -c 2-37)

LOG_API=$(echo "https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_FULL_NAME}/pipelines/$PIPELINE_UUID/steps/$STEP_UUID/log")

curl --request GET --url "$LOG_API"
This creates an url like this, but I constantly receive an error.
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

100 61 100 61 0 0 1033 0 --:--:-- --:--:-- --:--:-- 1033

{"type": "error", "error": {"message": "Resource not found"}}

Screenshot 2022-05-10 201251.png

I am pretty sure that these are the correct details to access the logs, I would appreciate any help. ❤️


Otherwise I have thought about saving error messages as a file during the build, then save it as an artifact, so the after-script has access to it, but I found no way of doing this, a suggestion from this side could also help.

1 answer

1 accepted

0 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 12, 2022

Hello @Attila Berczik ,

Thank you for reaching out to Atlassian Community!

The reason why the API call to get the logs is failing is because the step is only completed after the Build teardown, which runs at the very end on each step. Before that, the logs will not be available in our backend and thus cannot be fetched from the API.

My suggestion in this case would be to output the logs of the commands you are executing in the step to a file by using the tee command :

command |& tee -a output.txt

The |& tee -a command will make both the standard output and standard error streams to be copied to the file while still being visible in the UI. If the file already exists, the new data will get appended to the end of the file.

The output.txt file can then be accessed in the after-script section, so you can POST it to an external endpoint. Please find an example YML file below :

image: atlassian/default-image:3

pipelines:
default:
- step:
script:
- echo "test log" |& tee -a output.txt
- echo "test log 2" |& tee -a output.txt
after-script:
- cat output.txt

Hope that helps! Let me know in case you have any questions related to this topic.

Thank you @Attila Berczik .

Kind regards,

Patrik S

Attila Berczik May 16, 2022

Thank you very much for your help, it helped tremendously.

 

Do you have an idea how could I use this command without distorting the exit code?

- echo "test log 2" |& tee -a output.txt

With this the errors get lost  and the Pipeline runs successfully all the time, I somehow have to return a 1 exit code.

 

I have thought about checking the file if it contains the error word, but it doesn't work in all cases.

if [[ $OUTPUT == *"ERROR"* ]]; then exit 1; fi

 

Could you help me with this too?

Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events