Showing posts with label CI/CD. Show all posts
Showing posts with label CI/CD. Show all posts

May 29, 2025

Streamline SPFx Builds with Azure DevOps CI/CD Pipeline – Part 1: Setting Up Continuous Integration

Introduction

If you're developing SharePoint Framework (SPFx) solutions, streamlining your build process is key. In this post, we’ll walk through setting up a Continuous Integration (CI) pipeline using Azure DevOps to automatically prepare your SPFx solution whenever you push code.


Step 1: Set Up the Repo

  • First, create a DevOps repository and add your SPFx source code into a dedicated folder.


Step 2: Create the CI Pipeline

  • Navigate to Pipelines: In Azure DevOps, go to the Pipelines section and click Create Pipeline.

  • Classic Editor: Choose the classic editor for easier configuration.

  • Repo & Branch: Select your repository and branch, then continue.

 

Step 3: Configure Pipeline Jobs

  • Empty Job: Choose the empty job template.
  • Add Tasks:

    1. Node.js Tool Installer : Set the Node.js version required for your SPFx project.
    2. NPM Install: Add the npm task to install dependencies (install command).
    3. Gulp Clean: Add the gulp task with clean.
    4. Gulp Build: Repeat the above for build.
    5. Gulp Bundle: Add another gulp task for bundle.
    6. Gulp Package Solution: Finally, a gulp task for package-solution

    Step 4: Handle Artifacts

    • Copy Files: Add a task to copy the generated .sppkg file from the solution folder to the drop folder.

    Step 5: Publish Artifacts

    Add a task to publish the pipeline artifacts for the next stage.

    Step 6: Set Up Triggers

    In the trigger settings, define the source code path. This ensures that every commit triggers the pipeline automatically.


    Conclusion:

    With this setup, every push to your repository will automatically trigger the CI pipeline, ensuring your SPFx solution is built and packaged consistently.

    Next Up: Part 2 – Deploy SPFx Solution to SharePoint App Catalog using Azure DevOps, we’ll cover how to automate the deployment of the .sppkg file to your SharePoint App Catalog as part of a Continuous Deployment (CD) pipeline.

    If you have any questions you can reach out our SharePoint Consulting team here.

    Streamline SPFx Builds with Azure DevOps CI/CD Pipeline – Part 2: Automating Deployment (CD)

    Introduction

    In the First Part, we built a Continuous Integration (CI) pipeline to automatically package our SharePoint Framework (SPFx) solution. Now it’s time to automate deployment across multiple SharePoint sites using Continuous Deployment (CD).

    This blog covers setting up a CD pipeline in Azure DevOps, triggered automatically after your CI pipeline finishes, to deploy your .sppkg file to all the required sites. 


    Step 1: Prepare Your PowerShell Deployment Script

    We’ll use PowerShell with PnP.PowerShell to handle the deployment. This script connects to SharePoint Online, uploads the .sppkg package, and publishes it on each target site.

    Here's the script:

      Param (  
       [string] $RootPath = $(Throw "Root Path is required."),  
       [string] $ClientId = $(Throw "ClientId is required."),  
       [string] $TenantId = $(Throw "TenantId is required."),  
       [string] $TenantURL = $(Throw "Tenant URL is required."),  
       [string] $ClientSecret = $(Throw "Client Secret is required."),  
       [string] $packagename = $(Throw "Package name is required.")  
     )  
     $SiteURL = "$($TenantURL)/sites/TestSite"  
     $ModernPOPAppPath = "$RootPath\drop\$packagename"  
     $currentPOPSite = ""  
     function ReconnectPNPConnection {  
       Write-Host "Reconnecting to site $($currentPOPSite)"  
       Disconnect-PnPOnline  
       Connect-PnPOnline -Url $currentPOPSite -ClientId $ClientId -ClientSecret $ClientSecret -Tenant $TenantId  
     }  
     function addCustomSPFxApps {  
       $appCatalog = Get-PnPSiteCollectionAppCatalog -CurrentSite -ErrorAction SilentlyContinue  
       while ($null -eq $appCatalog) {  
         Write-Host "Waiting for app catalog creation..."  
         Start-Sleep -Seconds 30  
         $appCatalog = Get-PnPSiteCollectionAppCatalog -CurrentSite -ErrorAction SilentlyContinue  
       }  
       $uploadApp = $false  
       while ($uploadApp -eq $false) {  
         try {  
           Write-Host "Uploading SPFx app..."  
           $App = Add-PnPApp -Path $ModernPOPAppPath -Scope Site -Overwrite -Timeout 900  
           Write-Host "Publishing SPFx app..."  
           Publish-PnPApp -Identity $App.ID -Scope Site -SkipFeatureDeployment -ErrorAction SilentlyContinue  
           Write-Host "SPFx app deployed successfully."  
           Start-Sleep -Seconds 60  
           $uploadApp = $true  
         } catch {  
           Write-Host $_.Exception.Message  
           Write-Host "Retrying after 30 seconds..."  
           Start-Sleep -Seconds 30  
           ReconnectPNPConnection  
         }  
       }  
     }  
     Write-Host -f Cyan "Installing PnP.PowerShell module..."  
     [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12  
     Install-Module PnP.PowerShell -RequiredVersion 2.4.0 -Force -Scope CurrentUser  
     Import-Module -Name "PnP.PowerShell"  
     Write-Host "Connecting to $($SiteURL)..."  
     Connect-PnPOnline -Url $SiteURL -ClientId $ClientId -ClientSecret $ClientSecret -Tenant $TenantId  
     $currentPOPSite = $SiteURL  
     addCustomSPFxApps  
     Write-Host "Disconnecting from $($SiteURL)..."  
     Disconnect-PnPOnline  
    

    We need to upload this PowerShell script to a document folder as shown below and add a step in the CI (Continuous Integration) pipeline to copy this file into the artifacts.


    Step 2: Set Up the CD Pipeline in Azure DevOps

    1. Create the Release Pipeline:

    • Navigate to the Release section in Azure DevOps.
    • Click New Pipeline.

    2. Add Artifacts:

    • Link your CI pipeline’s artifacts (the .sppkg file and related files).test

    3. Enable Continuous Deployment Trigger:

    • Click the lightning bolt icon next to the artifact and enable the trigger to deploy automatically after CI.

    4. Add a Stage:

    • Click Add a stage and choose Empty Job.

    5. Add PowerShell Task:

    • In the stage, click to add a task.
    • Choose PowerShell.
    • Set the script path to point to your deployment script (e.g., Documents/CDScript.ps1).
    • Set the necessary parameters (RootPath, ClientId, TenantId, etc.).

    6. Save and Deploy:

    • Save the pipeline and test it by running the full CI/CD process.

    Conclusion

    By integrating both CI and CD pipelines in Azure DevOps, you’ve established a fully automated and reliable deployment process for your SPFx solutions:

    • CI Pipeline: Automatically builds and packages your SPFx solution on every code push.
    • CD Pipeline: Seamlessly deploys the package to all required SharePoint sites immediately after a successful build.

    This setup not only saves time but also ensures consistency and reduces the risk of manual deployment errors across environments.

    If you have any questions you can reach out our SharePoint Consulting team here.

    April 17, 2025

    Automating Azure Service App Deployment with Azure DevOps Pipelines

    Introduction

    In modern software development, Continuous Integration and Continuous Deployment (CI/CD) are crucial in ensuring smooth, automated deployments. Azure DevOps provides robust pipeline capabilities that enable developers to automate the deployment of their applications, reducing manual effort and minimizing errors.

    In this blog, we’ll walk through setting up an Azure DevOps pipeline for an Azure Service App, ensuring seamless deployment whenever changes are pushed to the repository.

    Setting Up the Azure DevOps Pipeline


    Before diving into the pipeline configuration, ensure you have:
    • An Azure Service App created in the Azure Portal.
    • service connection in Azure DevOps is linked to your Azure subscription.
    • Your Service App code is stored in a repository like Azure Repos, GitHub, or Bitbucket.

    Pipeline Configuration

    Below is a YAML-based Azure DevOps pipeline that automates the build and deployment of an Azure Service App.


    trigger:
      branches:
        include:
        - main # Change this to your branch if needed
      paths:
        include:
        - ServiceAppCode/*
    
    variables:
      azureSubscription: 'ServiceAppDeployment' # Azure service connection name
      serviceAppName: 'TestingApp' # Azure Service App name
      serviceAppPath: 'ServiceAppCode' # Path to Service App source code
      buildConfiguration: 'Release' # Build service app source code in release
      publishDirectory: '$(Build.ArtifactStagingDirectory)/publish'
    
    pool:
      vmImage: 'ubuntu-22.04' # Also use ubuntu-latest
    
    stages:
    - stage: Build
      displayName: 'Build Stage'
      jobs:
      - job: Build
        displayName: 'Build Job'
        steps:
        - task: UseDotNet@2
          displayName: 'Install .NET SDK'
          inputs:
            packageType: 'sdk'
            version: '8.0.x'
            includePreviewVersions: false
    
        - script: |
            echo "Cleaning up existing publish directory..."
            rm -rf $(publishDirectory)
            mkdir -p $(publishDirectory)
          displayName: 'Ensure Clean Publish Directory'
    
        - task: DotNetCoreCLI@2
          displayName: 'Restore Dependencies'
          inputs:
            command: 'restore'
            projects: '$(serviceAppPath)/*.csproj'
    
        - task: DotNetCoreCLI@2
          displayName: 'Build Service App'
          inputs:
            command: 'build'
            projects: '$(serviceAppPath)/*.csproj'
            arguments: '--configuration $(buildConfiguration) /p:WarningLevel=0' # /p:WarningLevel=0 Remove the warning at the time of build service app
    
        - task: DotNetCoreCLI@2
          displayName: 'Publish Service App'
          inputs:
            command: 'publish'
            projects: '$(serviceAppPath)/*.csproj'
            publishWebProjects: false
            arguments: '--configuration $(buildConfiguration) --output $(publishDirectory)'
            zipAfterPublish: true
    
        - task: PublishBuildArtifacts@1
          displayName: 'Publish Artifacts'
          inputs:
            pathToPublish: '$(publishDirectory)'
            artifactName: 'drop'
    
    - stage: Deploy
      displayName: 'Deploy Stage'
      dependsOn: Build
      condition: succeeded()
      jobs:
      - job: Deploy
        displayName: 'Deploy to Azure Service App'
        steps:
        - download: current
          displayName: 'Download Build Artifacts'
          artifact: 'drop'
    
        - task: AzureServiceApp@1
          displayName: 'Deploy to Azure Service App'
          inputs:
            azureSubscription: '$(azureSubscription)'
            appType: 'webApp' # functionApp for function app OR webApp for web app
            appName: '$(serviceAppName)'
            package: '$(Pipeline.Workspace)/drop/*.zip'

    Understanding the Pipeline

    1. Trigger Configuration

    • The pipeline is triggered when changes are pushed to the main branch.
    • It monitors specific folders where Service App updates are made.

    2. Defining Variables

    • azureSubscription: The name of the Azure DevOps service connection.
    • serviceAppName: The name of the Service App in Azure.
    • serviceAppPath: The directory containing the Service App source code.
    • publishDirectory: The folder where the build output is stored.

    3. Choosing the Right Agent Pool

    • The pipeline uses the Ubuntu 22.04 VM image for the build process

    4. Build Stage

    • Installing .NET SDK: Ensures that the required version is available.
    • Cleaning the Publish Directory: Prevents old files from interfering with new builds.
    • Restoring Dependencies: Ensures that all NuGet dependencies are downloaded.
    • Building the Service App: Compiles the service app with the specified configuration.
    • Publishing the Service App: Packages the compiled app for deployment.
    • Publishing Artifacts: Stores the published files as build artifacts for the deployment stage.

    5. Deploy Stage

    • Downloading Build Artifacts: Retrieves the published application files.
    • Deploying to Azure: Uses the AzureServiceApp@1 task to deploy the service app to Azure.

    Conclusion

    By setting up this Azure DevOps pipeline, you can automate the deployment of your Azure Service App, ensuring quick and error-free releases.

    With automation in place, you can focus on developing new features while Azure DevOps handles the heavy lifting of deployments!


    If you have any questions you can reach out our SharePoint Consulting team here.