0

In a GitHub Workflow I can define a strategy key and loop through all combinations of a matrix. Here is an example for a CI pipeline of a Node.js app.

name: CI

on:
  pull_request:

jobs:
  test:
    strategy:
      matrix:
        node: [16, 14]
        os: [ubuntu-latest, macos-latest, windows-latest]

    runs-on: ${{ matrix.os }}
    name: Test node@${{ matrix.node }} on ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci
      - run: npm test

Can I achieve the same thing in a cloudbuild.yaml file? I haven't found any mention of this looping functionality in the documentation regarding the Build configuration file schema.

I guess I could achieve what I want using user-defined substitutions and calling the same Cloud Build config file multiple times, passing different substitutions each time... but I was wondering if this is the only possible approach. I would rather have all configuration defined in that single cloudbuild.yaml.

jackdbd
  • 4,583
  • 3
  • 26
  • 36
  • 1
    To my knowledge, no you can't. Cloud Build jobs are a linear series of steps; there's no conditionals nor looping. Your suggestion is correct, you would need to execute the loop outside of Cloud Build. Or you could define a step that does the looping but this step would then have to combine the looping behavior too; the result would be to effectively obscure the workflow within a single step which isn't ideal. It may (never tried) be possible to combine Cloud Workflows with Cloud Build to more elegantly solve this. – DazWilkin May 22 '22 at 15:51
  • Using a GCP Workflow is a cool idea, but the bummer is that you have to invoke it manually. You can't define a Cloud Build trigger that calls the workflow. Instead, you can define multiple triggers using the same cloudbuild.yaml file and different combinations of substitution variables. – jackdbd May 22 '22 at 16:09
  • 1
    I haven't used Workflows so I'm on thin ice.... Why can't you invoke the Workflow from a Cloud Build step? It's a little recursive but Cloud Build is triggered, invokes Workflow, invokes Cloud Build – DazWilkin May 22 '22 at 16:30
  • 1
    ah, that would do the trick I think. I had not thought about a recursive solution. Looks like you can also run workflow steps in parallel with experimental.executions.map. https://cloud.google.com/workflows/docs/reference/stdlib/experimental.executions/map – jackdbd May 22 '22 at 17:22
  • Something is cooking on Cloud Build team side. Say tuned! – guillaume blaquiere May 22 '22 at 20:12
  • @guillaumeblaquiere Any update on doing this natively in Cloud Build? Thanks! – Spain Train Jul 07 '23 at 15:56
  • 1
    I've been testing it in the Alpha for 1 month now. There are some things to fix but it's promising. Maybe more at Cloud Next 2023, late in august. – guillaume blaquiere Jul 07 '23 at 18:37

0 Answers0