Continuous Integration and Continuous Deployment is essential in modern software development for maintaining quality and accelerating delivery.
In my previous post, I have described how to Configure a Bitbucket Repository in Visual Studio. In this post, we’ll walk through how to configure bitbucket CI/CD Pipelines for an ASP.NET application using Bitbucket Pipelines.
CI/CD pipeline Configuration In Bitbucket
Getting Started
Bitbucket CI/CD Pipelines offers a simple yet powerful way to automate the build, test, and deployment of your code directly from your Bitbucket repository. It is essential in modern software development for maintaining quality and accelerating delivery.
What Is CI/CD Pipeline?
Bitbucket CI/CD Pipeline stands for Continuous Integration and Continuous Deployment/Delivery. It's a development practice that automates building, testing, and deploying code, ensuring faster delivery and fewer bugs in production.
Bitbucket Pipeline Deployment Steps
Setting up Bitbucket Pipelines for deployment involves automating your build, test, and deployment processes using the bitbucket-pipelines.yml
file. Here’s a high-level guide to get you started, followed by a more detailed example for a common scenario (like deploying to a server via SSH or to a cloud provider).
Prerequisites
Before you start, ensure you have:- A Bitbucket repository with your ASP.NET project (ASP.NET Core recommended)
- Basic knowledge of YAML syntax
- Docker installed locally (optional but helpful)
- A hosting provider for deployment (e.g., Azure, AWS, IIS server, etc.)
Enable Bitbucket Pipelines
- Go to your Bitbucket repository.
- Click on Pipelines in the sidebar.
- Click Enable Pipelines.
- Bitbucket will prompt you to create a
bitbucket-pipelines.yml
file.
Create the bitbucket-pipelines.yml
file
This YAML file defines your pipeline steps, to know YAML file visit my previous post "Understanding Bitbucket Pipeline YAML File". Here's a sample pipeline configuration for an ASP.NET Core web application. Place this file at the root of your repository. Bitbucket will use it to define how to build and deploy your project.
image: mcr.microsoft.com/dotnet/sdk:8.0
pipelines:
default:
- step:
name: Build and Publish ASP.NET Core App
caches:
- dotnetcore
script:
- echo "Restoring NuGet packages..."
- dotnet restore
- echo "Building the application..."
- dotnet build --configuration Release
- echo "Publishing the application..."
- dotnet publish -c Release -o output
artifacts:
- output/**
Configure Deployment Variables
Deployment variables in Bitbucket Pipelines are a special kind of environment variable that are tied to a specific deployment environment, such as:
- Test
- Staging
- Production
They allow you to define different values for each environment without modifying your pipeline code. Here are the steps to add deployment variables.
- Go to your repository.
- Click Repository settings.
- Under Pipelines, select Repository variables.
- Click Add variable.
- Give it a name and value.
- Mark as secured if it’s sensitive (prevents it from being echoed in logs).
Deploying to a Target (Example: Azure App Service)
To deploy to Azure using FTP or the Azure CLI, you can modify the pipeline. Here's an example using FTP:- Add FTP Credentials:
FTP_SERVER
FTP_USERNAME
FTP_PASSWORD
- Add Deployment Step
image: mcr.microsoft.com/dotnet/sdk:8.0 pipelines: default: - step: name: Build and Publish caches: - dotnetcore script: - dotnet restore - dotnet build --configuration Release - dotnet publish -c Release -o output artifacts: - output/** - step: name: Deploy to Azure (FTP) deployment: production script: - apt-get update && apt-get install -y lftp - echo "Deploying to Azure FTP..." - lftp -c "set ftp:ssl-allow no; open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_SERVER; mirror -R ./output /site/wwwroot"
Notes
- The Docker image
mcr.microsoft.com/dotnet/sdk:8.0
includes the full .NET SDK. artifacts
preserve the build output between steps.- You can define multiple pipelines for different branches or tags, e.g.,
branches: master
,tags: v*
.
Security Tips
- Always store secrets (API keys, FTP passwords, etc.) in Repository Variables, not directly in your YAML.
- Use deployment environments in Bitbucket to manage access and visibility of deployments.
Summary
Bitbucket CI/CD Pipelines provides a clean and integrated way to implement CI/CD for your ASP.NET applications. Whether you're deploying to Azure, AWS, or an on-prem server, with a little setup, you can fully automate your build and deployment process.
Thanks