3

I have a pipeline I'm putting together that has three different "deploy" steps, each with its own unique deployment but can be triggered by the same job. Ideally, I would like to find a way to "or" the items inside of the needs section to make the job automatically run after one of those previous jobs completes.

I know I can create a separate job for each job to "run" but I'd like to avoid repeating myself if possible.


Int (Dry Run):
  extends: .stageBatchDryRunJob
  stage: Deploy Non-Prod
  except:
    variables:
      - $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/
  variables:
    <<: *hubNonVars
    kube_cluster_id: HubInt
    kube_env: int
  environment: int


Int (Rollback):
  extends: .stageBatchRollbackJob
  stage: Deploy Non-Prod
  except:
    variables:
      - $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/
  variables:
    <<: *hubNonVars
    kube_cluster_id: HubInt
    kube_env: int
  environment: int

I would like to have a single "run" job that requires one of these two above jobs to be completed.

Int (run):
  extends: .run-batch
  variables:
    TOWER_JOB_TEMPLATE: $TOWER_JOB_TEMPLATE_INT
    kube_cluster_id: HubInt
    kube_env: int
  needs: [Int (Dry Run), Int (Rollback)] # can this be "or-ed?" IE needs Int (Dry Run) or Int (Rollback)

Patrick Zawadzki
  • 456
  • 2
  • 6
  • 26

1 Answers1

1

If both jobs actually exist in the pipeline, no. There is no way to do what you describe with a single job and have the downstream job start immediately.

However, if only one of the two jobs will exist in the pipeline (for example one is excluded by a rules configuration), you can leverage the needs optional feature.

needs:
  - job: "Int (Dry Run)"
    optional: true
  - job: "Int (Rollback)"
    optional: true

Of course this introduces the possibility that the job could run when neither of these jobs exist. So, you may want to run a check in your job to see if expected artifacts or variables exist.

You can make the two-job approach a bit more DRY, too, you can use !reference tags or other YAML features like anchors/extends (which you're already using in your provided code).

sytech
  • 29,298
  • 3
  • 45
  • 86