0

I am trying to build a common YAML file for both push and tag-based triggers. I can build two YAML files and pass them to the triggers setting page in Google cloud build, but I want to have a common file for both of them.

In the push trigger, the YAML consists of unit testing and in the tag-based trigger, the YAML consists of unit testing and containerizing the application to be pushed to the artifact registry. As YAML is a text-formatting language and if/else conditions cannot be incorporated in it, how can I do this in one single file?

If you want any other information, please let me know and looking forward to help, thanks in advance

A_the_kunal
  • 59
  • 2
  • 8

1 Answers1

1

You are right, YAML doesn’t support conditional statements, but you can use GitLab to help you parse if statements as mentioned in If condition is possible in YAML file. With the help of this you will be able to add the respective code for Push and tag trigger in the same file.

Zeenath S N
  • 1,100
  • 2
  • 8
  • Yeah you are right @Zeenath. But our organization is using GitHub only, so I guess I have no other way but to make separate files. Thank you for your time – A_the_kunal Feb 09 '22 at 19:02
  • Hi, I think you can still use if in the yaml file as shown in the [Google Cloud Build conditional step](https://stackoverflow.com/a/58239148/15774177) and [Is this the correct way to write if-else in cloudbuild.yaml](https://stackoverflow.com/a/62004745/15774177), let me know if this works. – Zeenath S N Feb 10 '22 at 07:35
  • Thank you so much for these resources. I am trying these and will let you know – A_the_kunal Feb 10 '22 at 10:15
  • So I am working in this repository https://github.com/Athe-kunal/cloud-build-samples/tree/v1.0.2/quickstart-automate. Here you can see the cloudbuild_common.yaml file which has the trigger instructions for both tags and push-based trigger. Can you please tell me what is going wrong here? – A_the_kunal Feb 10 '22 at 11:35
  • Are you receiving any error? or what's not working in this? – Zeenath S N Feb 10 '22 at 11:49
  • Here, it is not going inside the if loops. It is just executing the initial name line and then terminating. – A_the_kunal Feb 10 '22 at 11:50
  • Were you able to go through the repo and find what is wrong? – A_the_kunal Feb 11 '22 at 10:29
  • Hi, I wasn't able to locate where have you gone wrong, it should work as mentioned in [Running Inline bash script](https://cloud.google.com/build/docs/configuring-builds/run-bash-scripts#running_inline_bash_scripts). As an alternative you can also try writing your code in separate bash script and calling it in .yaml file as mentioned in [Running bash script](https://cloud.google.com/build/docs/configuring-builds/run-bash-scripts#running_bash_scripts_on_disk). – Zeenath S N Feb 16 '22 at 11:25
  • Thank you Zeenath, this helped me a lot in – A_the_kunal Feb 24 '22 at 11:15