How to expand env vars inside of a form validation method?

42 views
Skip to first unread message

mso...@soasta.com

unread,
Mar 22, 2013, 4:43:02 PM3/22/13
to jenkin...@googlegroups.com
Hi everyone.  I have a build step descriptor with the following code:

        /**
         * Called automatically by Jenkins whenever the "projectFile"
         * field is modified by the user.
         * @param value the new path.
         */
        public FormValidation doCheckProjectFile(@AncestorInPath AbstractProject project, @QueryParameter String value) throws IOException {
            if (value == null || value.trim().isEmpty()) {
                return FormValidation.error("Project directory is required.");
            } else {
                // Make sure the directory exists.
                return FilePath.validateFileMask(project.getSomeWorkspace(), value);
            }
        }

This works as expected, until the user tries to input a path containing ${WORKSPACE}, at which point the validation promptly fails.  I know I can expand environment variables during the actual build, using the build's EnvVars object, but I couldn't find any way to get an EnvVars instance from the validation method.

Is there a way to do this?  Any help would be appreciated.

-- Matt

domi

unread,
Mar 23, 2013, 12:57:22 AM3/23/13
to jenkin...@googlegroups.com
Hi,
think about it, how would you expect this to work?
The env variables depend on (not only):
- the system of execution
- the user the process runs with
- parameters
- scripts
Most of these information is not available until the process really started, so therefore no there is no reliable way to do this during form validation.
/Domi

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matt Solnit

unread,
Mar 23, 2013, 3:12:48 PM3/23/13
to <jenkinsci-dev@googlegroups.com>
In some ways that makes sense, but then how does validation work for *any* path?  Jenkins knows enough to return a value for project.getSomeWorkspace(), which implies choosing a build machine.

If Jenkins can provide access to the workspace, then it should also be able to resolve any environment variables that are active for that connection:  PATH, HOME, TERM, etc.

-- Matt
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/PQ_LSyQ_7_c/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.

Vojtech Juranek

unread,
Mar 24, 2013, 9:49:05 PM3/24/13
to jenkin...@googlegroups.com, Matt Solnit
Hi,

On Saturday 23 March 2013 19:12:48 Matt Solnit wrote:
> In some ways that makes sense, but then how does validation work for *any*
> path? Jenkins knows enough to return a value for
> project.getSomeWorkspace(), which implies choosing a build machine.

if you want to follow this approach (choose some slave, where the build
already run and validate against it), something like this should work:

EnvVars ev = new
EnvVars(project.getSomeBuildWithWorkspace().getEnvironment(null));
String fullPath = ev.expand(value);

However, from the code you've provided, it seems to me that the path should be
relative to the workspace, so if there's ${WORKSPACE} in the path, I would
consider it as an error

HTH
Vojta
Reply all
Reply to author
Forward
0 new messages