How can I remotely trigger a groovy script on my Jira Server?

Rick Trudeau March 25, 2014

I want to provide a mechanim to automate the addition of new options to a select list in our Jira system. I have written a groovy script that does all the heavy lifting and uses the JIRA API to add the provided Options to the select list.

Using the ScriptRunner Script Console, I can run my script and have verified the functionality is correct. The final missing link is finding a way to remotely trigger/execute the groovy script without having to rely on being logged into the Jira GUI.

Does anyone know of a curl/wget/anything-based solution that would allow me to populate the "Script File Path" field on the groovyRunner Script Console page and then trigger the "Run" button? I've been scouring the interwebs for an example of someone doing this, but have come up shy.

Thanks.

2 answers

1 accepted

2 votes
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 25, 2014

Have you got chrome? If you press f12, run the script from the UI, and look at the network tab. Right-click on the request, there is an option "copy as curl".

Remove all the cookie crap and other stuff and you have what you want, although you will probably want to add "--user username:password" for http basic authn.

Rick Trudeau March 25, 2014

Thanks for the reply Jamie. Going to give this a try tonight and will update then.

Rick Trudeau March 26, 2014

Worked beautifully Jamie, thank you very much.

For the benefit of others, here's an example of a Windows batch file for triggering a Script Console script remotely:

@echo off

rem Script to run a Jira script on the ScriptRunner Console remotely.
set USERNAME=admin
set PASSWORD=secret
set DASHBOARD_URL=http://localhost:8080/secure/Dashboard.jspa
set SCRIPT_CONSOLE_URL=http://localhost:8080/secure/admin/groovy/GroovyRunner.jspa
set COOKIE_FILE=jiracoookie
set SCRIPT_TO_RUN=C:\helloWorld.groovy

"C:\Program Files\cURL\bin\curl.exe" -u %USERNAME%:%PASSWORD% --cookie-jar %COOKIE_FILE% --output output1.txt %DASHBOARD_URL%
"C:\Program Files\cURL\bin\curl.exe" --cookie %COOKIE_FILE% --header "X-Atlassian-Token: no-check" --data "filename=%SCRIPT_TO_RUN%" --data "Run+now=Run+now" --output output2.txt %SCRIPT_CONSOLE_URL%
erase %COOKIE_FILE%

Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2014

In case anyone wants to see this in bash, I have some sample code for you: https://answers.atlassian.com/questions/284007/how-to-call-canned-builtin-script-runner-scripts-from-a-script

0 votes
Tansu Akdeniz February 23, 2015

It helped a lot Jamie smile 

I changed the cURL a little bit and requested the script from directory. scriptFile=/var/..........clear.groovy"

I would like to run the code but each time I have different variables (dynamic data). Such as posting with different issuekeys. Is it possible ? 

Rick Trudeau February 23, 2015

Tansu, I have found that the best way to accomplish what you're trying to do is to create a custom Built-In script that sits along side the ones that are bundled/provided by Jamie. These scripts can have input parameters, and can be tested locally from the "Built-in Scripts" console. When you're satisfied everything works, you can then trigger a curl request to populate the script form, sending as input your dynamic parameters. This amounts to the same concept described above, only you use the Build-in Scripts interface rather than the Script Console interface. Hope that helps.

Suggest an answer

Log in or Sign up to answer