0

I have a java application that I'm trying to deploy to a Windows server via SSH (via Jenkins). I wrote a powershell script to start the process in the background using start-process and javaw:

$SSL_KEYSTORE_PASSWORD = $args[0]
Write-Output "Running war in the background"
Start-Process javaw -ArgumentList "-jar", `
    "e:\app\app.war", `
    $("-Dserver.ssl.key-store-password=" + $SSL_KEYSTORE_PASSWORD), `
    "-Dserver.tomcat.basedir=e:\app" `
    -RedirectStandardOutput "e:\app\output\stdout.txt" `
    -RedirectStandardError "e:\app\output\stderr.txt"
Write-Output "Starting javaw instance"

When I run the script from the server, it works perfectly: the process runs in the background indefinitely and all the output goes to the right place. But, when I attempt to run it via SSH from jenkins, the java application will start up properly, but as soon as the powershell script ends, the process gets killed. Initially, I thought it was jenkins killing the process, but I did some debugging and figured out that it's getting killed as soon as it exits the powershell script, so I think it's related to SSH. This is the line in the jenkinsfile that's attempting to start up the process:

sh 'ssh -o StrictHostKeyChecking=no $ssh_target powershell e:\\app\\startup $SSL_KEYSTORE_PASSWORD'

I would like to start the process using a powershell script like this, because once I get this working, I'd like to add some other stuff into the powershell script to make sure it started up properly, and I don't want to clutter up the jenkinsfile even more than it already is.

1 Answers1

0

I figured out a way to do this using this answer, a comment on this answer, and a whole bunch of trial and error.

First of all, on an unrelated note, I had to switch the line order on the script for the app to start properly (it kept saying the ssl certificate password was incorrect even though it wasn't).

$SSL_KEYSTORE_PASSWORD = $args[0]
Write-Output "Running war in the background"
Start-Process javaw -ArgumentList "-jar", `
    $("-Dserver.ssl.key-store-password=" + $SSL_KEYSTORE_PASSWORD), `
    "-Dserver.tomcat.basedir=e:\app", `
    "e:\app\app.war" `
    -RedirectStandardOutput "e:\app\output\stdout.txt" `
    -RedirectStandardError "e:\app\output\stderr.txt"
Write-Output "Starting javaw instance"

Now, to get to the main question, the way I solved it was by using the Invoke-WmiMethod command mentioned in those two links.

The actual working line looks like this:

sh "ssh -o StrictHostKeyChecking=no $ssh_target powershell Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList \'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe e:\\app\\startup " + "${SSL_KEYSTORE_PASSWORD}" + "\'"

It turns out that I had to wrap whatever was in the -ArgumentList in single quotes (so, of course, I had to escape them) and that if I wanted to call a powershell script, I had to invoke the full file path of the powershell executable.

It works now at least.