1. Prerequisites
Before starting, ensure you have:
- A .NET application (can be ASP.NET Core or a console app).
- GitHub repository for your project.
- Basic understanding of GitHub Actions and YAML workflows.
2. Understand CI/CD with GitHub Actions
GitHub Actions allows you to automate:
- CI (Continuous Integration): Build, test, and validate code every time you push or create a pull request.
- CD (Continuous Deployment/Delivery): Automatically deploy your app to a server, cloud, or container registry.
Key concepts:
- Workflow: YAML file that defines CI/CD steps (.github/workflows/ci-cd.yml).
- Job: A set of steps executed on a runner (Ubuntu, Windows, or macOS).
- Step: Individual action or command.
- Action: Reusable units like actions/checkout or actions/setup-dotnet.
3. Create a GitHub Actions Workflow
1. In your repository, create a folder:
.github/workflows
2. Inside, create a file ci-cd.yml (or any name).
3. Example: CI/CD for .NET Application
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
# 1. Checkout the code
- name: Checkout code
uses: actions/checkout@v3
# 2. Setup .NET SDK
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x # or your .NET version
# 3. Restore dependencies
- name: Restore dependencies
run: dotnet restore
# 4. Build the project
- name: Build
run: dotnet build --configuration Release --no-restore
# 5. Run unit tests
- name: Run tests
run: dotnet test --no-build --verbosity normal
# 6. Publish the app (for deployment)
- name: Publish
run: dotnet publish -c Release -o ./publish
✅ This workflow triggers on push or pull request to the main branch.
4. Add CD (Deployment) Step
You can deploy to Azure Web App, AWS, Docker, or any server.
Example: Deploy to Azure Web App
- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy@v2
with:
app-name: 'your-app-name'
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ./publish
You need to create a Publish Profile from Azure portal and store it as a GitHub Secret named AZURE_WEBAPP_PUBLISH_PROFILE.
5. Optional Enhancements
Build Docker Image & Push to Docker Hub:
run: docker build -t username/app-name:latest .
- name: Push to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push image
run: docker push username/app-name:latest
Parallel testing for multiple .NET versions.
6. Verify Your Pipeline
- Commit and push your workflow to the repository.
- Go to Actions tab in GitHub to see the workflow running.
- Fix any errors and re-run until all steps pass.
7. Key Benefits
- Automated build and test with each commit.
- Ensures code quality and reduces bugs.
- Easy deployment to cloud or containers.
- Fully integrated with GitHub without extra CI/CD servers.








.gif)
0 Comments