How to combine multistage CICD yaml with pull request

358 Views Asked by At

I've recently applied the multistage YAML for my project, which consists of both CI and CD.

Afterwards I setup the branch policies to only trigger the CI portion when a pull request was created, by adding condition into the CD stage to skip it.

trigger:
  branches: 
    include: 
    - master
  paths:
    include:
    ...

stages:
  - stage: Build
    jobs:
      - job: Build
        pool:
          vmImage: 'windows-latest'

        steps:
        ...
  
  - stage: Deploy
    condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))
    displayName: 'Deploy'
    jobs:
      ... 

Once the pull request completed, the whole CI/CD part triggered again as master branch has new code pushed in. Is there a way to prevent the CI from running again, or download the artifact from previous run, so to kind of "resume" the CD?

The idea is to run only the CI when pull request created, and to continue for CD when pull request completed.

1

There are 1 best solutions below

0
On

Resolved by splitting CI and CD into 2 yaml files, with CD consuming artifacts from CI.

When PR set the triggers in branch policies with path filter for specify CI, and CD set as trigger by same branch with same path filter. This way CD will triggers after PR completed since the branch got updated, and then it will downloads artifacts created by CI during PR.