@@ -13,6 +13,26 @@ inputs:
1313 description : ' Version of the nuget-license tool'
1414 required : false
1515 default : ' 4.0.0'
16+ ignore-errors :
17+ description : ' Treat non-zero exit codes from nuget-license as warnings instead of failing'
18+ required : false
19+ default : ' true'
20+ include-transitive :
21+ description : ' Include transitive dependencies'
22+ required : false
23+ default : ' false'
24+ override-package-info-file :
25+ description : ' JSON file with override package/license info'
26+ required : false
27+ ignored-packages-file :
28+ description : ' JSON file listing packages to ignore'
29+ required : false
30+ allowed-licenses-file :
31+ description : ' JSON file listing allowed license types'
32+ required : false
33+ licenseurl-mappings-file :
34+ description : ' JSON file mapping license URLs to license types'
35+ required : false
1636outputs :
1737 license_file :
1838 description : ' Path to the generated license file'
@@ -26,13 +46,26 @@ runs:
2646 echo 'Dotnet SDK not found. Please run actions/setup-dotnet earlier in the workflow.' >&2
2747 exit 1
2848 fi
49+ - name : Restore project (ensure assets for license scan)
50+ shell : bash
51+ run : |
52+ set -euo pipefail
53+ PROJECT='${{ inputs.project }}'
54+ if [ ! -f "$PROJECT" ]; then echo "Project/Solution file not found: $PROJECT" >&2; exit 1; fi
55+ # Perform a targeted restore only if no obj/project.assets.json exists yet
56+ if ! grep -q 'project.assets.json' <(find . -path './**/obj/project.assets.json' 2>/dev/null); then
57+ echo 'Performing dotnet restore...'
58+ dotnet restore "$PROJECT" >/dev/null
59+ else
60+ echo 'Restore artifacts already present. Skipping restore.'
61+ fi
2962 - name : Install nuget-license tool
3063 shell : bash
3164 run : |
3265 set -euo pipefail
3366 TOOL_PATH=".tools"
3467 mkdir -p "$TOOL_PATH"
35- VERSION=" ${{ inputs.nuget-license-version }}"
68+ VERSION=' ${{ inputs.nuget-license-version }}'
3669 dotnet tool install --tool-path "$TOOL_PATH" nuget-license --version "$VERSION" \
3770 || dotnet tool update --tool-path "$TOOL_PATH" nuget-license --version "$VERSION"
3871 - name : Generate license file
@@ -43,16 +76,38 @@ runs:
4376 EXE=".tools/nuget-license"
4477 if [ ! -x "$EXE" ] && [ -f ".tools/nuget-license.exe" ]; then EXE=".tools/nuget-license.exe"; fi
4578 if [ ! -f "$EXE" ]; then echo 'nuget-license tool not found' >&2; exit 1; fi
46- PROJECT="${{ inputs.project }}"
47- OUT_FILE="${{ inputs.output-file }}"
79+ PROJECT='${{ inputs.project }}'
80+ OUT_FILE='${{ inputs.output-file }}'
81+ IGNORE_ERRORS='${{ inputs.ignore-errors }}'
4882 echo "Starting license collection for $PROJECT -> $OUT_FILE"
83+ CMD=("$EXE" -i "$PROJECT" -fo "$OUT_FILE" -o Markdown)
84+ if [ '${{ inputs.include-transitive }}' = 'true' ]; then CMD+=( -t ); fi
85+ [ -n '${{ inputs.override-package-info-file }}' ] && [ -f '${{ inputs.override-package-info-file }}' ] && CMD+=( -override '${{ inputs.override-package-info-file }}' )
86+ [ -n '${{ inputs.ignored-packages-file }}' ] && [ -f '${{ inputs.ignored-packages-file }}' ] && CMD+=( -ignore '${{ inputs.ignored-packages-file }}' )
87+ [ -n '${{ inputs.allowed-licenses-file }}' ] && [ -f '${{ inputs.allowed-licenses-file }}' ] && CMD+=( -a '${{ inputs.allowed-licenses-file }}' )
88+ [ -n '${{ inputs.licenseurl-mappings-file }}' ] && [ -f '${{ inputs.licenseurl-mappings-file }}' ] && CMD+=( -mapping '${{ inputs.licenseurl-mappings-file }}' )
4989 set +e
50- "$EXE" -i "$PROJECT" -fo "$OUT_FILE" -o Markdown
90+ "${CMD[@]}"
5191 EXIT_CODE=$?
5292 set -e
5393 if [ ! -f "$OUT_FILE" ]; then echo 'License file was not generated' >&2; exit 1; fi
94+ # Sanitize: remove completely empty package rows (tool sometimes emits duplicates with missing data)
95+ TMP_SANITIZE="$OUT_FILE.sanitized.tmp"
96+ awk 'NR==1 || NR==2 || !($0 ~ /^\|[[:space:]]+\|[[:space:]]+\|/)' "$OUT_FILE" > "$TMP_SANITIZE" && mv "$TMP_SANITIZE" "$OUT_FILE"
97+ if grep -q 'No license information found' "$OUT_FILE"; then
98+ echo 'Detected packages without license information.' >&2
99+ if [ "$IGNORE_ERRORS" != 'true' ]; then
100+ echo 'Failing due to missing license information.' >&2
101+ exit 2
102+ fi
103+ fi
54104 if [ $EXIT_CODE -ne 0 ]; then
55- echo "Warning: nuget-license exited with code $EXIT_CODE (possibly missing license info)." >&2
105+ if [ "$IGNORE_ERRORS" = 'true' ]; then
106+ echo "Warning: nuget-license exited with code $EXIT_CODE (continuing)." >&2
107+ else
108+ echo "nuget-license exited with code $EXIT_CODE (failing)." >&2
109+ exit $EXIT_CODE
110+ fi
56111 fi
57112 echo "license_file=$OUT_FILE" >> "$GITHUB_OUTPUT"
58113 echo "Licenses collected at $OUT_FILE"
0 commit comments