Skip to content

Commit c21db1a

Browse files
aaronpowellCopilot
andauthored
Add detect-pr-label job to manage package publishing based on PR labels (#829)
* Add detect-pr-label job to manage package publishing based on PR labels * Don't need to install jq, already there * Using github cli * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Apply suggestion from @Copilot Co-authored-by: Copilot <[email protected]> * Update .github/workflows/dotnet-main.yml Co-authored-by: Copilot <[email protected]> * Update .github/workflows/dotnet-main.yml Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 78acc96 commit c21db1a

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

.github/workflows/dotnet-main.yml

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ on:
88
env:
99
DEFAULT_DOTNET_VERSION: "8.0.x"
1010

11+
permissions:
12+
contents: read
13+
pull-requests: read
14+
1115
jobs:
1216
build:
1317
strategy:
@@ -98,10 +102,13 @@ jobs:
98102
${{ github.workspace }}/packages/**/*.nupkg
99103
100104
publish-nuget:
101-
needs: sign
105+
needs: [sign, detect-pr-label]
102106
runs-on: ubuntu-latest
103107
environment:
104108
name: nuget-beta
109+
# Only run publish when the merged PR does NOT contain the skip label.
110+
# The label name is configurable via the `SKIP_PUBLISH_LABEL` env in the detector job below.
111+
if: needs.detect-pr-label.outputs.skip_publish != 'true'
105112
steps:
106113
- name: Download package
107114
uses: actions/download-artifact@v5
@@ -112,10 +119,12 @@ jobs:
112119
run: dotnet nuget push ./*.nupkg --source "https://api.nuget.org/v3/index.json" --api-key ${{ secrets.NUGET_PACKAGE_PUSH_TOKEN }}
113120

114121
publish-azure-artifacts:
115-
needs: sign
122+
needs: [sign, detect-pr-label]
116123
runs-on: windows-latest
117124
environment:
118125
name: azure-artifacts
126+
# Skip pushing to Azure Artifacts when the merged PR requested skipping the publish.
127+
if: needs.detect-pr-label.outputs.skip_publish != 'true'
119128
steps:
120129
- name: Download package
121130
uses: actions/download-artifact@v5
@@ -144,3 +153,35 @@ jobs:
144153
uses: ./.github/workflows/code-coverage.yml
145154
secrets: inherit
146155

156+
detect-pr-label:
157+
# This job detects whether the commit that triggered this push
158+
# is associated with a merged pull request that contains a label
159+
# indicating we should skip publishing packages.
160+
runs-on: ubuntu-latest
161+
outputs:
162+
skip_publish: ${{ steps.check.outputs.skip }}
163+
env:
164+
# Change this label name to whatever you use to skip publishing.
165+
SKIP_PUBLISH_LABEL: skip-nuget-publish
166+
steps:
167+
id: check
168+
env:
169+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
170+
run: |
171+
LABEL="${SKIP_PUBLISH_LABEL}"
172+
# Get the first PR associated with this commit (if any)
173+
number=$(gh api -H "Accept: application/vnd.github+json" \
174+
"/repos/${{ github.repository }}/commits/${{ github.sha }}/pulls" --jq '.[0].number' 2>/dev/null || echo "")
175+
if [ -z "$number" ] || [ "$number" = "null" ]; then
176+
# No PR found for this commit -> do not skip by default
177+
echo "skip=false" >> $GITHUB_OUTPUT
178+
exit 0
179+
fi
180+
181+
# List label names for the PR and check for an exact match
182+
has_label=$(gh api "/repos/${{ github.repository }}/issues/$number/labels" --jq '.[].name' 2>/dev/null | grep -Fx -- "$LABEL" || true)
183+
if [ -n "$has_label" ]; then
184+
echo "skip=true" >> $GITHUB_OUTPUT
185+
else
186+
echo "skip=false" >> $GITHUB_OUTPUT
187+
fi

0 commit comments

Comments
 (0)