|
| 1 | +name: Main Build |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + packages: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + build-and-pack: |
| 14 | + name: Build, Pack and Create Draft Release |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 # Required for GitVersion |
| 22 | + |
| 23 | + - name: Setup .NET |
| 24 | + uses: actions/setup-dotnet@v4 |
| 25 | + with: |
| 26 | + dotnet-version: | |
| 27 | + 8.0.x |
| 28 | + 9.0.x |
| 29 | + 10.0.x |
| 30 | +
|
| 31 | + - name: Restore dependencies |
| 32 | + run: dotnet restore |
| 33 | + |
| 34 | + - name: Build |
| 35 | + run: dotnet build --configuration Release --no-restore |
| 36 | + |
| 37 | + - name: Test |
| 38 | + run: dotnet test --configuration Release --no-build --verbosity normal |
| 39 | + |
| 40 | + - name: Pack NuGet packages |
| 41 | + run: dotnet pack --configuration Release --no-build --output ./artifacts |
| 42 | + |
| 43 | + - name: Get version from packages |
| 44 | + id: get-version |
| 45 | + run: | |
| 46 | + # Extract version from the first package |
| 47 | + VERSION=$(ls ./artifacts/*.nupkg | head -1 | sed -n 's/.*\.MyCSharp\.HttpUserAgentParser\.\([0-9]\+\.[0-9]\+\.[0-9]\+.*\)\.nupkg/\1/p') |
| 48 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 49 | + echo "Version: $VERSION" |
| 50 | +
|
| 51 | + - name: Check for existing draft release |
| 52 | + id: check-draft |
| 53 | + env: |
| 54 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + run: | |
| 56 | + DRAFT_RELEASE=$(gh release list --limit 100 --json isDraft,name,tagName | jq -r '.[] | select(.isDraft == true) | .tagName' | head -1) |
| 57 | + if [ -n "$DRAFT_RELEASE" ]; then |
| 58 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 59 | + echo "tag=$DRAFT_RELEASE" >> $GITHUB_OUTPUT |
| 60 | + echo "Found existing draft release: $DRAFT_RELEASE" |
| 61 | + else |
| 62 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 63 | + echo "No existing draft release found" |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: Delete existing draft release |
| 67 | + if: steps.check-draft.outputs.exists == 'true' |
| 68 | + env: |
| 69 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 70 | + run: | |
| 71 | + echo "Deleting existing draft release: ${{ steps.check-draft.outputs.tag }}" |
| 72 | + gh release delete ${{ steps.check-draft.outputs.tag }} --yes --cleanup-tag || true |
| 73 | +
|
| 74 | + - name: Create draft release |
| 75 | + env: |
| 76 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 77 | + run: | |
| 78 | + VERSION="${{ steps.get-version.outputs.version }}" |
| 79 | + TAG="v${VERSION}" |
| 80 | +
|
| 81 | + # Create release notes |
| 82 | + cat > release-notes.md << 'EOF' |
| 83 | + ## What's Changed |
| 84 | +
|
| 85 | + This is an automated draft release created from the main branch. |
| 86 | +
|
| 87 | + ### Packages |
| 88 | +
|
| 89 | + The following NuGet packages are included in this release: |
| 90 | +
|
| 91 | + EOF |
| 92 | +
|
| 93 | + # List all packages |
| 94 | + for file in ./artifacts/*.nupkg; do |
| 95 | + filename=$(basename "$file") |
| 96 | + echo "- \`$filename\`" >> release-notes.md |
| 97 | + done |
| 98 | +
|
| 99 | + cat >> release-notes.md << 'EOF' |
| 100 | +
|
| 101 | + ### Installation |
| 102 | +
|
| 103 | + ```bash |
| 104 | + dotnet add package MyCSharp.HttpUserAgentParser |
| 105 | + dotnet add package MyCSharp.HttpUserAgentParser.AspNetCore |
| 106 | + dotnet add package MyCSharp.HttpUserAgentParser.MemoryCache |
| 107 | + ``` |
| 108 | +
|
| 109 | + **Full Changelog**: https://github.com/${{ github.repository }}/commits/${{ github.sha }} |
| 110 | + EOF |
| 111 | +
|
| 112 | + # Create draft release |
| 113 | + gh release create "$TAG" \ |
| 114 | + ./artifacts/*.nupkg \ |
| 115 | + --draft \ |
| 116 | + --title "Release $VERSION" \ |
| 117 | + --notes-file release-notes.md \ |
| 118 | + --target ${{ github.sha }} |
| 119 | +
|
| 120 | + echo "Created draft release: $TAG" |
| 121 | +
|
| 122 | + - name: Upload artifacts |
| 123 | + uses: actions/upload-artifact@v4 |
| 124 | + with: |
| 125 | + name: nuget-packages |
| 126 | + path: ./artifacts/*.nupkg |
| 127 | + retention-days: 30 |
0 commit comments