In this guide, we’ll walk you through creating an Azure DevOps project, setting up a pipeline that triggers on code changes, and automatically deploying your application to an Azure Web App. This setup ensures that every time you update your code, a new build is triggered, and the latest version is deployed seamlessly.
Table of Contents
- Create an Azure DevOps Project
- Initialize a Git Repository
- Add Sample Code
- Create a Service Connection
- Create the CI/CD Pipeline
- Configure the Trigger for Code Changes
- Deploy to Azure Web App
- Run and Monitor the Pipeline
- Conclusion
Step 1: Create an Azure DevOps Project
- Go to the Azure DevOps Portal.
- Sign in with your Microsoft account or create one if you don’t have it.
- Click Create New Organization and set up your organization.
- Create a new project:
-
Project Name:
MyFirstDevOpsProject - Visibility: Private or Public
-
Project Name:
- Click Create.
Step 2: Initialize a Git Repository
- In your Azure DevOps project, navigate to Repos.
- Click Initialize to create a new repository.
- Clone the repository locally:
bash
Copied!git clone <your-repo-url> cd MyFirstDevOpsProject
Step 3: Add Sample Code
Create a simple Node.js application:
-
Create
index.js:
bash
Copied!touch index.js
2. Add the following code:
javascript
Copied!const http = require('http');const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain');res.end('Hello, Azure DevOps CI/CD Pipeline!\n'); }); server.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
3. Commit and push the changes:
bash
Copied!git add index.jsgit commit -m "Add sample Node.js application"git push origin main
Step 4: Create a Service Connection
A Service Connection allows Azure DevOps to securely connect and deploy to your Azure resources.
Steps to Create a Service Connection:
-
Navigate to Project Settings:
- Go to your Azure DevOps project.
- Click Project Settings (gear icon at the bottom left).
-
Create a New Service Connection:
- Under Pipelines, select Service Connections.
- Click New Service Connection > Azure Resource Manager.
-
Fill in the Details:
- Scope Level: Select Subscription.
- Subscription: Choose your Azure Subscription.
- Resource Group: Select the relevant Resource Group or leave it as All Resource Groups.
-
Service Connection Name: e.g.,
my-azure-connection. - Grant Access Permission: Check Grant access permission to all pipelines.
- Save the Service Connection.
Now your pipeline can authenticate and deploy to Azure using this connection.
Step 5: Create the CI/CD Pipeline
- In Azure DevOps, go to Pipelines > New Pipeline.
- Select Azure Repos Git and choose your repository.
- Select Starter Pipeline.
- Replace the content with the following
azure-pipelines.yml:
yaml
Copied!trigger: branches: include: - main paths: include: - '**/*.js' - '**/*.json' pool: vmImage: 'ubuntu-latest' steps: - task: ArchiveFiles@2 inputs: rootFolderOrFile: '$(System.DefaultWorkingDirectory)' includeRootFolder: false archiveType: 'zip' archiveFile: '$(Build.ArtifactStagingDirectory)/app.zip' replaceExistingArchive: true - task: AzureWebApp@1 inputs: azureSubscription: 'my-azure-connection' # Replace with your Service Connection name appName: 'APPDevOps' # Replace with your Web App name package: '$(Build.ArtifactStagingDirectory)/app.zip'
5. Save and Run the pipeline.
Step 6: Verify the Trigger for Code Changes
The trigger section in the pipeline ensures that builds are triggered automatically when specific files are changed:
yaml
Copied!trigger: branches: include: - main paths: include: - '**/*.js' # Trigger on changes to JavaScript files exclude: - README.md # Exclude changes to README files
This ensures the pipeline runs when you update files like index.js.
Step 7: Deploy to Azure Web App
Create an Azure Web App
- Go to the Azure Portal.
- Navigate to Create a Resource > Web App.
- Fill in the following details:
-
Name:
APPDevOps -
Runtime Stack:
Node 20 LTS - Region: Choose the closest region.
-
Name:
- Click Create.
Your Azure Web App is now ready to receive deployments.
Step 8: Run and Monitor the Pipeline
- Make a change to
index.js:
bash
Copied!echo "console.log('New deployment triggered!');" >> index.js
-
Commit and push:
bash
Copied!git add index.js git commit -m "Update index.js with new changes" git push origin main
- Go to Pipelines > Runs in Azure DevOps.
- Monitor the build and deployment process.
- Once complete, visit your Azure Web App URL:
arduino
Copied!https://<your-webapp-name>.azurewebsites.net
Conclusion
What We Achieved
- Created an Azure DevOps Project.
- Initialized a Git Repository and added sample code.
- Created a Service Connection to securely deploy to Azure.
-
Configured a CI/CD Pipeline that:
- Automatically triggers on code changes.
- Archives the application files.
- Deploys the updated code to an Azure Web App.
This streamlined workflow ensures your latest code is deployed seamlessly with minimal manual intervention. Happy DevOps-ing!

Leave a Reply