Update
Based on the further discussions, I would suggest we trigger the deployment pipeline (TerraformDemoDeployment) by a PR-merged commit (IndividualCI) and in this pipeline, we can configure DownloadPipelineArtifact@2 task to download the latest artifacts published by the merged PR with the filter of specific branch refs/pull/$(prId)/merge.
Prerequisite Considerations/Knowledge
DownloadPipelineArtifact@2 supports downloading Latest from sepcific branch;
- The triggering Build.SourceBranch of a PR validation pipeline (
TerraformDemoPR) is refs/pull/$(prId)/merge;
- By default, the message of the merge commit message begins with
Merged PR $(prId):;
- Therefore, we can split the value of
$prId from the predined variable $(Build.SourceVersionMessage) that comes with the PR-merged commit trigger - IndividualCI.
TerraformDemoPR.yml
TerraformDemoPR is the PR validation pipeline that generates pipeline aritfacts of tfplan file(s). Having created the PR with the Id of 55 from dev to main, it is triggered with the code from branch ref/pull/55/merge.

pool:
vmImage: windows-latest
variables:
- group: VG-TerraformAuthSP-Secret
trigger: none
pr:
branches:
include:
- main
stages:
- stage: PR
jobs:
- job: PRTerraformRG
displayName: PR validation - Terrafrom RG
condition: eq(variables['Build.Reason'], 'PullRequest')
steps:
- script: |
choco install terraform
displayName: Install Terraform
- script: |
env
echo "1. Start initializing"
terraform init
echo "2. Start validating"
terraform validate
echo "3. Start Planning"
terraform plan -input=false -var "RGNAME=rg-tf-demorg-sp-$(System.PullRequest.PullRequestId)-Build$(Build.BuildId)" -out "tfplan-rg-tf-demorg-sp-PR$(System.PullRequest.PullRequestId)-Build$(Build.BuildId)"
displayName: Terraform validatie and plan
workingDirectory: $(System.DefaultWorkingDirectory)/RG
env:
# ARM_USE_MSI: true
ARM_CLIENT_ID: $(ARM_CLIENT_ID)
ARM_CLIENT_SECRET: $(ARM_CLIENT_SECRET)
ARM_SUBSCRIPTION_ID: $(ARM_SUBSCRIPTION_ID)
ARM_TENANT_ID: $(ARM_TENANT_ID)
- task: CopyFiles@2
inputs:
SourceFolder: '$(System.DefaultWorkingDirectory)/RG'
Contents: '*tfplan*'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
publishLocation: 'pipeline'
There are several other validation builds of this pipeline publishing their artifacts, which are manually re-queued in the PR or automatically triggered by new commits pushed to the source branch dev of PR 55 and also by some other PRs to main. The latest succeeded build of PR 55 validation is the run of 2597.

TerraformDemoDeployment.yml
As soon as the PR 55 is completed and its code is merged to main with the default merge commit message Merged PR 55: XXXXX, the TerraformDemoDeployment pipeline is automatically triggered by this PR-merged commit. The prId is splited from the default merge commit message and the DownloadPipelineArtifact@2 succeeds to download the pubished artifacts of build 2597, even though there are already other newer artifacts published by the builds of TerraformDemoPR.
trigger:
- main
pool:
vmImage: windows-latest
jobs:
- deployment: TerraformDeploymentJob
condition: eq(variables['Build.Reason'], 'IndividualCI')
variables:
prId: ${{ split(split(variables['Build.SourceVersionMessage'], ':')[0], 'Merged PR ')[1] }}
environment: E-TerraformDeployment
strategy:
runOnce:
deploy:
steps:
- download: none
- powershell: |
Write-Host "Build.SourceVersionMessage is $(Build.SourceVersionMessage)"
Write-Host "prId is $(prId)"
displayName: Get prId from Build.SourceVersionMessage
- task: DownloadPipelineArtifact@2
inputs:
buildType: 'specific'
project: '97d5fd3d-48ec-4d63-b7df-c6f7b49023ef'
definition: 'TerraformDemoPR'
buildVersionToDownload: 'latestFromBranch'
branchName: 'refs/pull/$(prId)/merge'
targetPath: '$(Pipeline.Workspace)'
displayName: Download latest artifacts published by TerraformDemoPR from branch refs/pull/$(prId)/merge
- powershell: tree $(Pipeline.Workspace) /F /A
displayName: Show the downloaded artifacts

Limitations
- We have to make sure the merge commit message begins with default merge commit message if using custom messages;
- Based on my test, the split function to sperate
$(Build.SourceVersionMessage) only works in the template expression when the trigger is IndividualCI, as $(Build.SourceVersionMessage) is not available in template when the trigger is BatchedCI or Manual.
Hope the workaround can meet your requirment.
Legacy
There isn't a method out of the box for the pipeline to be triggered by a PR merge commit and retrieve the corresponding artifacts published by the commit's PR validation pipeline.
Since you would expect the deployment pipeline to be triggered by any commit merged onto main branch, why not generate the artifacts via another pipeline with the merged version of code from main instead of via the PR validation pipeline with the intermediate version of code from a PR?
I am not sure why it is required to generate the artifacts by your PR validation pipeline.
If not, I would suggest using a new stage in the same deployment pipeline with CI trigger from main to publish artifacts, which makes more sense. Here is a very simple structure of this workflow. Kindly note that
If no checkout steps are added in a traditional job, the default behavior is as if checkout: self were the first step, and the current repository is checked out.
Similarly, if no download steps are added in a deployment job,
All available artifacts from the current pipeline and from the associated pipeline resources are automatically downloaded in deployment jobs and made available for your deployment.
trigger:
- main
stages:
- stage: build
jobs:
- job: BuildJob
steps:
- script: echo "Some build steps in front of publish."
- publish: $(System.DefaultWorkingDirectory)
artifact: build-$(Build.BuildId)
- stage: deploy
jobs:
- deployment: DeploymentJob
displayName: Deploy to Environment Test
environment:
name: E-Test
strategy:
runOnce:
preDeploy:
steps:
- download: current
Thus the artifact is generated and published based on each commit on main branch and the subsequent deployment job can pick up the expected artifact.
Besides, if you would like to trigger the deployment pipeline by the published artifact from PR pipeline rather than by the CI trigger, you may also consider using pipeline resource trigger.
Hope the information can help.