[Jenkins plug-in development 2] front and rear data transmission Foundation

How to save a configuration

The last HelloWorld generated project contains a Build extension by default. The configuration page of Jenkins task can be seen as follows.

Let's copy the url and change the configuration to ` ` ` ` config.xml.
We enter the name "Victor Liu" as prompted, save it, and then open the xml address we just copied and modified. As shown below:

These are basic. As mentioned in previous articles, I won't say more. Let me talk about the principle:

Such an extension includes a jelly page and a background class. The variables of the background class correspond to a page input value, as follows:

    <f:entry field="name">
        <f:textbox />
    </f:entry>
    @DataBoundSetter
    public void setName(String name) {
        this.name = name;
    }

The above tag will be converted into an input form with name="_name" and passed to the background. The member variable in the background class will receive this value, and then use the DataBoundSetter annotation on the setter method to save it to xml.
tips: the constructor can be empty or all setter s can be used.

How to display background data

The background classes generated by default are roughly as follows. The internal class DescriptorImpl is responsible for data transmission. We add a get method getClusterList:

public class HelloWorldBuilder extends Builder implements SimpleBuildStep {
    String name;
...slightly...
    @Symbol("greet")
    @Extension
    public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
        public String[] getClusterList() {
            return new String[]{"Qingdao","Beijing"};
        }
    }
}

For example, we display a drop-down box with the following values on the page:

    <f:entry title="Choose a name">
        <select name="_.name">
            <j:forEach var="item" items="${descriptor.clusterList}">
                <f:option>${item}</f:option>
            </j:forEach>
        </select>
    </f:entry>

Of course, select can also be replaced with its own tag f:select, but Jerry's document is not easy to check, so I'm useless. In fact, I think it's more like jsp and freemaker. If you can't find them, you can find how they are written. It's basically the same.

How to execute js on a page

This is how html is written, as follows:

<script>console.log("hi")</script>

After installing the plug-in of jQuery3, you can also introduce:

<st:adjunct includes="io.jenkins.plugins.jquery3"/>

There is a pitfall that dom and jq are introduced dynamically when you click "Add build step" for the first time. You can't find the elements on your Jerry page.
Later, I found a way to monitor dom changes:

    <script>
        var targetNode = document.getElementById('page-body');
        var config = { attributes: true, childList: true, subtree: true };
        var callback = function(mutationsList) {
            mutationsList.forEach(function(item,index){
                if (item.type == 'childList') {
                    console.log(document.getElementById("aa"));
                }
            });
        };
        var observer = new MutationObserver(callback);
        observer.observe(targetNode, config);
    </script>

In this way, you can get the elements on your own page in the callback, but it is more troublesome.

How to prompt errors and interrupt build

We add the processing we need to do in the method of the background class, as follows:

@Override
    public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
    ...
    }

The above is generated by default. The method has been abandoned. Now use:

@Override
    public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws InterruptedException, IOException {
    ...
    }

In fact, the parameters are different. Note that Jenkins environment variables can be used in env, such as:

env.get("WORKSPACE")

How to interrupt:

listener.error(msg);
run.getExecutor().doStop();
return;

Tags: Operation & Maintenance jenkins

Posted on Tue, 26 Oct 2021 14:05:54 -0400 by citytours