clechner77
10/23/2017 - 4:12 AM

Complete Sharepoint Framework SPFX Bundle Build Upload Deploy Process with Gulp and Powershell

Completely automates Sharepoint Framework build and deployment, including uploading files to Sharepoint Document Library and app package to app catalog, increments SPFx version number, and deploys/upgrades it. You do not need to touch Sharepoint -- this fully automates the build/deploy process

<# place deploy.ps1 in root of SPFx project #>
cls
Write-Host "Removing SHAREPOINT and TEMP directories..." -foregroundcolor "green"
$spFolderExists = test-path -Path .\sharepoint
$tempFolderExists = test-path -Path .\temp
if ($spFolderExists) { 
    Remove-Item .\sharepoint -Force -Recurse 
    Write-Host "Deleted SharePoint solution folders..." -foregroundcolor "green"
} 

if ($tempFolderExists){ 
    Remove-Item .\temp -Force -Recurse 
    Write-Host "Deleted temp folders..." -foregroundcolor "green"
}

Write-Host "Auto-incrementing SPFx Solution Version..."  -foregroundcolor "green"

# The following auto-increments the version number of the SPFx solution
$tokens = $null
$config = Get-Content '.\config\package-solution.json' -raw | ConvertFrom-Json
$tokens = $config."solution".version.Split(".")
$major = [int]( $tokens[0])
$minor = [int]( $tokens[1])
$patch = [int]( $tokens[2])
$fix = [int]( $tokens[3])
$new_fix = $fix + 1    # updates patch... must update below
$Version = ([string] $major)+"."+ ([string] $minor ) +"." + ([string]$patch)+"." + ([string]$new_fix)

$config.update | % {
   $config."solution".version = $version
   Write-Host "Updated to" $Version
   }

$config | ConvertTo-Json  | set-content '.\config\package-solution.json'

# Bundling, packaging, uploading, deploying, etc
gulp bundle --ship
Write-Host "Bundling app..." -foregroundcolor "green"

gulp package-solution --ship
Write-Host "Packaging app..." -foregroundcolor "green"

$site = "https://mlmins.sharepoint.com"
$username = "svcdirsync@mlmins.onmicrosoft.com"
$password = "Kofo5273"
$encpassword = convertto-securestring -String $password -AsPlainText -Force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword

Connect-PnPOnline -Url $site -Credentials $creds
Write-Host "Connected to:" $site

$pkgPath = "./sharepoint/solution/"
$apppkg = Get-ChildItem $pkgPath -Filter *.sppkg
$path = $pkgPath + $apppkg

Add-PnPApp -Path $path -Publish -Overwrite
Write-Host $apppkg "uploaded and deployed" -foregroundcolor "green"
Write-Host "Completed" -foregroundcolor "green"