powershelljenkinssubstitutionremote-execution

Jenkins parameter Release versus Staging


I created a freestyle job in Jenkins that I just set up (latest version).

I added parameters to it. One of those is a Options selection for ReleaseType with the options of Staging and Release.

One of the build steps is executing a remote command on the server when the site is uploaded to. It uses the Execute Windows Batch Command build step.

Here is the command line (with things made generic):

sexec myuser@mysite.com -pw=mypassword -cmd="PowerShell -Command ""C:\batch\bvCopyFast.ps1 C:\inetpub\mysite${ReleaseType}\siteLoad C:\inetpub\mysite${ReleaseType}\site""

Basically I am executing a powershell command that uses Robocopy to copy the files from the upload folder to the actual release folder for the site.

As you can see I need to have the ${ReleaseType} replaced with the actual value. The problem is that when this gets executed it isn't doing the substitution. I just uses that literal value in the command and that doesn't work.


Solution

  • If you use the -Command parameter it implies you are going to write raw PowerShell code in between the quotation marks that follow (allow you can call a script as you have).

    PowerShell -Command "Get-Date; pause;"
    

    To call a PowerShell script file you should use:

    PowerShell -File "Your-Script.ps1 -Parameter1 Argument1 -Parameter2 Argument2"
    

    https://learn.microsoft.com/en-us/powershell/scripting/components/console/powershell.exe-command-line-help?view=powershell-6

    I would write a PowerShell script that accepted your root path and the releaseType as arguments and execute that.

    Param($rootPath,$releaseType)
    {
       robocopy "$($rootPath)\$($releaseType)\siteLoad" "$($rootPath)\$($releaseType)\site"  
    }
    

    I have never used Jenkins so I hope this works as I expect it to!

    sexec myuser@mysite.com -pw=mypassword -cmd=""PowerShell -File 'C:\batch\newScript.ps1' -RootPath 'c:\inetpub\mysite' -ReleaseType {ReleaseType}""