Search code examples
gitlab-cipipeline

In the Gitlab CI pipeline, how can I conditionally run jobs in parallel?


Say, for example, my pipeline contains the following job:

sast-container:
  <<: *branches
  allow_failure: true
  parallel:
    matrix:
      - CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE/address-check-stub
      - CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE/manual-service-stub
      - CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE/employment-record-stub

and I want each of the jobs in the matrix if and only if there has been a change to the code that effects them.

I was thinking along the lines of something like this:

sast-container:
  <<: *branches
  allow_failure: true
  parallel:
    matrix:
      only:
        changes:
          - stub-services/address-check-stub      
            - CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE/address-check-stub
      only:
        changes:
          - stub-services/address-check-stub  
            - CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE/manual-service-stub
      only:
        changes:
          - stub-services/address-check-stub  
            - CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE/employment-record-stub

which, of course, to nobody's surprise (including my own), doesn't work.


Solution

  • came across your post looking for a similar situation.

    Here I post you what I did to make it work, hope it helps for your case:

    ...
    terraform format:
      stage: validate
      script:
        - gitlab-terraform fmt
      parallel:
        matrix:
          - TARGET: test
          - TARGET: qa
      environment:
        name: $TARGET
        action: verify
      rules:
        - changes:
            - terraform/$TARGET/**/*
    ...
    

    With that code, I manage to create specific jobs for a given env(TARGET) only if the terraform/$TARGET/**/* folder contains modifications.

    Hope it helps!