Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This is the simplest way to generate your step script, and we strongly recommend the use of this snippet due to the complexity of some task related parameters. 

Jira instances configuration via Groovy script (Jenkins Script Console)

If you use a containerized version of Jenkins, or simply want to avoid creating the Jira configurations manually (using the Jenkins UI), you can use the following script in the Jenkins Script Console.

To use the script below, you just need to modify the contents of the instances and deleteOldInstances variables.

Code Block
languagegroovy
firstline1
titleCreate new Jira instances in Xray global configuration
import jenkins.model.Jenkins
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import com.xpandit.plugins.xrayjenkins.model.HostingType
import com.xpandit.plugins.xrayjenkins.model.XrayInstance
import com.xpandit.plugins.xrayjenkins.model.ServerConfiguration

// true, if you want the old Jira instances removed, false otherwise.
boolean deleteOldInstances = false

/* Represents the Jira instances to be added to the Global Jenkins configuration.
 * - name: the name of the Jira instance to be displayed to the users.
 * - hostingType: must be one of two values. 'SERVER' for Server or Data Center instances OR 'CLOUD' for cloud instances.
 * - url: [ONLY FOR SERVER INSTANCES] the base URL/IP of the Jira server address.
 * - credentialId: [OPTIONAL] the credential ID from the 'Credentials' plugin that will be used to authenticate the jira REST API requests.
 */
JSONArray instances = [
        [
                name: 'my Jira server',
                hostingType: 'SERVER',
                url: 'http://example.com',
                credentialId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Credential ID from the 'Credentials' plugin.
        ],
        [
                name: 'my Jira cloud',
                hostingType: 'CLOUD',
                credentialId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Credential ID from the 'Credentials' plugin.
        ]
] as JSONArray

// ~~~ Saves the new Jira instances into the Jenkins global configuration ~~~
ServerConfiguration config = ServerConfiguration.get()
List<XrayInstance> xrayInstances = new ArrayList<XrayInstance>()

instances.each {instance ->
    String name = instance.optString('name', '')
    String hostingTypeString = instance.optString('hostingType', '')
    String url = instance.optString('url', '')
    String credentialId = instance.optString('credentialId', null)

    HostingType hostingType = hostingTypeString == 'CLOUD' ? HostingType.CLOUD : HostingType.SERVER

    xrayInstances.add(new XrayInstance(null, name, hostingType, url, credentialId))
}

List<XrayInstance> oldXrayInstances = config.getServerInstances()
if (!deleteOldInstances && oldXrayInstances != null) {
    xrayInstances.addAll(oldXrayInstances)
}

config.setServerInstances(xrayInstances)
config.save()

println('Xray Jira Instances created :)')


Troubleshooting

The build process is failing with status code 403

...

If you are a Jira administrator, you can go to Jira administration > User Management and reset the failed login.

The Jira xxx configuration of this

...

task was not found

If you obtain this error, probably you have migrated from an old version of this plugin. You need to open each project/job configuration and save it.

...