Overview
CI (Continuous Integration) and CD (Continuous Deployment) helps to automate the business processes. With the help of Office 365 CLI, the SPFx solutions can be automated for CI and CD.
In this article, we will explore the build and release pipelines for SPFx library components and a consumer web part.
The Solution Structure
Two independent SPFx solutions are set up as below:
- spfx-library: The SPFx library component, which is independently versioned and deployed through tenant app catalog.
- spfx-library-consumer: The SPFx web part which consumes the above library.
The Catch for Build Pipeline
There is a difference in the mechanism of how the library is consumed from local and from tenant app catalog.
To consume an SPFx library for local testing:
To consume an SPFx library for local testing:
- Run ‘npm link’ from the root directory of the library solution to create a local npm link.
- From the root of the web part folder, run the command ‘npm link spfx-library'.
To consume an SPFx library from tenant app catalog:
- Deploy spfx-library package in the tenant app catalog (tenant wide).
- In the web part’s package.json refer library in the dependencies section.
- "dependencies": {
- "spfx-library": "0.0.1", // here we added the reference to the library
- "@microsoft/sp-core-library": "1.9.0",
- "@types/webpack-env": "1.13.1",
- "@types/es6-promise": "0.0.33"
- },
The catch here for setting up the build pipeline is as below:
- For the web part to build during the build pipeline, it should have symlink to the library locally.
- For the web part to work on SharePoint site post-deployment, it should have library dependency specified in the package.json
- However, if we specify the library dependency in pakage.json, during the build it will try to get the package from npm as “GET https://registry.npmjs.org/spfx-library” and the build will fail.
Workaround
To avoid the above issue, I am maintaining a copy of package.json with library dependency inside folder “postinstall”.
To avoid the above issue, I am maintaining a copy of package.json with library dependency inside folder “postinstall”.
After the ‘npm install’ and ‘npm link’ on the web part solution, we will replace the web part solution’s package.json file with above file. The subsequent bundle and package solution activities will process this package.json. When the web part is deployed to SharePoint, it will be able to refer the library component.
Set Up Build Pipeline
We will independently install npm packages, bundle, and package the library and web part. To achieve continuous build, create a build definition and add below steps to it.Install Node JS
- On the default agent, click the + sign.
- Search for “Node”.
- Add the Node.js tool installer.
- Specify the version as 10.x.
Restore npm Packages (for library component)
SharePoint Framework solution uses third-party npm packages. We need to restore those before starting the build process. This step will restore npm packages for the library component.
- Add npm task.
- Verify if the command is set to install.
Build the Library Solution
Build the SPFx solution to minify the required assets to upload to CDN.
- Add gulp task.
- Set the Gulp file path to gulpfile.js.
- Set the Gulp task as bundle.
- Set Gulp arguments to --ship.
Package the Library Solution
The next step is to combine the assets into a package.
- Add gulp task.
- Set the Gulp file path to gulpfile.js.
- Set the Gulp task as package-solution.
- Set Gulp arguments to --ship.
Reference library component type
Consume an SPFx library in the build definition locally.
- Add npm task.
- Under ‘Command’, select custom.
- In the ‘Command and Arguments’, type ‘link’.
Prepare the artifacts for library component
Azure DevOps build does not retain any files. The .sppkg file created from the above step needs to be copied to the staging directory to be published to release pipeline.
- Add the “Copy Files” task.
- Set “Source Folder” to $(Build.Repository.LocalPath)/ spfx-library-component/spfx-library/sharepoint/solution.
- Set “Contents” to *.sppkg.
- Set target folder to $(Build.ArtifactStagingDirectory)/drop.



















payal kapoorPosted Jun 10, 2020, 6:41 AM
Can we modify the build pipeline in way so that I don't have to deploy my library component into appCatalog. I've installed it in my solution as a npm package and referring the published version in webpart package.json file ?
Ano MepaniPosted Oct 23, 2019, 1:19 PM
Can we run multiple o365 command in single script as per shown in your demo?
Ano MepaniPosted Oct 20, 2019, 8:12 AM
Very useful article. Is it possible to share build and release pipelines yml file. It will helpful to verify steps and troubleshoot while CI CD configuration