In this page

Running a Python program when a commit is accepted

This automation runs a Python program, optionally parsing parameters from the VCS commit message and passing those to the program.

The Python process is started by a little Groovy script.

Configuration

  1. Login to Jira as admin, go to AdministrationSystemAutomation rules.
  2. Click Create rule.
  3. Select the trigger Genius Commit created (from the DevOps category).
  4. Choose the command Run code, and click Save.
  5. Click New action.
  6. Select the action Run Groovy script.
    1. Enter a one-line description in the Summary field.
    2. Enter the script code in the Script field.
      1. If you use are using a command configured with a parameter pattern and the parameter arg was successfully parsed, you can access its value using this expression:
        ## use the parameter's name in the suffix (last item)
        def x = devops.arg
        
      2. To access smart values, use this syntax. (The {{...}} syntax doesn't work in Groovy scripts.)
  7. Click Save.
  8. Name your automation rule intuitively, and click Turn it on.

Usage

  1. Enter this inline Groovy script in your automation action:
    def cmd = "python /my/directory/test.py ${devops.arg}"
    // ...or on Windows: "python c:\\my\\directory\\test.py ${devops.arg}"
    
    def out = new StringBuilder(), err = new StringBuilder()
    def proc = cmd.execute()
    proc.consumeProcessOutput(out, err)
    proc.waitForOrKill(1000)
    
    if (out) {
    	auditLog.info(out)
    }
    if (err) {
    	auditLog.error(err)
    }
    
    It starts the Python program process passing the parameter value as argument, waits for its completion, then writes the process' standard output and standard error to the automation action's audit log.
  2. Create test.py with the following content:
    import sys
    
    print('Python program started with the argument: %s' % (str(sys.argv[1])))
    
    (It just writes a line to the standard output and demonstrates how to access the parameter value received as argument.)
  3. Create a commit with this commit message:
    Fix the FOO-1 bug. @run foobar
  4. The Python program will be run with the parameter passed to it.
  5. The following message will appear in the automation action's audit log:
    Python program started with the argument: foobar

Troubleshooting

If you don't get the expected results:

  1. Check if the @run command is defined in the Genius Commands screen? (It's a default command.)
  2. Check if the @run command followed by the command parameter was included in the commit message?
  3. See the general troubleshooting steps.

Questions?

Ask us any time.