How to declare PowerShell variable in Jenkins Pipeline

Problem

  • Jenkins Pipeline (Groovy) and PowerShell use the dollar sign ($) to access or declare variables. When you use dollar sign ($), Jenkins Pipeline will become confused. 

Solution

  • In powershell section change $yourVaraible to \$yourVaraible
  • Example
pipeline {
    agent any

    stages{
        //....
        stage ('PUBLISH') {
            steps {
                dir("\\Web\\ds.Invest.WebAPI"){
                   powershell(script: """
                     \$env:NODE_OPTIONS = "--openssl-legacy-provider"
                     dotnet publish ds.Invest.WebApi.csproj -c Release --no-build --no-restore      
                   """) 
                }  
            }
        }
    }
}

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.