If/Else condition in .yml file bitbucket

NitinKhandelwal February 23, 2021

Hi , 

 

Can anybody help me with a code snippet fot if else condition in email notiufy pipe. I am trying something like 

after-script:          
echo $BITBUCKET_EXIT_CODE         
if [ BITBUCKET_EXIT_CODE == 0 ]; then  echo "Step Successful"; fi; 
-  
pipeatlassian/email-notify:0.4.2              
variables:USERNAME'test@test.com'                
PASSWORD$PASSWORD                
FROM'test@test.com'                
TO$EMAILRECEIVERS               
HOST'smtp.gmail.com'               
BODY_PLAINEmail send from Bitbucket Pipeline <a href='https://bitbucket.org/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/addon/pipelines/home#!/results/${BITBUCKET_BUILD_NUMBER}'>build#${BITBUCKET_BUILD_NUMBER}</a> . The deployment is successful.               
DEBUG'true'               
if [ BITBUCKET_EXIT_CODE != 0 ]; then  echo "Step failed"; fi;          
-  
pipeatlassian/email-notify:0.4.2              
variables               
USERNAME'test@test.com'                
PASSWORD$PASSWORD                
FROM'test@test.com'                
TO$EMAILRECEIVERS               
HOST'smtp.gmail.com'               
BODY_PLAINEmail send from Bitbucket Pipeline <a href='https://bitbucket.org/${BITBUCKET_WORKSPACE}/${BITBUCKET_REPO_SLUG}/addon/pipelines/home#!/results/${BITBUCKET_BUILD_NUMBER}'>build#${BITBUCKET_BUILD_NUMBER}</a> . The deployment failed.               
DEBUG'true'

Both the if condition is getting fired. 

2 answers

2 votes
Oleksandr Kyrdan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 26, 2021

Hi @NitinKhandelwal  ,

Thank you for your question!

Let's simplify the code with an additional variable BUILD_STATUS:

after-script:
- echo $BITBUCKET_EXIT_CODE
- BUILD_STATUS="successful"
- if [[ $BITBUCKET_EXIT_CODE -ne 0 ]]; then BUILD_STATUS="failed" ; fi
- echo "Step ${BUILD_STATUS}"
- pipe: atlassian/email-notify:0.4.3
variables:
USERNAME: 'test@test.com'
PASSWORD: $PASSWORD
FROM: 'test@test.com'
TO: $EMAILRECEIVERS
HOST: 'smtp.gmail.com'
BODY_PLAIN: "Email send from Bitbucket Pipeline ... The deployment is ${BUILD_STATUS}."
DEBUG: 'true'

More recipes you could find by following the tag recipe  in the Atlassian Community.


Cheers,
Oleksandr Kyrdan

1 vote
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 26, 2021

Hi @NitinKhandelwal ,

The reason that both pipes are executed is because they are not part of the if conditions, but a separate command in the after-script. All commands in a script or after-script are executed sequentially (unless one fails and the build then fails).

That being said, I'm afraid that it is not possible to place a pipe within an if condition. The reason for this is that bash can't really recognize what a Pipe is, since it's something our team has implemented specifically for Pipelines, it's not something that bash would recognize.

We have a feature request for the ability to condition the execution of a pipe:

I would suggest to add your vote (by selecting the Vote for this issue link) as the number of votes helps the development team and product managers better understand the demand for new features. You are more than welcome to leave any feedback, and you can also add yourself as a watcher (by selecting the Start watching this issue link) if you'd like to get notified via email on updates.

Implementation of new features is done as per our policy here https://confluence.atlassian.com/support/implementation-of-new-features-policy-201294576.html and any updates will be posted in the feature request.

Please feel free to let me know if you have any questions.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events