How to programmatically add a job to a view in hud

2019-06-26 06:52发布

问题:

I have a Java program controlling automatically generated jobs for a set of hudson servers. It is no problem to create, delete or update job(config)s using the hudson remote API. I also managed it to create hudson views and to create a new job for a hudson view. But I still need to know how to add an already existing job to a view and how to remove it again.

Are there URLs with arguments doing the job (like for the job creation)?

回答1:

You can execute a groovy script through the Hudson CLI API that will add a job to a view. Here is the code:

import hudson.model.*

def job = Hudson.instance.getView("View").getItem("Job")
Hudson.instance.getView("View2").add(job)

and the command for the CLI is:

java -jar hudson-cli.jar -s http://`your-hudson-server` groovy myScript.groovy

Note that you must have Groovy support plugin installed on your Hudson instance in order to execute the script. You can install it on: http://your-hudson-server/pluginManager .



回答2:

There is no api for view configuration (at least not in Jenkins v1.424.6), but it should be possible to add a job to view foo by using the form at http://[jenkins-host]/view/foo/configure (submitting to http://[jenkins]/view/foo/configSubmit).

If you are using Java, HTMLUnit or HttpClient of Apache HttpComponents can help you with this.