Search in sources :

Example 61 with EnvVars

use of hudson.EnvVars in project sonar-scanner-jenkins by SonarSource.

the class MsBuildSQRunnerEnd method perform.

@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
    ArgumentListBuilder args = new ArgumentListBuilder();
    EnvVars env = BuilderUtils.getEnvAndBuildVars(run, listener);
    SonarQubeScannerMsBuildParams beginParams = run.getAction(SonarQubeScannerMsBuildParams.class);
    if (beginParams == null) {
        throw new AbortException(Messages.MsBuildScannerEnd_NoBeginStep());
    }
    String scannerName = beginParams.getScannerName();
    String sonarInstName = beginParams.getSqServerName();
    SonarInstallation sonarInstallation = getSonarInstallation(sonarInstName, listener);
    MsBuildSQRunnerInstallation msBuildScanner = Jenkins.getInstance().getDescriptorByType(MsBuildSQRunnerBegin.DescriptorImpl.class).getMsBuildScannerInstallation(scannerName);
    args.add(getExeName(msBuildScanner, env, launcher, listener, workspace));
    addArgs(args, env, sonarInstallation);
    int result = launcher.launch().cmds(args).envs(env).stdout(listener).pwd(BuilderUtils.getModuleRoot(run, workspace)).join();
    if (result != 0) {
        addBadge(run, listener, workspace, sonarInstallation);
        throw new AbortException(Messages.MSBuildScanner_ExecFailed(result));
    }
    addBadge(run, listener, workspace, sonarInstallation);
}
Also used : EnvVars(hudson.EnvVars) ArgumentListBuilder(hudson.util.ArgumentListBuilder) AbortException(hudson.AbortException)

Example 62 with EnvVars

use of hudson.EnvVars in project sonar-scanner-jenkins by SonarSource.

the class SonarMaven method executeMaven.

public static boolean executeMaven(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, String mavenName, String pom, SonarInstallation sonarInstallation, SonarPublisher sonarPublisher, JDK jdk, SettingsProvider settings, GlobalSettingsProvider globalSettings, boolean usesLocalRepository) throws IOException, InterruptedException {
    MavenModuleSet mavenModuleProject = sonarPublisher.getMavenProject(build);
    EnvVars envVars = build.getEnvironment(listener);
    /**
     * MAVEN_OPTS
     */
    String mvnOptions = sonarPublisher.getMavenOpts();
    if (StringUtils.isEmpty(mvnOptions) && mavenModuleProject != null && StringUtils.isNotEmpty(mavenModuleProject.getMavenOpts())) {
        mvnOptions = mavenModuleProject.getMavenOpts();
    }
    // Private Repository and Alternate Settings
    LocalRepositoryLocator locaRepositoryToUse = usesLocalRepository ? new PerJobLocalRepositoryLocator() : new DefaultLocalRepositoryLocator();
    SettingsProvider settingsToUse = settings;
    GlobalSettingsProvider globalSettingsToUse = globalSettings;
    if (mavenModuleProject != null) {
        // If we are on a Maven job then take values from the job itself
        locaRepositoryToUse = mavenModuleProject.getLocalRepository();
        settingsToUse = mavenModuleProject.getSettings();
        globalSettingsToUse = mavenModuleProject.getGlobalSettings();
    }
    // Other properties
    String additionalArguments = sonarInstallation.getAdditionalProperties();
    String analysisProperties = StringUtils.join(sonarInstallation.getAdditionalAnalysisPropertiesUnix(), ' ');
    String jobProperties = envVars.expand(sonarPublisher.getJobAdditionalProperties());
    String additionalProperties = "" + (StringUtils.isNotBlank(additionalArguments) ? additionalArguments : "") + " " + (StringUtils.isNotBlank(analysisProperties) ? analysisProperties : "") + " " + (StringUtils.isNotBlank(jobProperties) ? jobProperties : "");
    // Execute Maven
    // SONARPLUGINS-487
    String pomPath = build.getModuleRoot().child(pom).getRemote();
    return new SonarMaven(additionalProperties, mavenName, pomPath, mvnOptions, locaRepositoryToUse, sonarPublisher, listener, jdk, settingsToUse, globalSettingsToUse).perform(build, launcher, listener);
}
Also used : SettingsProvider(jenkins.mvn.SettingsProvider) GlobalSettingsProvider(jenkins.mvn.GlobalSettingsProvider) EnvVars(hudson.EnvVars) PerJobLocalRepositoryLocator(hudson.maven.local_repo.PerJobLocalRepositoryLocator) DefaultLocalRepositoryLocator(hudson.maven.local_repo.DefaultLocalRepositoryLocator) LocalRepositoryLocator(hudson.maven.local_repo.LocalRepositoryLocator) PerJobLocalRepositoryLocator(hudson.maven.local_repo.PerJobLocalRepositoryLocator) MavenModuleSet(hudson.maven.MavenModuleSet) DefaultLocalRepositoryLocator(hudson.maven.local_repo.DefaultLocalRepositoryLocator) GlobalSettingsProvider(jenkins.mvn.GlobalSettingsProvider)

Example 63 with EnvVars

use of hudson.EnvVars in project sonar-scanner-jenkins by SonarSource.

the class TriggersConfig method isSkipSonar.

public String isSkipSonar(AbstractBuild<?, ?> build, BuildListener listener) throws IOException, InterruptedException {
    Result result = build.getResult();
    if (result != null && result.isWorseThan(Result.UNSTABLE)) {
        // unstable means that build completed, but there were some test failures, which is not critical for analysis
        return Messages.SonarPublisher_BadBuildStatus(result.toString());
    }
    // skip analysis by environment variable or build parameter
    if (getEnvVar() != null) {
        // check against build parameters
        String value = build.getBuildVariableResolver().resolve(getEnvVar());
        if ("true".equalsIgnoreCase(value)) {
            return Messages.Skipping_Sonar_analysis();
        }
        // check against environment variable
        EnvVars envVars = build.getEnvironment(listener);
        value = new VariableResolver.ByMap<>(envVars).resolve(getEnvVar());
        if ("true".equalsIgnoreCase(value)) {
            return Messages.Skipping_Sonar_analysis();
        }
    }
    List<Cause> causes = new ArrayList<>(build.getCauses());
    // skip analysis, when all causes from blacklist
    if (isSkipScmCause() || isSkipUpstreamCause()) {
        Iterator<Cause> iter = causes.iterator();
        while (iter.hasNext()) {
            Cause cause = iter.next();
            if ((isSkipScmCause() && SCMTrigger.SCMTriggerCause.class.isInstance(cause)) || (isSkipUpstreamCause() && Cause.UpstreamCause.class.isInstance(cause))) {
                iter.remove();
            }
        }
    }
    return causes.isEmpty() ? Messages.Skipping_Sonar_analysis() : null;
}
Also used : EnvVars(hudson.EnvVars) Cause(hudson.model.Cause) ArrayList(java.util.ArrayList) Result(hudson.model.Result)

Example 64 with EnvVars

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

the class Promotion method getEnvironment.

@Override
public EnvVars getEnvironment(TaskListener listener) throws IOException, InterruptedException {
    EnvVars e = super.getEnvironment(listener);
    // Augment environment with target build's information
    String rootUrl = Jenkins.get().getRootUrl();
    AbstractBuild<?, ?> target = getTargetBuildOrFail();
    if (rootUrl != null)
        e.put("PROMOTED_URL", rootUrl + target.getUrl());
    e.put("PROMOTED_JOB_NAME", target.getParent().getName());
    e.put("PROMOTED_JOB_FULL_NAME", target.getParent().getFullName());
    e.put("PROMOTED_NUMBER", Integer.toString(target.getNumber()));
    e.put("PROMOTED_ID", target.getId());
    GlobalBuildPromotedBuilds globalBuildPromotedBuilds = GlobalBuildPromotedBuilds.get();
    String dateFormat = globalBuildPromotedBuilds.getDateFormat();
    String timeZone = globalBuildPromotedBuilds.getTimeZone();
    SimpleDateFormat format = null;
    TimeZone tz = null;
    if (dateFormat != null && !StringUtils.isBlank(dateFormat)) {
        try {
            format = new SimpleDateFormat(dateFormat);
        } catch (IllegalArgumentException e1) {
            LOGGER.log(Level.WARNING, String.format("An illegal date format was introduced: %s. Default ISO 8601 yyyy-MM-dd'T'HH:mmZ will be used", dateFormat), e1);
            format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
        }
    } else {
        // Per ISO 8601
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
    }
    if (timeZone != null && !StringUtils.isBlank(timeZone)) {
        try {
            tz = TimeZone.getTimeZone(timeZone);
        } catch (IllegalArgumentException e2) {
            LOGGER.log(Level.WARNING, String.format("An illegal time zone was introduced: %s. Default GMT time zone will be used", timeZone), e2);
            tz = TimeZone.getTimeZone("GMT");
        }
    } else {
        tz = TimeZone.getTimeZone("GMT");
    }
    format.setTimeZone(tz);
    e.put("PROMOTED_TIMESTAMP", format.format(new Date()));
    e.put("PROMOTED_DISPLAY_NAME", target.getDisplayName());
    e.put("PROMOTED_USER_NAME", getUserName());
    e.put("PROMOTED_USER_ID", getUserId());
    EnvVars envScm = new EnvVars();
    target.getProject().getScm().buildEnvVars(target, envScm);
    for (Entry<String, String> entry : envScm.entrySet()) {
        e.put("PROMOTED_" + entry.getKey(), entry.getValue());
    }
    // Allow the promotion status to contribute to build environment
    getStatus().buildEnvVars(this, e);
    return e;
}
Also used : TimeZone(java.util.TimeZone) EnvVars(hudson.EnvVars) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 65 with EnvVars

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

the class PromotionProcess method getDefaultParameterValuesAsEnvVars.

private static EnvVars getDefaultParameterValuesAsEnvVars(AbstractProject owner) {
    EnvVars envVars = null;
    ParametersDefinitionProperty parametersDefinitionProperty = (ParametersDefinitionProperty) owner.getProperty(ParametersDefinitionProperty.class);
    if (parametersDefinitionProperty != null) {
        envVars = new EnvVars();
        for (ParameterDefinition parameterDefinition : parametersDefinitionProperty.getParameterDefinitions()) {
            ParameterValue defaultParameterValue = parameterDefinition.getDefaultParameterValue();
            if (defaultParameterValue != null) {
                if (defaultParameterValue instanceof StringParameterValue) {
                    envVars.put(parameterDefinition.getName(), (String) defaultParameterValue.getValue());
                }
            }
        }
        EnvVars.resolve(envVars);
    }
    return envVars;
}
Also used : EnvVars(hudson.EnvVars) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) StringParameterValue(hudson.model.StringParameterValue) ParameterValue(hudson.model.ParameterValue) StringParameterValue(hudson.model.StringParameterValue) ParameterDefinition(hudson.model.ParameterDefinition)

Aggregations

EnvVars (hudson.EnvVars)72 Test (org.junit.Test)25 IOException (java.io.IOException)17 FilePath (hudson.FilePath)11 TaskListener (hudson.model.TaskListener)9 AbortException (hudson.AbortException)8 ClientConfiguration (com.amazonaws.ClientConfiguration)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Computer (hudson.model.Computer)5 FreeStyleProject (hudson.model.FreeStyleProject)5 ArgumentListBuilder (hudson.util.ArgumentListBuilder)5 ParameterValue (hudson.model.ParameterValue)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Step (org.jenkinsci.plugins.workflow.steps.Step)4 ConduitAPIClient (com.uber.jenkins.phabricator.conduit.ConduitAPIClient)3 ConduitAPIException (com.uber.jenkins.phabricator.conduit.ConduitAPIException)3 Differential (com.uber.jenkins.phabricator.conduit.Differential)3 DifferentialClient (com.uber.jenkins.phabricator.conduit.DifferentialClient)3