SimpleBuildWrapper EnvVar Updates

37 views
Skip to first unread message

Trevor Howard

unread,
Aug 8, 2018, 9:26:10 PM8/8/18
to Jenkins Developers
Hello, utilizing SimpleBuildWrapper is it possible to update EnvVars post setup but before disposal? Use case is to update tokens provided from SAML when session is about to expire. Contents of the block would be tools like AWS CLI that can pull keys/tokens from EnvVar.

I have something that quasi works by using a custom version of SimpleBuildWrapper to allow updates to an existing env key:
    public static final class Context {
        private Disposer disposer;
        private final Map<String,String> env = new HashMap<String,String>();
        public @Nonnull Map<String,String> getEnv() {
            return env;
        }

        public void env(String key, String value) {
            //if (env.containsKey(key)) {
            //    throw new IllegalStateException("just one binding for " + key);
            //}
            env.put(key, value);
        }
    ...

Then from inside the implementation's setup function:
            Runnable runnable = new Runnable() {
                public void run() {
                    updateENV(context);
                }
            };

            thread = new Thread(runnable);
            thread.start();
            context.setDisposer(new DisposeEnvironment(thread));

Teardown:
    private static class DisposeEnvironment extends Disposer {
        private static final long serialVersionUID = 1;
        private final Thread thread;

        DisposeEnvironment(Thread thread) { this.thread = thread; }

        @Override
        public void tearDown(
                Run<?, ?> build,
                FilePath workspace,
                Launcher launcher,
                TaskListener listener
        ) throws IOException, InterruptedException {
            thread.interrupt();
        }
    }


This only works for build step initialization inside the wrapper's block. Once a build step is running it appears it only retains whatever state Context was in during initialization. The next build step in a sequence of steps will then get the new Context state and so on. Preferably I'd like to update Context of a build step while it's still running as well. Is there a method of doing this?

Jesse Glick

unread,
Aug 9, 2018, 12:47:06 PM8/9/18
to Jenkins Dev
On Wed, Aug 8, 2018 at 9:26 PM Trevor Howard <thatr...@gmail.com> wrote:
> update EnvVars post setup but before disposal?

No it is not possible with `SimpleBuildWrapper`. `Context.getEnv` is
called once when the body is entered.

A Pipeline step could define an `EnvironmentExpander` which is
consulted whenever a nested step calls
`StepContext.get(EnvVars.class)`. This would happen, for example, when
a `sh` step starts. Of course that is useless if your step happens to
be something like

sh '''
make world
# two hours later…
aws s3 cp a.out s3://mybucket/binary
'''

since by the time the `aws` binary is run, the token has expired, even
though it was valid when `make` started.

> private static class DisposeEnvironment extends Disposer {
> private static final long serialVersionUID = 1;
> private final Thread thread;

This attempt is in any event impossible as a `Thread` is not `Serializable`.
Reply all
Reply to author
Forward
0 new messages