MSDN Community

Connecting Developers with Knowledge

Leveraging GitHub Actions for .NET Development

GitHub Actions is a powerful CI/CD platform that allows you to automate your software development workflows, including building, testing, and deploying your .NET applications. This topic explores how to effectively integrate GitHub Actions into your .NET development lifecycle.

Getting Started with GitHub Actions for .NET

To begin, you'll need a GitHub repository for your .NET project. You can create a workflow file in the .github/workflows/ directory of your repository. This file, typically written in YAML, defines the steps your workflow will execute.

name: .NET CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 with: dotnet-version: '7.0.x' # Specify your desired .NET version - name: Restore dependencies run: dotnet restore - name: Build run: dotnet build --no-restore - name: Test run: dotnet test --no-build --no-restore

Key Concepts for .NET Workflows

  • Jobs: A job is a set of steps that are executed on a runner. You can define multiple jobs to run in parallel or sequentially.
  • Steps: Individual tasks within a job. These can be pre-built GitHub Actions (like actions/checkout or actions/setup-dotnet) or custom shell commands.
  • Runners: The machines where your jobs are executed. GitHub-hosted runners are available for various operating systems (e.g., ubuntu-latest, windows-latest, macos-latest).
  • Triggers: Events that initiate a workflow, such as a push to a branch or a pull request.

Advanced .NET CI/CD with GitHub Actions

Beyond basic building and testing, GitHub Actions can be used for:

  • Publishing NuGet Packages: Automate the creation and publishing of your .NET libraries to NuGet.org.
  • Deployment: Integrate with Azure, AWS, or other cloud providers to deploy your web applications or services.
  • Code Analysis: Incorporate tools like SonarQube for static code analysis.
  • Containerization: Build Docker images for your .NET applications.

Example: Publishing a NuGet Package

This example demonstrates publishing a package on a release tag:

name: Publish NuGet Package on: push: tags: - 'v*' # Trigger on tags starting with 'v' jobs: publish: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 with: dotnet-version: '7.0.x' - name: Get version from tag id: get_version run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/v} - name: Pack NuGet package run: dotnet pack --configuration Release /p:Version=${{ steps.get_version.outputs.VERSION }} - name: Push NuGet package run: dotnet nuget push "bin/Release/*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json env: NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

Remember to store your NUGET_API_KEY as a secret in your GitHub repository's settings.

Related Tags:

.NET GitHub Actions CI/CD Development Azure AWS NuGet