Setting Up CI/CD Pipelines for a NestJS Application on Azure
In the world of modern software development, Continuous Integration (CI) and Continuous Deployment (CD) have become essential practices that enable teams to deliver high-quality applications more efficiently. For developers working with NestJS—a progressive Node.js framework for building efficient and scalable server-side applications—setting up CI/CD pipelines in Azure can enhance your deployment processes, automate testing, and ensure that your applications run smoothly in production.
In this article, we will walk through the process of setting up a CI/CD pipeline for a NestJS application on Azure. We’ll cover definitions, use cases, and actionable insights, all while providing clear code examples and step-by-step instructions.
What is CI/CD?
Continuous Integration (CI)
CI is the practice of automatically testing and merging code changes into a shared repository. This helps identify bugs early in the development cycle, ensuring that the main branch is always in a deployable state.
Continuous Deployment (CD)
CD is the process of automatically deploying the application to a production environment after it passes the CI testing phase. This reduces the time between writing code and getting it into users’ hands.
Why Use CI/CD with NestJS on Azure?
- Automation: Automate builds, tests, and deployments, reducing manual errors.
- Faster Feedback: Quickly identify issues through automated testing.
- Consistent Deployments: Ensure that your application behaves the same way in all environments.
- Scalability: Easily scale your applications and infrastructure as needed.
Prerequisites
Before we start, make sure you have:
- A NestJS application set up locally.
- An Azure account (you can use the free tier to get started).
- Node.js and npm installed on your machine.
- Azure CLI installed.
Step 1: Create an Azure App Service
-
Log in to Azure:
bash az login
-
Create a Resource Group:
bash az group create --name myResourceGroup --location eastus
-
Create an App Service Plan:
bash az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1 --is-linux
-
Create a Web App:
bash az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name myNestApp --runtime "NODE|14-lts"
Step 2: Configure Your NestJS Application
Prepare Your Application for Deployment
-
Add a
start
script inpackage.json
:json "scripts": { "start": "node dist/main.js", "build": "npm run compile", "compile": "tsc -p tsconfig.build.json" }
-
Build Your Application:
bash npm run build
-
Ensure your application listens on the correct port: In your
main.ts
, ensure that the application listens to the port defined by the environment variable:typescript const port = process.env.PORT || 3000; await app.listen(port); console.log(`Application is running on: http://localhost:${port}`);
Step 3: Set Up CI/CD with GitHub Actions
Create a GitHub Repository
- Push your NestJS application to GitHub.
Configure GitHub Actions
- Create a
.github/workflows/deploy.yml
file in your repository: ```yaml name: CI/CD Pipeline for NestJS
on: push: branches: - main
jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Dependencies
run: npm install
- name: Build Application
run: npm run build
- name: Deploy to Azure
uses: azure/webapps-deploy@v2
with:
app-name: myNestApp
slot-name: production
publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}
package: ./dist
```
Configure Azure Publish Profile
- Navigate to your Azure Portal:
- Go to your Web App.
- In the left sidebar, find "Deployment Center" and select "Get Publish Profile".
-
Copy the contents of the publish profile.
-
Add the Publish Profile to GitHub Secrets:
- Go to your GitHub repository.
- Click on "Settings" > "Secrets" > "Actions".
- Create a new secret named
AZURE_PUBLISH_PROFILE
and paste your publish profile contents.
Step 4: Testing Your CI/CD Pipeline
-
Push changes to the
main
branch:bash git add . git commit -m "Set up CI/CD pipeline for NestJS" git push origin main
-
Monitor the Actions tab in GitHub:
- Check for successful builds and deployments.
- Any errors will be displayed, allowing for quick troubleshooting.
Troubleshooting Tips
- Check Logs: Azure provides logs in the portal to help diagnose issues.
- Environment Variables: Ensure that any environment variables needed by your NestJS app are set in Azure.
- Build Errors: If the build fails, verify that all dependencies are correctly defined in
package.json
.
Conclusion
Setting up a CI/CD pipeline for your NestJS application on Azure can drastically improve your development workflow. By automating the build, test, and deployment processes, you can focus on writing code that delivers value to your users. With the right setup, you can ensure that your application is always in a deployable state, reducing time spent on manual tasks.
Now that you have a comprehensive guide at your disposal, it’s time to implement CI/CD in your own projects. Embrace automation, improve code quality, and deliver your applications faster with Azure and NestJS!