Search code examples
jenkinssharepointjenkins-pipelinecicd

How to upload files from Jenkins to SharePoint: REST API or OneDrive sync?


I’m working on a CI/CD pipeline using Jenkins, where part of the process involves uploading build artifacts to an intranet SharePoint. Currently, I have two possible approaches in mind:

  1. Using the SharePoint REST API: modify the Jenkinsfile to include a step that uses curl (or a similar tool) to send files to SharePoint, treating it like a REST API. I prefer this approach as it keeps the pipeline configuration entirely as code.

  2. Using OneDrive sync: install OneDrive on the server hosting Jenkins, configure a local folder to sync with a SharePoint folder, and have the pipeline drop artifacts into this local folder.

My concerns are:

  • For approach 1, does Microsoft SharePoint provide a REST API for uploading files? If yes, are there any examples or libraries that can simplify this process?
  • For approach 2, I dislike the idea of modifying server infrastructure (e.g., installing and configuring OneDrive), as it goes against the principle of maintaining the pipeline entirely as code. This approach may also introduce portability issues if we migrate to a cloud-based IaaS solution in the future.

Are there better alternatives for integrating Jenkins with SharePoint for artifact upload? If not, which of these approaches is more reliable and maintainable in the long term?


Solution

  • SharePoint does provide a Rest API to upload files.

    Example REST API Request:

    POST /sites/your-site/_api/web/GetFolderByServerRelativeUrl('/sites/your-site/your-library')/Files/Add(url='your-file-name.zip', overwrite=true)
    Host: your-sharepoint-site.sharepoint.com
    Authorization: Bearer your-access-token
    Content-Type: application/octet-stream
    Content-Length: your-file-size
    
    (binary content of your-file-name.zip)
    

    Reference:

    Working with large files by using REST

    Upload a file by using the REST API and jQuery