How to get SimpleBuildWrapper decorateLauncher behavior in Pipeline Wrapper

108 views
Skip to first unread message

Caleb Lloyd

unread,
May 23, 2017, 12:46:55 AM5/23/17
to Jenkins Developers
I am trying to implement a Pipeline Wrapper that runs commands in an LXD Container.

I see that SimpleBuildWrapper has a decorateLauncher method that seems like it would be perfect.  But the "HelloWorldWrapper decorateLauncher" message is never logged, and it seems that decorateLauncher is not getting called in my Pipeline stage.

Here is the pipeline code:

package org.boxboat.plugins.lxd;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.*;
import hudson.tasks.BuildWrapperDescriptor;
import jenkins.tasks.SimpleBuildWrapper;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.logging.Logger;

public class HelloWorldWrapper extends SimpleBuildWrapper {

    private final static Logger Log = Logger.getLogger(HelloWorldWrapper.class.getName());

    @DataBoundConstructor
    public HelloWorldWrapper() {
        super();
        Log.info("HelloWorldWrapper construct");
    }

    @Override
    public void setUp(Context context, Run<?, ?> run, FilePath filePath, Launcher launcher, TaskListener taskListener, EnvVars envVars) throws IOException, InterruptedException {
        Log.info("HelloWorldWrapper setUp");
    }

    @Override
    public Launcher decorateLauncher(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws IOException, InterruptedException, Run.RunnerAbortedException {
        Log.info("HelloWorldWrapper decorateLauncher");
        return new LxdDecoratedLauncher(launcher);
    }

    @Extension
    public static class DescriptorImpl extends BuildWrapperDescriptor {
        @Override public boolean isApplicable(AbstractProject<?,?> item) {
            return true;
        }
    }

}

Here is my Jenkinsfile:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                wrap([$class: 'org.boxboat.plugins.lxd.HelloWorldWrapper']) {
                    sh '''
                    ls /tmp
                    cat /tmp/jenkins'''
                }
            }
        }
        stage('Publish') {
            steps {
                wrap([$class: 'org.boxboat.plugins.lxd.HelloWorldWrapper']) {
                    sh '''
                    ls /tmp
                    cat /tmp/jenkins'''
                }
            }
        }
    } 
}

How do I achieve launcher decoration in pipeline mode?  It seems that this would be very useful for LXC, chroot, and the likes.

Thanks

Jesse Glick

unread,
May 23, 2017, 9:46:51 AM5/23/17
to Jenkins Dev
As noted in https://github.com/jenkinsci/jenkins/blob/d13b1361e1650494528a7b46f82fe25d7fb5e3c3/core/src/main/java/jenkins/tasks/SimpleBuildWrapper.java#L203-L207
this is not currently supported by `SimpleBuildWrapper`, as is noted
in the Javadoc for the method you are overriding.

You would need to create a custom `Step` with a body argument, and use
`BodyInvoker.mergeLauncherDecorators`, as for example the
`docker-workflow` plugin does to run nested `sh` steps inside a
container.
Reply all
Reply to author
Forward
0 new messages