Watch2–part 4–CI and CD
Now I want to do build and auto-deployment as a Nuget Package( at https://www.nuget.org/packages/watch2 )
The most simple build action on GitHub Actions is simple example of how to do it.
name: .NET on: push: branches: [ main ] pull_request: branches: [ main ] workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - name: Setup .NET 8 uses: actions/setup-dotnet@v3 with: dotnet-version: 8.0.x - name: build run: | cd src cd Watch2 dotnet restore dotnet build
However, I want to do more than that. I want to build , test and deploy on demand the Nuget package to Nuget.org.
However , the building should be done first on local – and then with GitHub Actions – the deployment should be done.
For this I do like https://github.com/xt0rted/dotnet-run-script
This is how I use to do test and pack, by using a global.json file
{ "scripts": { "build": "dotnet build --configuration Release", "test": "dotnet test --configuration Release", "ci": "dotnet r build && dotnet r test", "pack":"dotnet r ci && cd Watch2 && dotnet pack -o ../../nugetPackages" } }
So the github action now looks like this
name: compile and deploy with tag v* on: push: branches: [ "main" ] tags: [ 'v*' ] # Listen for tag pushes that match version tags pull_request: branches: [ "main" ] jobs: build: # runs-on: windows-latest runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 with: dotnet-version: 8.0.x - name: Restore dependencies run: | dotnet tool install --global PowerShell cd src cd Watch2 dotnet restore - name: Build run: | cd src cd Watch2 dotnet tool restore dotnet r pack - name: 'Upload nuget' uses: actions/upload-artifact@v4 with: name: Nuget_${{github.run_number}} path: src/nugetPackages retention-days: 1 - name: push to nuget if: startsWith(github.ref, 'refs/tags/v') # This line ensures the step runs only if a tag version is present run: | dir src/nugetPackages/*.* echo '1' dir src/nugetPackages/*.symbols.nupkg echo '2' cd src cd nugetPackages dotnet nuget push "*.symbols.nupkg" --api-key ${{ secrets.NUGET_KEY }} --source https://api.nuget.org/v3/index.json
More, we should add to github Settings => Secrets and variables => Actions secrets a new secret with name NUGET_KEY
The build and test are made automatically . More, if I want to deploy also, then
1. I modify the version in the csproj file
<PropertyGroup> <Version>8.2024.1102.950</Version> </PropertyGroup>
2. I push the changes to the repository with a tag ( e.g. v8.2024.1102.950)
So this achieves the CI/CD basic .
Leave a Reply