Search in sources :

Example 16 with EnvVars

use of hudson.EnvVars in project evosuite by EvoSuite.

the class Git method commit.

@Override
public int commit(AbstractMavenProject<?, ?> project, AbstractBuild<?, ?> build, BuildListener listener, String branchName, String ctgBestsDir) {
    try {
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Commiting new test cases");
        Set<String> branches = this.getBranches();
        if (!branches.contains(branchName)) {
            // create a new branch called "evosuite-tests" to commit and
            // push the new generated test suites
            listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "There is no branch called " + branchName);
            this.gitClient.branch(branchName);
        }
        this.gitClient.setAuthor("jenkins", "jenkins@localhost.com");
        this.gitClient.setCommitter("jenkins", "jenkins@localhost.com");
        this.gitClient.checkoutBranch(branchName, "HEAD");
        EnvVars env = build.getEnvironment(listener);
        env.overrideAll(build.getBuildVariables());
        int number_of_files_committed = 0;
        try {
            MavenModuleSet prj = (MavenModuleSet) project;
            // parse list of new and modified files per module
            StringBuilder filesToBeCommitted = new StringBuilder();
            for (MavenModule module : prj.getModules()) {
                String status = ((CliGitAPIImpl) this.gitClient).launchCommand("ls-files", "--deleted", "--modified", "--others", (module.getRelativePath().isEmpty() ? "" : module.getRelativePath() + File.separator) + ctgBestsDir);
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Status (" + status.length() + "):\n" + status);
                filesToBeCommitted.append(status);
            }
            String s_filesToBeCommitted = filesToBeCommitted.toString();
            if (s_filesToBeCommitted.isEmpty()) {
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Nothing to commit");
                return 0;
            }
            for (String toCommit : s_filesToBeCommitted.split("\\R")) {
                String filePath = build.getWorkspace().getRemote() + File.separator + toCommit;
                if (new File(filePath).exists()) {
                    listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "adding: " + filePath);
                    this.gitClient.add(filePath);
                    number_of_files_committed++;
                } else {
                    listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "File '" + filePath + "' reported by git status command does not exist");
                }
            }
        } catch (ClassCastException e) {
            // FIXME when building a project remotely, we just have access to a GitClient of type
            // RemoteGitImpl, which cannot be cast to CliGitAPIImpl. and therefore, we cannot use
            // launchCommand method. as a workaround, we can simple add all files under .evosuite/best-tests
            // and hopefully git will take care of the rest. GitClient already supports the creation
            // of a new branch, checkout some branch, add files to be committed, commmit, push, etc.
            // there must be a way of getting the list of modified / new / deleted files just using
            // GitClient, however we still do not know how to get that.
            listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + e.getMessage() + "\nTrying a different approach!");
            FilePath[] filesToCommit = build.getWorkspace().list(build.getEnvironment(listener).expand("**" + File.separator + ctgBestsDir + File.separator + "**" + File.separator + "*"));
            if (filesToCommit.length == 0) {
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Nothing to commit");
                return number_of_files_committed;
            }
            number_of_files_committed = filesToCommit.length;
            for (FilePath fileToCommit : filesToCommit) {
                listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "adding: " + fileToCommit.getRemote());
                this.gitClient.add(fileToCommit.getRemote());
            }
        }
        // commit
        String commit_msg = SCM.COMMIT_MSG_PREFIX + build.getProject().getName().replace(" ", "_") + "-" + build.getNumber();
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + commit_msg);
        this.gitClient.commit(commit_msg);
        return number_of_files_committed;
    } catch (InterruptedException | IOException e) {
        listener.getLogger().println(EvoSuiteRecorder.LOG_PREFIX + "Commit failed " + e.getMessage());
        e.printStackTrace();
        this.rollback(build, listener);
        return -1;
    }
}
Also used : FilePath(hudson.FilePath) MavenModule(hudson.maven.MavenModule) CliGitAPIImpl(org.jenkinsci.plugins.gitclient.CliGitAPIImpl) IOException(java.io.IOException) EnvVars(hudson.EnvVars) File(java.io.File) MavenModuleSet(hudson.maven.MavenModuleSet)

Example 17 with EnvVars

use of hudson.EnvVars in project phabricator-jenkins-plugin by uber.

the class PhabricatorNotifier method perform.

@Override
public final boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher, final BuildListener listener) throws InterruptedException, IOException {
    EnvVars environment = build.getEnvironment(listener);
    Logger logger = new Logger(listener.getLogger());
    final String branch = environment.get("GIT_BRANCH");
    final String gitUrl = environment.get("GIT_URL");
    final UberallsClient uberallsClient = getUberallsClient(logger, gitUrl, branch);
    final boolean needsDecoration = build.getActions(PhabricatorPostbuildAction.class).size() == 0;
    final String diffID = environment.get(PhabricatorPlugin.DIFFERENTIAL_ID_FIELD);
    final String phid = environment.get(PhabricatorPlugin.PHID_FIELD);
    final boolean isDifferential = !CommonUtils.isBlank(diffID);
    InterruptedBuildAction action = build.getAction(InterruptedBuildAction.class);
    if (action != null) {
        List<CauseOfInterruption> causes = action.getCauses();
        for (CauseOfInterruption cause : causes) {
            if (cause instanceof PhabricatorCauseOfInterruption) {
                logger.warn(ABORT_TAG, "Skipping notification step since this build was interrupted" + " by a newer build with the same differential revision");
                return true;
            }
        }
    }
    CoverageProvider coverageProvider;
    // So only skip build result processing if both are blank (e.g. master runs to update coverage data)
    if (CommonUtils.isBlank(phid) && !isDifferential) {
        if (needsDecoration) {
            build.addAction(PhabricatorPostbuildAction.createShortText(branch, null));
        }
        coverageProvider = getCoverageProvider(build, listener, Collections.<String>emptySet());
        CodeCoverageMetrics coverageResult = null;
        if (coverageProvider != null) {
            coverageResult = coverageProvider.getMetrics();
        }
        NonDifferentialBuildTask nonDifferentialBuildTask = new NonDifferentialBuildTask(logger, uberallsClient, coverageResult, uberallsEnabled, environment.get("GIT_COMMIT"));
        // Ignore the result.
        nonDifferentialBuildTask.run();
        return true;
    }
    ConduitAPIClient conduitClient;
    try {
        conduitClient = getConduitClient(build.getParent());
    } catch (ConduitAPIException e) {
        e.printStackTrace(logger.getStream());
        logger.warn(CONDUIT_TAG, e.getMessage());
        return false;
    }
    final String buildUrl = environment.get("BUILD_URL");
    if (!isDifferential) {
        // Process harbormaster for non-differential builds
        Task.Result result = new NonDifferentialHarbormasterTask(logger, phid, conduitClient, build.getResult(), buildUrl).run();
        return result == Task.Result.SUCCESS;
    }
    DifferentialClient diffClient = new DifferentialClient(diffID, conduitClient);
    Differential diff;
    try {
        diff = new Differential(diffClient.fetchDiff());
    } catch (ConduitAPIException e) {
        e.printStackTrace(logger.getStream());
        logger.warn(CONDUIT_TAG, "Unable to fetch differential from Conduit API");
        logger.warn(CONDUIT_TAG, e.getMessage());
        return true;
    }
    if (needsDecoration) {
        diff.decorate(build, this.getPhabricatorURL(build.getParent()));
    }
    Set<String> includeFileNames = new HashSet<String>();
    for (String file : diff.getChangedFiles()) {
        includeFileNames.add(FilenameUtils.getName(file));
    }
    coverageProvider = getCoverageProvider(build, listener, includeFileNames);
    CodeCoverageMetrics coverageResult = null;
    if (coverageProvider != null) {
        coverageResult = coverageProvider.getMetrics();
    }
    BuildResultProcessor resultProcessor = new BuildResultProcessor(logger, build, diff, diffClient, environment.get(PhabricatorPlugin.PHID_FIELD), coverageResult, buildUrl, preserveFormatting, coverageCheckSettings);
    if (uberallsEnabled) {
        boolean passBuildOnUberalls = resultProcessor.processParentCoverage(uberallsClient);
        if (!passBuildOnUberalls) {
            build.setResult(Result.FAILURE);
        }
    }
    // Add in comments about the build result
    resultProcessor.processBuildResult(commentOnSuccess, commentWithConsoleLinkOnFailure);
    // Process unit tests results to send to Harbormaster
    resultProcessor.processUnitResults(getUnitProvider(build, listener));
    // Read coverage data to send to Harbormaster
    resultProcessor.processCoverage(coverageProvider);
    if (processLint) {
        // Read lint results to send to Harbormaster
        resultProcessor.processLintResults(lintFile, lintFileSize);
    }
    // Fail the build if we can't report to Harbormaster
    if (!resultProcessor.processHarbormaster()) {
        return false;
    }
    resultProcessor.processRemoteComment(commentFile, commentSize);
    resultProcessor.sendComment(commentWithConsoleLinkOnFailure);
    return true;
}
Also used : CodeCoverageMetrics(com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics) NonDifferentialHarbormasterTask(com.uber.jenkins.phabricator.tasks.NonDifferentialHarbormasterTask) Task(com.uber.jenkins.phabricator.tasks.Task) NonDifferentialBuildTask(com.uber.jenkins.phabricator.tasks.NonDifferentialBuildTask) NonDifferentialHarbormasterTask(com.uber.jenkins.phabricator.tasks.NonDifferentialHarbormasterTask) CauseOfInterruption(jenkins.model.CauseOfInterruption) Differential(com.uber.jenkins.phabricator.conduit.Differential) UberallsClient(com.uber.jenkins.phabricator.uberalls.UberallsClient) ConduitAPIException(com.uber.jenkins.phabricator.conduit.ConduitAPIException) DifferentialClient(com.uber.jenkins.phabricator.conduit.DifferentialClient) ConduitAPIClient(com.uber.jenkins.phabricator.conduit.ConduitAPIClient) Logger(com.uber.jenkins.phabricator.utils.Logger) EnvVars(hudson.EnvVars) CoverageProvider(com.uber.jenkins.phabricator.coverage.CoverageProvider) NonDifferentialBuildTask(com.uber.jenkins.phabricator.tasks.NonDifferentialBuildTask) InterruptedBuildAction(jenkins.model.InterruptedBuildAction) HashSet(java.util.HashSet)

Example 18 with EnvVars

use of hudson.EnvVars in project phabricator-jenkins-plugin by uber.

the class PhabricatorBuildWrapper method setUp.

/**
 * {@inheritDoc}
 */
@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
    EnvVars environment = build.getEnvironment(listener);
    Logger logger = new Logger(listener.getLogger());
    if (environment == null) {
        return this.ignoreBuild(logger, "No environment variables found?!");
    }
    final Map<String, String> envAdditions = new HashMap<String, String>();
    String phid = environment.get(PhabricatorPlugin.PHID_FIELD);
    String diffID = environment.get(PhabricatorPlugin.DIFFERENTIAL_ID_FIELD);
    if (CommonUtils.isBlank(diffID)) {
        this.addShortText(build);
        this.ignoreBuild(logger, "No differential ID found.");
        return new Environment() {
        };
    }
    FilePath arcWorkPath;
    if (this.workDir != null && this.workDir.length() > 0) {
        arcWorkPath = build.getWorkspace().child(workDir);
    } else {
        arcWorkPath = build.getWorkspace();
    }
    LauncherFactory starter = new LauncherFactory(launcher, environment, listener.getLogger(), arcWorkPath);
    ConduitAPIClient conduitClient;
    try {
        conduitClient = getConduitClient(build.getParent(), logger);
    } catch (ConduitAPIException e) {
        e.printStackTrace(logger.getStream());
        logger.warn(CONDUIT_TAG, e.getMessage());
        return null;
    }
    DifferentialClient diffClient = new DifferentialClient(diffID, conduitClient);
    if (!CommonUtils.isBlank(phid)) {
        logger.info("harbormaster", "Sending Harbormaster BUILD_URL via PHID: " + phid);
        String buildUrl = environment.get("BUILD_URL");
        Task.Result sendUriResult = new SendHarbormasterUriTask(logger, diffClient, phid, buildUrl).run();
        if (sendUriResult != Task.Result.SUCCESS) {
            logger.info("harbormaster", "Unable to send BUILD_URL to Harbormaster");
        }
    }
    Differential diff;
    try {
        diff = new Differential(diffClient.fetchDiff());
        diff.setCommitMessage(diffClient.getCommitMessage(diff.getRevisionID(false)));
        diff.decorate(build, this.getPhabricatorURL(build.getParent()));
        logger.info(CONDUIT_TAG, "Fetching differential from Conduit API");
        envAdditions.put(DIFFERENTIAL_AUTHOR, diff.getAuthorEmail());
        envAdditions.put(DIFFERENTIAL_BASE_COMMIT, diff.getBaseCommit());
        envAdditions.put(DIFFERENTIAL_BRANCH, diff.getBranch());
        envAdditions.put(DIFFERENTIAL_SUMMARY, diff.getCommitMessage());
    } catch (ConduitAPIException e) {
        e.printStackTrace(logger.getStream());
        logger.warn(CONDUIT_TAG, "Unable to fetch differential from Conduit API");
        logger.warn(CONDUIT_TAG, e.getMessage());
        return null;
    }
    String baseCommit = "origin/master";
    if (!applyToMaster) {
        baseCommit = diff.getBaseCommit();
    }
    final String conduitToken = this.getConduitToken(build.getParent(), logger);
    Task.Result result = new ApplyPatchTask(logger, starter, baseCommit, diffID, conduitToken, getArcPath(), DEFAULT_GIT_PATH, createCommit, skipForcedClean, createBranch, patchWithForceFlag, scmType).run();
    if (result != Task.Result.SUCCESS) {
        logger.warn("arcanist", "Error applying arc patch; got non-zero exit code " + result);
        Task.Result failureResult = new SendHarbormasterResultTask(logger, diffClient, phid, false, null, null, null).run();
        if (failureResult != Task.Result.SUCCESS) {
            // such failure, very broke
            logger.warn("arcanist", "Unable to notify harbormaster of patch failure");
        }
        // Indicate failure
        return null;
    }
    return new Environment() {

        @Override
        public void buildEnvVars(Map<String, String> env) {
            EnvVars envVars = new EnvVars(env);
            envVars.putAll(envAdditions);
            env.putAll(envVars);
        }
    };
}
Also used : FilePath(hudson.FilePath) SendHarbormasterUriTask(com.uber.jenkins.phabricator.tasks.SendHarbormasterUriTask) ApplyPatchTask(com.uber.jenkins.phabricator.tasks.ApplyPatchTask) SendHarbormasterResultTask(com.uber.jenkins.phabricator.tasks.SendHarbormasterResultTask) Task(com.uber.jenkins.phabricator.tasks.Task) SendHarbormasterResultTask(com.uber.jenkins.phabricator.tasks.SendHarbormasterResultTask) Differential(com.uber.jenkins.phabricator.conduit.Differential) HashMap(java.util.HashMap) ConduitAPIException(com.uber.jenkins.phabricator.conduit.ConduitAPIException) DifferentialClient(com.uber.jenkins.phabricator.conduit.DifferentialClient) ConduitAPIClient(com.uber.jenkins.phabricator.conduit.ConduitAPIClient) Logger(com.uber.jenkins.phabricator.utils.Logger) SendHarbormasterUriTask(com.uber.jenkins.phabricator.tasks.SendHarbormasterUriTask) EnvVars(hudson.EnvVars) ApplyPatchTask(com.uber.jenkins.phabricator.tasks.ApplyPatchTask) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with EnvVars

use of hudson.EnvVars in project workflow-job-plugin by jenkinsci.

the class WorkflowRun method getEnvironment.

@Override
public EnvVars getEnvironment(TaskListener listener) throws IOException, InterruptedException {
    EnvVars env = super.getEnvironment(listener);
    Jenkins instance = Jenkins.getInstance();
    if (instance != null) {
        for (NodeProperty nodeProperty : instance.getGlobalNodeProperties()) {
            nodeProperty.buildEnvVars(env, listener);
        }
    }
    // TODO EnvironmentContributingAction does not support Job yet:
    ParametersAction a = getAction(ParametersAction.class);
    if (a != null) {
        for (ParameterValue v : a) {
            v.buildEnvironment(this, env);
        }
    }
    EnvVars.resolve(env);
    return env;
}
Also used : Jenkins(jenkins.model.Jenkins) EnvVars(hudson.EnvVars) ParameterValue(hudson.model.ParameterValue) NodeProperty(hudson.slaves.NodeProperty) ParametersAction(hudson.model.ParametersAction)

Example 20 with EnvVars

use of hudson.EnvVars in project promoted-builds-plugin by jenkinsci.

the class PromotionProcess method isVisible.

public boolean isVisible() {
    if (isVisible == null)
        return true;
    AbstractProject<?, ?> job = getOwner();
    if (job == null)
        return true;
    String expandedIsVisible = isVisible;
    EnvVars environment = getDefaultParameterValuesAsEnvVars(job);
    if (environment != null) {
        expandedIsVisible = environment.expand(expandedIsVisible);
    }
    if (expandedIsVisible == null) {
        return true;
    }
    return !expandedIsVisible.toLowerCase().equals("false");
}
Also used : EnvVars(hudson.EnvVars)

Aggregations

EnvVars (hudson.EnvVars)64 Test (org.junit.Test)23 IOException (java.io.IOException)15 FilePath (hudson.FilePath)11 ClientConfiguration (com.amazonaws.ClientConfiguration)7 ArgumentListBuilder (hudson.util.ArgumentListBuilder)7 TaskListener (hudson.model.TaskListener)6 AbortException (hudson.AbortException)5 HashMap (java.util.HashMap)5 MavenModuleSet (hudson.maven.MavenModuleSet)4 FreeStyleProject (hudson.model.FreeStyleProject)4 ParameterValue (hudson.model.ParameterValue)4 Launcher (hudson.Launcher)3 GitException (hudson.plugins.git.GitException)3 LogTaskListener (hudson.util.LogTaskListener)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NodeJSInstallation (jenkins.plugins.nodejs.tools.NodeJSInstallation)3 ConduitAPIClient (com.uber.jenkins.phabricator.conduit.ConduitAPIClient)2 ConduitAPIException (com.uber.jenkins.phabricator.conduit.ConduitAPIException)2