BuildCommand.java 11.2 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 *
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc.
K
kohsuke 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
package hudson.cli;

26
import hudson.Util;
27
import hudson.console.ModelHyperlinkNote;
28
import hudson.model.AbstractProject;
29
import hudson.model.Cause.UserIdCause;
30 31 32
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.model.Run;
33 34 35 36
import hudson.model.ParametersAction;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.ParameterDefinition;
37
import hudson.Extension;
38
import hudson.AbortException;
39
import hudson.model.Queue;
40
import hudson.model.Item;
41
import hudson.model.TaskListener;
42
import hudson.model.User;
43
import hudson.model.queue.QueueTaskFuture;
44
import hudson.util.EditDistance;
45
import hudson.util.StreamTaskListener;
46

47
import jenkins.scm.SCMDecisionHandler;
48
import org.kohsuke.args4j.Argument;
49
import org.kohsuke.args4j.CmdLineException;
50 51
import org.kohsuke.args4j.Option;

52 53 54 55 56
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map.Entry;
E
Esa Laine 已提交
57
import java.io.FileNotFoundException;
58 59
import java.io.PrintStream;

60
import jenkins.model.Jenkins;
61 62
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem;
63

64 65 66 67 68 69 70 71 72
/**
 * Builds a job, and optionally waits until its completion.
 *
 * @author Kohsuke Kawaguchi
 */
@Extension
public class BuildCommand extends CLICommand {
    @Override
    public String getShortDescription() {
73
        return Messages.BuildCommand_ShortDescription();
74 75 76
    }

    @Argument(metaVar="JOB",usage="Name of the job to build",required=true)
77
    public Job<?,?> job;
78

79 80 81 82
    @Option(name="-f", usage="Follow the build progress. Like -s only interrupts are not passed through to the build.")
    public boolean follow = false;

    @Option(name="-s",usage="Wait until the completion/abortion of the command. Interrupts are passed through to the build.")
83 84
    public boolean sync = false;

85 86 87
    @Option(name="-w",usage="Wait until the start of the command")
    public boolean wait = false;

88 89 90
    @Option(name="-c",usage="Check for SCM changes before starting the build, and if there's no change, exit without doing a build")
    public boolean checkSCM = false;

91 92 93
    @Option(name="-p",usage="Specify the build parameters in the key=value format.")
    public Map<String,String> parameters = new HashMap<String, String>();

94 95 96
    @Option(name="-v",usage="Prints out the console output of the build. Use with -s")
    public boolean consoleOutput = false;

97 98
    @Option(name="-r") @Deprecated
    public int retryCnt = 10;
E
Esa Laine 已提交
99

100
    protected static final String BUILD_SCHEDULING_REFUSED = "Build scheduling Refused by an extension, hence not in Queue.";
101

102
    protected int run() throws Exception {
103
        job.checkPermission(Item.BUILD);
104

105
        ParametersAction a = null;
106 107 108
        if (!parameters.isEmpty()) {
            ParametersDefinitionProperty pdp = job.getProperty(ParametersDefinitionProperty.class);
            if (pdp==null)
109
                throw new IllegalStateException(job.getFullDisplayName()+" is not parameterized but the -p option was specified.");
110

111
            //TODO: switch to type annotations after the migration to Java 1.8
112
            List<ParameterValue> values = new ArrayList<ParameterValue>();
113 114 115 116

            for (Entry<String, String> e : parameters.entrySet()) {
                String name = e.getKey();
                ParameterDefinition pd = pdp.getParameterDefinition(name);
117
                if (pd==null) {
118 119 120 121
                    String nearest = EditDistance.findNearest(name, pdp.getParameterDefinitionNames());
                    throw new CmdLineException(null, nearest == null ?
                            String.format("'%s' is not a valid parameter.", name) :
                            String.format("'%s' is not a valid parameter. Did you mean %s?", name, nearest));
122 123 124
                }
                ParameterValue val = pd.createValue(this, Util.fixNull(e.getValue()));
                if (val == null) {
125
                    throw new CmdLineException(null, String.format("Cannot resolve the value for the parameter '%s'.",name));
126 127
                }
                values.add(val);
128
            }
129 130 131 132 133 134 135

            // handle missing parameters by adding as default values ISSUE JENKINS-7162
            for(ParameterDefinition pd :  pdp.getParameterDefinitions()) {
                if (parameters.containsKey(pd.getName()))
                    continue;

                // not passed in use default
136 137
                ParameterValue defaultValue = pd.getDefaultParameterValue();
                if (defaultValue == null) {
138
                    throw new CmdLineException(null, String.format("No default value for the parameter '%s'.",pd.getName()));
139 140
                }
                values.add(defaultValue);
141 142
            }

143 144 145
            a = new ParametersAction(values);
        }

146
        if (checkSCM) {
147 148 149
            SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job);
            if (item == null)
                throw new AbortException(job.getFullDisplayName()+" has no SCM trigger, but checkSCM was specified");
150
            // pre-emtively check for a polling veto
151
            if (SCMDecisionHandler.firstShouldPollVeto(job) != null) {
152
                return 0;
153
            }
154
            if (!item.poll(new StreamTaskListener(stdout, getClientCharset())).hasChanges())
155 156 157
                return 0;
        }

158
        if (!job.isBuildable()) {
159
            String msg = Messages.BuildCommand_CLICause_CannotBuildUnknownReasons(job.getFullDisplayName());
160
            if (job instanceof AbstractProject<?, ?> && ((AbstractProject<?, ?>)job).isDisabled()) {
161
                msg = Messages.BuildCommand_CLICause_CannotBuildDisabled(job.getFullDisplayName());
162
            } else if (job.isHoldOffBuildUntilSave()){
163
                msg = Messages.BuildCommand_CLICause_CannotBuildConfigNotSaved(job.getFullDisplayName());
164
            }
165
            throw new IllegalStateException(msg);
166
        }
167

168 169
        Queue.Item item = ParameterizedJobMixIn.scheduleBuild2(job, 0, new CauseAction(new CLICause(Jenkins.getAuthentication().getName())), a);
        QueueTaskFuture<? extends Run<?,?>> f = item != null ? (QueueTaskFuture)item.getFuture() : null;
170
        
171
        if (wait || sync || follow) {
172
            if (f == null) {
173
                throw new IllegalStateException(BUILD_SCHEDULING_REFUSED);
174
            }
175
            Run<?,?> b = f.waitForStart();    // wait for the start
176
            stdout.println("Started "+b.getFullDisplayName());
177
            stdout.flush();
178

179
            if (sync || follow) {
180 181 182 183 184 185 186 187 188 189 190 191 192 193
                try {
                    if (consoleOutput) {
                        // read output in a retry loop, by default try only once
                        // writeWholeLogTo may fail with FileNotFound
                        // exception on a slow/busy machine, if it takes
                        // longish to create the log file
                        int retryInterval = 100;
                        for (int i=0;i<=retryCnt;) {
                            try {
                                b.writeWholeLogTo(stdout);
                                break;
                            }
                            catch (FileNotFoundException e) {
                                if ( i == retryCnt ) {
194 195 196
                                    Exception myException = new AbortException();
                                    myException.initCause(e);
                                    throw myException;
197 198 199
                                }
                                i++;
                                Thread.sleep(retryInterval);
E
Esa Laine 已提交
200 201 202
                            }
                        }
                    }
203 204 205 206
                    f.get();    // wait for the completion
                    stdout.println("Completed "+b.getFullDisplayName()+" : "+b.getResult());
                    return b.getResult().ordinal;
                } catch (InterruptedException e) {
207 208 209 210 211
                    if (follow) {
                        return 125;
                    } else {
                        // if the CLI is aborted, try to abort the build as well
                        f.cancel(true);
212 213 214
                        Exception myException = new AbortException();
                        myException.initCause(e);
                        throw myException;
215
                    }
216 217
                }
            }
218
        }
219

220
        return 0;
221 222 223 224 225 226 227 228 229
    }

    @Override
    protected void printUsageSummary(PrintStream stderr) {
        stderr.println(
            "Starts a build, and optionally waits for a completion.\n" +
            "Aside from general scripting use, this command can be\n" +
            "used to invoke another job from within a build of one job.\n" +
            "With the -s option, this command changes the exit code based on\n" +
230 231 232 233 234
            "the outcome of the build (exit code 0 indicates a success)\n" +
            "and interrupting the command will interrupt the job.\n" +
            "With the -f option, this command changes the exit code based on\n" +
            "the outcome of the build (exit code 0 indicates a success)\n" +
            "however, unlike -s, interrupting the command will not interrupt\n" +
235
            "the job (exit code 125 indicates the command was interrupted).\n" +
236
            "With the -c option, a build will only run if there has been\n" +
237
            "an SCM change."
238 239 240
        );
    }

241
    public static class CLICause extends UserIdCause {
242

243
    	private String startedBy;
244

245
    	public CLICause(){
246
    		startedBy = "unknown";
247
    	}
248

249 250 251
    	public CLICause(String startedBy){
    		this.startedBy = startedBy;
    	}
252

253
        @Override
254
        public String getShortDescription() {
255 256
            User user = User.get(startedBy, false);
            String userName = user != null ? user.getDisplayName() : startedBy;
257
            return Messages.BuildCommand_CLICause_ShortDescription(userName);
258 259 260 261 262 263
        }

        @Override
        public void print(TaskListener listener) {
            listener.getLogger().println(Messages.BuildCommand_CLICause_ShortDescription(
                    ModelHyperlinkNote.encodeTo("/user/" + startedBy, startedBy)));
264
        }
265 266 267 268 269 270 271 272 273 274

        @Override
        public boolean equals(Object o) {
            return o instanceof CLICause;
        }

        @Override
        public int hashCode() {
            return 7;
        }
275 276 277
    }
}