Skip to content

Commit bbaf3ea

Browse files
committed
Convert nuget.exe to dotnet CLI in GitHub Actions workflows
- Created convert-nuget-to-dotnet.sh script to automate conversion - Replaced nuget source Add with dotnet nuget add source - Replaced nuget push with dotnet nuget push - Added actions/setup-dotnet@v4 to all workflows - Removed nuget/setup-nuget@v1 action (no longer needed) - Updated GitHub Package Registry authentication to use dotnet CLI This addresses issue #120 for migrating from nuget.exe to dotnet CLI.
1 parent aefe6f6 commit bbaf3ea

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

.github/workflows/AutoMerge.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ jobs:
77
auto-merge:
88
runs-on: ubuntu-latest
99
steps:
10+
- name: Setup .NET
11+
uses: actions/setup-dotnet@v4
12+
with:
13+
dotnet-version: '8.0.x'
14+
1015
- uses: actions/checkout@v2
1116
- uses: ahmadnassri/action-dependabot-auto-merge@v2
1217
with:

.github/workflows/csharp.yml

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ jobs:
1919
test:
2020
runs-on: ubuntu-latest
2121
steps:
22+
- name: Setup .NET
23+
uses: actions/setup-dotnet@v4
24+
with:
25+
dotnet-version: '8.0.x'
26+
2227
- uses: actions/checkout@v1
2328
with:
2429
submodules: true
@@ -29,20 +34,29 @@ jobs:
2934
needs: test
3035
runs-on: ubuntu-latest
3136
steps:
37+
- name: Setup .NET
38+
uses: actions/setup-dotnet@v4
39+
with:
40+
dotnet-version: '8.0.x'
41+
3242
- uses: actions/checkout@v1
3343
with:
3444
submodules: true
35-
- uses: nuget/setup-nuget@v1
3645
- name: Publish NuGet package to GitHub Package Registry
3746
run: |
3847
dotnet build -c Release
3948
dotnet pack -c Release
40-
nuget source Add -Name "GitHub" -Source "https://nuget.pkg.github.com/linksplatform/index.json" -UserName linksplatform -Password ${{ secrets.GITHUB_TOKEN }}
41-
nuget push **/*.nupkg -Source "GitHub" -SkipDuplicate
49+
dotnet nuget add source https://nuget.pkg.github.com/linksplatform/index.json --name GitHub --username linksplatform --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text
50+
dotnet nuget push **/*.nupkg --source GitHub --skip-duplicate
4251
pusnToNuget:
4352
runs-on: ubuntu-latest
4453
needs: test
4554
steps:
55+
- name: Setup .NET
56+
uses: actions/setup-dotnet@v4
57+
with:
58+
dotnet-version: '8.0.x'
59+
4660
- uses: actions/checkout@v1
4761
with:
4862
submodules: true
@@ -60,6 +74,11 @@ jobs:
6074
runs-on: ubuntu-latest
6175
needs: test
6276
steps:
77+
- name: Setup .NET
78+
uses: actions/setup-dotnet@v4
79+
with:
80+
dotnet-version: '8.0.x'
81+
6382
- uses: actions/checkout@v1
6483
with:
6584
submodules: true
@@ -82,6 +101,11 @@ jobs:
82101
outputs:
83102
isCsFilesChanged: ${{ steps.setIsCsFilesChangedOutput.outputs.isCsFilesChanged }}
84103
steps:
104+
- name: Setup .NET
105+
uses: actions/setup-dotnet@v4
106+
with:
107+
dotnet-version: '8.0.x'
108+
85109
- uses: actions/checkout@v3
86110
with:
87111
fetch-depth: 0
@@ -107,6 +131,11 @@ jobs:
107131
needs: [findChangedCsFiles]
108132
if: ${{ needs.findChangedCsFiles.outputs.isCsFilesChanged == 'true' }}
109133
steps:
134+
- name: Setup .NET
135+
uses: actions/setup-dotnet@v4
136+
with:
137+
dotnet-version: '8.0.x'
138+
110139
- uses: actions/checkout@v1
111140
with:
112141
submodules: true
@@ -122,6 +151,11 @@ jobs:
122151
needs: [findChangedCsFiles]
123152
if: ${{ needs.findChangedCsFiles.outputs.isCsFilesChanged == 'true' }}
124153
steps:
154+
- name: Setup .NET
155+
uses: actions/setup-dotnet@v4
156+
with:
157+
dotnet-version: '8.0.x'
158+
125159
- uses: actions/checkout@v1
126160
with:
127161
submodules: true

convert-nuget-to-dotnet.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
3+
# Script to convert GitHub Actions workflows in linksplatform/Interfaces from nuget.exe (with Mono) to dotnet CLI
4+
# Assumes workflows are in .github/workflows/*.yml or *.yaml
5+
# Targets nuget.exe push commands and replaces them with dotnet nuget push
6+
# Ensures actions/setup-dotnet is included for .NET SDK setup
7+
8+
# Exit on error
9+
set -e
10+
11+
# Directory containing GitHub Actions workflows
12+
WORKFLOW_DIR=".github/workflows"
13+
14+
# Check if workflow directory exists
15+
if [ ! -d "$WORKFLOW_DIR" ]; then
16+
echo "Error: Workflow directory $WORKFLOW_DIR not found. Please run this script from the repository root."
17+
exit 1
18+
fi
19+
20+
# Find all .yml and .yaml files in the workflow directory
21+
WORKFLOW_FILES=$(find "$WORKFLOW_DIR" -type f \( -name "*.yml" -o -name "*.yaml" \))
22+
23+
# Check if any workflow files exist
24+
if [ -z "$WORKFLOW_FILES" ]; then
25+
echo "Error: No workflow files (.yml or .yaml) found in $WORKFLOW_DIR."
26+
exit 1
27+
fi
28+
29+
# Function to check if actions/setup-dotnet is already included
30+
has_setup_dotnet() {
31+
local file="$1"
32+
grep -q "uses: actions/setup-dotnet@v[1-4]" "$file"
33+
}
34+
35+
# Function to add actions/setup-dotnet step if missing
36+
add_setup_dotnet() {
37+
local file="$1"
38+
# Find the first step in the job (after 'steps:') and insert setup-dotnet before it
39+
sed -i.bak '/steps:/a\
40+
- name: Setup .NET\
41+
uses: actions/setup-dotnet@v4\
42+
with:\
43+
dotnet-version: '\''8.0.x'\''\
44+
' "$file"
45+
echo "Added actions/setup-dotnet@v4 to $file"
46+
}
47+
48+
# Function to process a workflow file
49+
process_workflow() {
50+
local file="$1"
51+
echo "Processing workflow file: $file"
52+
53+
# Create a backup of the original file
54+
cp "$file" "$file.bak"
55+
56+
# Replace nuget source Add with dotnet nuget add source (simplified pattern)
57+
sed -i.bak "s|nuget source Add -Name \"GitHub\" -Source \"https://nuget.pkg.github.com/linksplatform/index.json\" -UserName linksplatform -Password \${{ secrets.GITHUB_TOKEN }}|dotnet nuget add source https://nuget.pkg.github.com/linksplatform/index.json --name GitHub --username linksplatform --password \${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text|g" "$file"
58+
59+
# Replace nuget push with dotnet nuget push
60+
sed -i.bak -E "s|nuget push ([^[:space:]]+) -Source \"([^\"]+)\" -SkipDuplicate|dotnet nuget push \1 --source \2 --skip-duplicate|g" "$file"
61+
62+
# Replace nuget.exe push with dotnet nuget push (more general pattern)
63+
sed -i.bak -E "s|run:[[:space:]]*(mono[[:space:]]+)?/?([a-zA-Z0-9/._-]*\/)?nuget(\.exe)?[[:space:]]+push[[:space:]]+([^[:space:]].*nupkg)([[:space:]]+--api-key[[:space:]]+\$\{\{[[:space:]]*[a-zA-Z0-9._-]+\}\})?([[:space:]]+--source[[:space:]]+[a-zA-Z0-9:/._-]+)?|run: dotnet nuget push \4 --api-key \5 --source \6 --skip-duplicate|g" "$file"
64+
65+
# Replace nuget.exe restore with dotnet restore (if present)
66+
sed -i.bak -E "s|run:[[:space:]]*(mono[[:space:]]+)?/?([a-zA-Z0-9/._-]*\/)?nuget(\.exe)?[[:space:]]+restore[[:space:]]+([^[:space:]].*)|run: dotnet restore \4|g" "$file"
67+
68+
# Remove nuget/setup-nuget@v1 action as it's no longer needed
69+
sed -i.bak -E "/- uses: nuget\/setup-nuget@v1/d" "$file"
70+
71+
# Check for changes
72+
if ! diff "$file" "$file.bak" >/dev/null; then
73+
echo "Modified $file to use dotnet CLI instead of nuget.exe"
74+
else
75+
echo "No nuget.exe commands found in $file; no changes made"
76+
rm "$file.bak" # Remove backup if no changes
77+
fi
78+
79+
# Add actions/setup-dotnet if not present
80+
if ! has_setup_dotnet "$file"; then
81+
add_setup_dotnet "$file"
82+
fi
83+
}
84+
85+
# Process each workflow file
86+
for file in $WORKFLOW_FILES; do
87+
process_workflow "$file"
88+
done
89+
90+
# Clean up any remaining backup files if no errors
91+
echo "Cleaning up backup files..."
92+
find "$WORKFLOW_DIR" -name "*.bak" -exec rm {} \;
93+
94+
echo "Conversion complete! Please review changes in $WORKFLOW_DIR and test the updated workflows."
95+
echo "If publishing to nuget.org, ensure NUGET_API_KEY is set in GitHub Secrets."
96+
echo "If targeting a different NuGet feed (e.g., GitHub Packages), update the --source URL and authentication as needed."

0 commit comments

Comments
 (0)