Showing posts with label Azure DevOps pipeline. Show all posts
Showing posts with label Azure DevOps pipeline. Show all posts

Wednesday, January 28, 2026

How to integrate SonarQube Cloud with Azure DevOps YAML Pipeline | SonarQube Cloud Integration with Azure DevOps | Automate Code Scan using SonarQube Cloud In Azure YAML Pipelines

Please find steps below for integrating SonarQube Cloud to perform static code analysis from Azure DevOps and automate this workflow by writing Azure devops yaml pipeline.




Pre-requisites in SonarQube Cloud:


    Click on Azure DevOps, enter your Microsoft credentials.
    Create an Organization, click on Import from a DevOps platform

    Create a Token in Azure DevOps with Read & Write Access under Code:

    Import organization details. Select free plan.


    Pre-requisites in Azure DevOps:

    • Azure DevOps Account
    • Make sure Java Project is setup in Azure Repos and default branch is either main or master.
    • Make sure you install SonarCloud plug-in/Add-on in Azure DevOps using below URL:
    How to add SonarQube Cloud plug-in in Azure DevOps?

    And look for SonarQube Cloud Add-on





    Once added SonarQube plug-in, click on proceed to Organization..



    How to integrate SonarQube Cloud with Azure DevOps:

    Create Token in SonarQube Cloud to authenticate with Azure DevOps
    You need to login to SonarQube using your admin password. admin/admin123 and click on Admin on your top side.
    Click on My Account, Security. 
    Under Tokens, Give some value for token name and choose Global analysis token, click on generate Tokens. Copy the token value generated.


    Create Service Connections in Azure DevOps 

    Login to Azure DevOps. Select your project dashboard.



    Click on Project settings --> Service connections


    click on New service connection

    Type SonarQube and Click Next

    Enter SonarQube server url as https://sonarcloud.io/
    and enter Token created 
    Give name for service connection and select grant access permission to all pipelines.
    Click on Save.




    Create a YAML Pipeline in Azure DevOps

    1. Login to Azure DevOps. Go to Azure Pipelines. Click on create a new pipeline, Select GitHub:

    2. Select your GitHub repo and select the Maven as YAML pipeline template

    3. Click on show assistant on right hand side, type SonarQube and select Prepare Analysis on SonarQube task and then select Service connection from the drop down and choose Integrate with Maven or Gradle option and then click on Add task



    Sample Code for entire pipeline is here below

    Azure DevOps Pipeline YAML Code:

    trigger:
    - main

    pool:
    vmImage: ubuntu-latest

    steps:
    - task: SonarCloudPrepare@4
    inputs:
    SonarQube: 'my_sonar_cloud'
    organization: 'mydevopscoachingapp'
    scannerMode: 'CLI'
    configMode: 'manual'
    cliProjectKey: 'MyDevopsCoachingApp_mySep2025WeekendRepo'
    cliProjectName: 'MyWebApp'
    - task: Maven@4
    inputs:
    mavenPomFile: 'MyWebApp/pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.17'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'clean install sonar:sonar'






    Click on Save and Queue to kick start build.
    Now login to SonarCloud dashboard, click on Projects:





    Sunday, September 8, 2024

    Azure DevOps Pipelines | Type of Azure DevOps pipelines | Classic vs Yaml Pipelines

    Azure DevOps offers two main types of pipelines for automating workflows: Classic Pipelines and YAML Pipelines


    1. Classic Pipelines (GUI-based Pipelines)

    These pipelines are created and managed using a graphical user interface (GUI) in Azure DevOps. They are suitable for users who prefer a no-code or low-code approach and need an easier setup. Classic pipelines consist of:

    • Build Pipelines: Automates the process of compiling code, running tests, and producing binaries.
    • Release Pipelines: Manages the deployment of applications to various environments such as staging, production, etc.

    Features:

    • Drag-and-drop interface.
    • Predefined templates.
    • Suited for users who prefer visual, step-by-step management of tasks.

    Example:

    Click here for configuring Azure DevOps Classic pipeline for a Java Web App.

      2. YAML Pipelines (Code-based Pipelines)

      These pipelines are defined as code using YAML syntax, giving you full control over the pipeline's configuration. YAML pipelines offer a more flexible, scalable, and maintainable approach for CI/CD.

      Key features:

      • Continuous Integration (CI): Automates the process of integrating code changes from multiple contributors in a shared repository.
      • Continuous Delivery (CD): Automates the deployment of applications to production environments.

      Advantages:

      • Fully version-controlled, as the pipeline definition is part of the codebase.
      • Supports multi-stage pipelines, allowing you to define build, test, and deployment stages in a single file.
      • Ideal for more complex workflows and teams that require a more DevOps-oriented, infrastructure-as-code approach.
      Example: 

      Click here for configuring Azure DevOps YAML pipeline for a Java Web App.

      Wednesday, May 15, 2024

      How to integrate SonarQube with Azure DevOps YAML Pipeline | SonarQube Integration with Azure DevOps | How to Automate Code Scan using SonarQube In Azure YAML Pipelines

      Please find steps below for integrating SonarQube to perform static code analysis using Azure DevOps YAML pipeline.



      Pre-requisites:

      How to add SonarQube plug-in in Azure DevOps?

      https://marketplace.visualstudio.com/acquisition?itemName=SonarSource.sonarqube


      Once added SonarQube plug-in, click on proceed to Organization..



      How to integrate SonarQube with Azure DevOps:

      Create Token in SonarQube to authenticate with Azure DevOps
      You need to login to SonarQube using your admin password. admin/admin123 and click on Admin on your top side.
      Click on My Account, Security. 
      Under Tokens, Give some value for token name and choose Global analysis token, click on generate Tokens. Copy the token value generated.


      Create Service Connections in Azure DevOps 

      Login to Azure DevOps. Select your project dashboard.



      Click on Project settings --> Service connections


      click on New service connection

      Type SonarQube and Click Next

      Enter SonarQube server url and enter Token created 
      Give name for service connection and select Grant access permission to all pipelines.
      Click on Save.

      Create a YAML Pipeline in Azure DevOps

      1. Login to Azure DevOps. Go to Azure Pipelines. Click on create a new pipeline, Select GitHub:

      2. Select your GitHub repo and select the Maven as YAML pipeline template

      3. Click on show assistant on right hand side, type SonarQube and select Prepare Analysis on SonarQube task and then select Service connection from the drop down and choose Integrate with Maven or Gradle option and then click on Add task



      Sample Code for entire pipeline is here below

      Azure DevOps Pipeline YAML Code:

      # Maven
      # Build your Java project and run tests with Apache Maven.
      # Add steps that analyze code, save build artifacts, deploy, and more:

      trigger:
      - master

      pool:
        vmImage: ubuntu-latest
      steps:
      - task: SonarQubePrepare@5
        inputs:
          SonarQube: 'My_SonarQube'
          scannerMode: 'Other'
      - task: Maven@3
        inputs:
          mavenPomFile: 'pom.xml'
          mavenOptions: '-Xmx3072m'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: '1.17'
          jdkArchitectureOption: 'x64'
          publishJUnitResults: true
          testResultsFiles: '**/surefire-reports/TEST-*.xml'
          goals: 'clean install sonar:sonar'
      - task: PublishTestResults@2
        inputs:
          testResultsFormat: 'JUnit'
          testResultsFiles: '**/surefire-reports/TEST-*.xml'
          failTaskOnFailedTests: true





      Click on Save and Queue to kick start build.
      Now login to SonarQube dashboard, click on Projects



      Watch steps in YouTube channel:

      Tuesday, April 23, 2024

      Automate Azure App Service setup using Ansible and Azure DevOps pipeline | How to integrate Ansible with Azure DevOps | How to Create WebApp in Azure Cloud using Ansible

      Ansible is an open-source, configuration management tool that automates cloud provisioning, configuration management, and application deployments. 
      Ansible Playbooks
      Ansible playbooks allow you to direct Ansible to configure your environment. Playbooks are coded using YAML so as to be human-readable. 
      Watch steps in YouTube channel:

      Automate Azure Web App setup using Ansible and Azure pipeline




      Integrate Ansible with Azure Cloud
      Integrating Ansible with Microsoft Azure allows you to automate and manage your Azure infrastructure using Ansible playbooks and modules. Ansible provides a collection of Azure-specific modules that enable you to provision and configure resources in Azure.


      To configure Azure credentials, you need the following information:

      • Your Azure subscription ID and tenant ID
      • The service principal application ID and secret

      Pre-requisites:

      Login to Azure

      az login

      Enter Microsoft credentials

      Create Azure Service Principal

      Run the following commands to create an Azure Service Principal:

      az ad sp create-for-rbac --name ansible-azure-sp --role Contributor --scopes /subscriptions/<subscription_id>
      Save the above output in a file as you will not be able retrieve later.
      Create an Ansible playbook - create-linux-app-svc.yml

      Create a simple playbook to create resource group in Azure and also a Azure App Service. Make sure you modify the name of the resource group, Azure WebApp and location below.


      - hosts: localhost
      connection: local
      vars:
      resource_group: myResourceGroup
      webapp_name: myfirstAwesomeWebApp
      plan_name: myAppServicePlan
      location: eastus
      tasks:
      - name: Ensure resource group exists
      azure_rm_resourcegroup:
      name: myResourceGroup
      location: East US
      register: rg_result
      - debug:
      var: rg_result
      - name: Create App Service on Linux with Java Runtime
      azure_rm_webapp:
      resource_group: "{{ resource_group }}"
      name: "{{ webapp_name }}"
      plan:
      resource_group: "{{ resource_group }}"
      name: "{{ plan_name }}"
      is_linux: true
      sku: S1
      number_of_workers: 1
      frameworks:
      - name: "java"
      version: "8"
      settings:
      java_container: tomcat
      java_container_version: 9.0

      Create Azure YAML build pipeline:

      Login to Azure Devops --> https://dev.azure.com

      Select project dashboard.

      Go to Pipelines -> New pipeline --> Click on Azure Repos Git or any SCM where you have playbooks stored. Select repo, click on Starter pipeline.

      Add below four pipeline variables with value received from service principal creation.

      AZURE_SUBSCRIPTION_ID
      AZURE_CLIENT_ID
      AZURE_SECRET
      AZURE_TENANT
      Add below tasks:
      • Install Ansible on build agent
      • Install Ansible rm module on build agent
      • Execute Ansible playbook for creating resource group in Azure cloud.
      trigger:
      - main
      pr: none # Disable PR triggers, can be adjusted as needed
      pool:
      vmImage: 'ubuntu-latest'
      steps:
      - script: |
      # Install Ansible
      pip3 install "ansible==2.9.17"
      displayName: 'Install Ansible'
      - script: |
      # Install Ansible rm module
      pip3 install ansible[azure]
      displayName: 'Install Ansible rm module'
      - script: |
      # Run Ansible playbook to create Azure App Service
      ansible-playbook create-linux-app-svc.yml
      displayName: 'Run Ansible Playbook'
      env:
      AZURE_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID)
      AZURE_CLIENT_ID: $(AZURE_CLIENT_ID)
      AZURE_SECRET: $(AZURE_SECRET)
      AZURE_TENANT: $(AZURE_TENANT)

      Save the pipeline and run it.


      Now Login to Azure cloud to see if the App Service have been created.

      Clean up service principal & Resource Group

      az ad sp list --display-name ansible-azure-sp --output table

      az ad sp delete --id <pass_the_id>

      az group delete --name myResourceGroup --yes --no-wait

      Delete Resource group and App Service using Ansible playbook: delete-linux-app-svc.yml
      - name: Delete Azure App Service
        hosts: localhost
        connection: local
        vars:
          resource_group: myResourceGroup
          webapp_namemyfirstAwesomeWebApp
        tasks:
        - name:
          azure_rm_webapp:
            name: "{{ webapp_name }}"
            resource_group: "{{ resource_group }}"
            state: absent

      Thursday, January 18, 2024

      How to Enable to use the classic editor to create a pipeline without YAML in Azure DevOps | Enable Classic Pipeline Option in Azure DevOps

      How to Enable to use classic editor to create a pipeline without YAML in Azure DevOps ?

      The "classic pipeline" is simply a term that refers to a simple way of creating pipelines in Azure DevOps using UI. The purpose of this web forms based assistant is basically to hide the complexity of the pipeline’s YAML based syntax. In other words, user can create pipelines without having to deal with “code”. 

      If classic editor option is disabled in Azure DevOps, you may see something like below in your Azure DevOps project. We can enable it by changing the settings at project or organizational level.


      How to enable the classic build and release pipelines?

      You can enable/disable it two ways
      • Organizational level
      • Project Level

      In Azure DevOps,  Go to Project Settings, Under Pipelines > Settings > General. Make sure 'Disable creation of classic build pipelines' and 'Disable creation of classic release pipelines' is turned off, to have classic editor shown after creating a project.


      If those options are disabled, go to organizational level and do the same in 'Organization Settings'.


      Select Pipelines --> Settings

      Finally disable the options highlighted in the image below
      when you go try creating a new pipeline, you should see the option now.


      Watch Steps in YouTube channel:

      Thursday, November 30, 2023

      Azure DevOps Pipeline Optimization Best Practices | Optimizing Azure DevOps pipelines

      Optimizing Azure DevOps pipelines is crucial for achieving faster and more efficient software delivery. Here are some best practices and strategies for optimizing Azure DevOps pipelines:

      1. Parallel Jobs and Stages:

      • Parallelization: Break down your pipeline into parallel jobs and stages to execute tasks concurrently, reducing overall pipeline execution time.
      jobs:
      - job: Build
        pool:
          vmImage: 'windows-latest'
        steps:
          - script: echo "Building..."
      - job: Test
        pool:
          vmImage: 'windows-latest'
        steps:
          - script: echo "Testing..."

      2. Agent Pools and Agents:
      • Agent Pools: Distribute builds across multiple agent pools to utilize available resources effectively. Configure agent capabilities to match job requirements.

      3. Artifact Caching:

      • Cache Dependencies: Utilize caching to store and retrieve build artifacts between different pipeline runs, reducing the time spent on redundant build steps.
      steps: - task: Cache@2 inputs: key: 'node | "$(Agent.OS)" | package-lock.json' path: '**/node_modules'

      4. Incremental Builds:

      • Trigger on Changes: Set up your pipeline to trigger builds only for changes in relevant branches. Use CI triggers to avoid unnecessary builds.

      5. Artifact Promotion:

      • Promote Artifacts: Promote artifacts from one environment to another instead of rebuilding them. This helps maintain consistency across environments and reduces build times.

      6. Use YAML Pipelines:

      • YAML Syntax: Use YAML-based pipelines for better version control and code review. YAML pipelines are more maintainable and offer a clearer representation of your CI/CD process.

      7. Job and Step Conditions:

      • Conditions: Use conditions to selectively execute jobs or steps based on criteria such as branch names, variable values, or expressions.

      • jobs: - job: Deploy condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main')) steps: - script: echo "Deploying..."

      8. Agent Clean-Up:

      • Clean Workspace: Include steps to clean up the agent workspace at the end of each build to avoid accumulation of unnecessary artifacts and files.
        steps: - script: echo "Build steps..." - task: DeleteFiles@1 inputs: contents: '**' cleanTargetFolder: true

      9. Multi-Stage Docker Builds:

      • Multi-Stage Builds: Utilize multi-stage Docker builds to create smaller and more efficient Docker images, reducing image size and improving deployment speed.

      10. Azure Container Registry (ACR) Tasks:

      - **ACR Build and Push:** Use Azure Container Registry Tasks for building and pushing Docker images directly within the pipeline, reducing the need for external scripts.

      - task: ACRBuild@2
        inputs:
          azureSubscription: '<AzureServiceConnection>'
          resourceGroupName: '<ResourceGroupName>'
          registry: '<ACRName>'
          imageName: '<ImageName>'
          dockerfilePath: '<DockerfilePath>'

      11. Deployment Strategies:
      - **Deployment Strategies:** Choose appropriate deployment strategies such as rolling deployments, canary releases, or blue-green deployments based on your application's requirements.
      12. Automated Testing:
      - **Automated Tests:** Integrate automated tests into your pipeline to catch issues early. Azure DevOps supports various testing frameworks and test runners.

      13. Parameterize Pipelines:

      - **Pipeline Parameters:** Parameterize your pipelines to make them more flexible and reusable across different environments or scenarios.

      14. Infrastructure as Code (IaC):
      - **IaC:** Treat your infrastructure as code. Use Azure Resource Manager (ARM) templates or Terraform scripts for defining and deploying infrastructure.

      15. Use Deployment Gates:
      - **Gates:** Implement deployment gates to add quality checks before promoting changes to the next environment. Gates can include approvals, automated tests, or custom conditions.

      Optimizing Azure DevOps pipelines is an iterative process. Regularly review and enhance your pipeline configurations to incorporate new best practices and improvements. Consider the specific needs and constraints of your projects when implementing optimizations.

      🚀 Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp – Sep 2026

      Live AI-Enabled DevSecOps & Cloud Engineering Bootcamp from Coach AK - Sep 2026 Schedule