Cloud Build: How to execute two CI jobs, in order?

Viewed 186

I have set up a Cloud Build pipeline for deploying a Firebase web app.

The problem

The current script does unnecessary deployments. Even if backend files hadn't changed, it ends up testing and deploying the backend (takes 3 minutes; 4 minutes with backend tests).

Likewise for front-end (build and deploy only ~30s).

Context

GitHub / Cloud Build integration works so that Cloud Build is informed after the PR is already merged. That's fine.

I can make two separate CI jobs, filtered by the files that changed (this is available in Cloud Build console). The problem here is that if both change in the same commit, I would like the backend to be deployed first.

Come to think of it, I can bypass this by giving development guidance: if there are breaking changes (eg. a data model changes in the backend, or a new service such as Cloud Storage is added), those changes need to be deployed first in the backend. Making eg. two commits or deploying the backend manually.

What would you do??

I think the above side-thought is where I'm heading.

If you have experience with Cloud Build, how do you tackle this kind of cases?


Link changed to how the code was at the time of writing the question.


Added Jul'22: I ended up doing as pondered in the quote part: have separate triggers for "things changed in backend", "things changed in front-end". They may freely compete on which one gets deployed first; if it ever becomes an issue, the quoted manual process would work.

1 Answers

If you have dependencies between your CI jobs, you need to make them dependent to each others. In your case, you have 2 independent trigger: both are triggered on the same event, but each has their own pipeline and file filtering. And therefore, you both can be executed in parallel, and they aren't dependent.

The solution for that is to make the jobs dependent: start a job, and then, start the next one. In your case, you can trigger the backend pipeline on Github event. At the end (if there is something to build or not), you need to trigger the frontend pipeline.

You have several solutions for that:

  • PubSub message
  • GCloud CLI
  • WebHook

The main tradeoff with this solution is the sequentiality. If you have to build the back and the front, it will take 4 min + 3 min.

Related