-
Notifications
You must be signed in to change notification settings - Fork 330
Skip redundant PR triggers (Draft<->Open state transitions) #4303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
paulmedynski
wants to merge
2
commits into
main
Choose a base branch
from
dev/paul/pr-checks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
eng/pipelines/stages/detect-redundant-trigger-stage.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| #################################################################################################### | ||
| # Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this | ||
| # file to you under the MIT license. See the LICENSE file in the project root for more information. | ||
| #################################################################################################### | ||
|
|
||
| # This stage gates PR pipeline runs to avoid redundant builds triggered by state-only transitions | ||
| # (e.g. Draft <-> Open). It compares the current merge commit (Build.SourceVersion) against the | ||
| # most recent successful build for the same PR in this pipeline definition. | ||
| # | ||
| # If the merge commit is unchanged and the prior build succeeded, the build is considered redundant | ||
| # and the output variable 'meaningful' is set to 'false'. Downstream stages should check this | ||
| # variable and skip when it is 'false'. | ||
| # | ||
| # Output variable reference (for use in stage conditions): | ||
| # | ||
| # dependencies.detect_redundant_trigger_stage.outputs['detect_redundant_trigger.detect.meaningful'] | ||
| # | ||
| # This stage defines: | ||
| # | ||
| # Stage name: detect_redundant_trigger_stage | ||
| # Job name: detect_redundant_trigger | ||
| # Step name: detect | ||
| # Variable: meaningful (true|false) | ||
|
|
||
| parameters: | ||
|
|
||
| # True to emit debug information. | ||
| - name: debug | ||
| type: boolean | ||
| default: false | ||
|
|
||
| stages: | ||
|
|
||
| - stage: detect_redundant_trigger_stage | ||
| displayName: Detect Redundant Trigger | ||
| jobs: | ||
|
|
||
| - job: detect_redundant_trigger | ||
| displayName: Detect Redundant Trigger | ||
| pool: | ||
| vmImage: ubuntu-latest | ||
|
|
||
| steps: | ||
|
|
||
| - checkout: none | ||
|
|
||
| - bash: | | ||
| set -euo pipefail | ||
|
|
||
| # The current merge commit SHA for this PR build. | ||
| CURRENT_SOURCE_VERSION="$(Build.SourceVersion)" | ||
|
|
||
| # The Azure DevOps REST API base URL. | ||
| ORG_URL="$(System.CollectionUri)" | ||
| PROJECT="$(System.TeamProject)" | ||
| DEFINITION_ID="$(System.DefinitionId)" | ||
| BUILD_ID="$(Build.BuildId)" | ||
|
|
||
| # The branch name used for PR builds (refs/pull/{number}/merge). | ||
| BRANCH="$(Build.SourceBranch)" | ||
|
|
||
| echo "Current source version: $CURRENT_SOURCE_VERSION" | ||
| echo "Branch: $BRANCH" | ||
| echo "Definition ID: $DEFINITION_ID" | ||
| echo "Build ID: $BUILD_ID" | ||
|
|
||
| # Query the most recent successful build for this PR in this pipeline, | ||
| # excluding the current build. | ||
| API_URL="${ORG_URL}${PROJECT}/_apis/build/builds" | ||
| API_URL="${API_URL}?definitions=${DEFINITION_ID}" | ||
| API_URL="${API_URL}&branchName=${BRANCH}" | ||
| API_URL="${API_URL}&resultFilter=succeeded" | ||
| API_URL="${API_URL}&\$top=5" | ||
| API_URL="${API_URL}&queryOrder=finishTimeDescending" | ||
| API_URL="${API_URL}&api-version=7.1" | ||
|
|
||
| RESPONSE=$(curl -s -H "Authorization: Bearer $(System.AccessToken)" "$API_URL") | ||
|
|
||
|
paulmedynski marked this conversation as resolved.
Outdated
|
||
| if [[ "${{ parameters.debug }}" == "True" ]]; then | ||
|
paulmedynski marked this conversation as resolved.
Outdated
|
||
| echo "API Response:" | ||
| echo "$RESPONSE" | head -c 2000 | ||
| fi | ||
|
|
||
| # Find the first successful build that is not the current build. | ||
| LAST_SUCCESSFUL_VERSION=$(echo "$RESPONSE" | \ | ||
| jq -r --argjson buildId "$BUILD_ID" \ | ||
| '[.value[] | select(.id != $buildId)] | .[0].sourceVersion // empty') | ||
|
|
||
| echo "Last successful source version: ${LAST_SUCCESSFUL_VERSION:-<none>}" | ||
|
|
||
| if [[ -n "$LAST_SUCCESSFUL_VERSION" && "$LAST_SUCCESSFUL_VERSION" == "$CURRENT_SOURCE_VERSION" ]]; then | ||
| echo "" | ||
| echo "##[section]Build is REDUNDANT — source version unchanged and prior build succeeded." | ||
| echo "This is likely a Draft/Open state transition. Skipping build." | ||
| echo "##vso[task.setvariable variable=meaningful;isOutput=true]false" | ||
| else | ||
|
paulmedynski marked this conversation as resolved.
|
||
| echo "" | ||
| echo "##[section]Build is MEANINGFUL — proceeding." | ||
| if [[ -z "$LAST_SUCCESSFUL_VERSION" ]]; then | ||
| echo " Reason: No prior successful build found for this PR." | ||
| elif [[ "$LAST_SUCCESSFUL_VERSION" != "$CURRENT_SOURCE_VERSION" ]]; then | ||
| echo " Reason: Source version changed (new commits or target branch update)." | ||
| fi | ||
| echo "##vso[task.setvariable variable=meaningful;isOutput=true]true" | ||
| fi | ||
| name: detect | ||
| displayName: Check for redundant PR trigger | ||
| env: | ||
| SYSTEM_ACCESSTOKEN: $(System.AccessToken) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.