Search in sources :

Example 1 with ArtifactoryBuildInfoClient

use of org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient in project build-info by JFrogDev.

the class ArtifactoryBuildListener method doDeploy.

private void doDeploy(BuildEvent event) {
    IvyBuildInfoLog log = getBuildInfoLog(event);
    log.info("[buildinfo:ant] Starting deployment");
    Project project = event.getProject();
    Set<DeployDetails> deployDetails = ctx.getDeployDetails();
    BuildInfoBuilder builder = new BuildInfoBuilder(project.getName()).modules(ctx.getModules()).number("0").durationMillis(System.currentTimeMillis() - ctx.getBuildStartTime()).startedDate(new Date(ctx.getBuildStartTime())).buildAgent(new BuildAgent("Ivy", Ivy.getIvyVersion())).agent(new Agent("Ivy", Ivy.getIvyVersion()));
    // This is here for backwards compatibility.
    builder.type(BuildType.IVY);
    ArtifactoryClientConfiguration clientConf = ctx.getClientConf();
    String agentName = clientConf.info.getAgentName();
    String agentVersion = clientConf.info.getAgentVersion();
    if (StringUtils.isNotBlank(agentName) && StringUtils.isNotBlank(agentVersion)) {
        builder.agent(new Agent(agentName, agentVersion));
    }
    String buildAgentName = clientConf.info.getBuildAgentName();
    String buildAgentVersion = clientConf.info.getBuildAgentVersion();
    if (StringUtils.isNotBlank(buildAgentName) && StringUtils.isNotBlank(buildAgentVersion)) {
        builder.buildAgent(new BuildAgent(buildAgentName, buildAgentVersion));
    }
    String buildName = clientConf.info.getBuildName();
    if (StringUtils.isNotBlank(buildName)) {
        builder.name(buildName);
    }
    String buildNumber = clientConf.info.getBuildNumber();
    if (StringUtils.isNotBlank(buildNumber)) {
        builder.number(buildNumber);
    }
    String buildUrl = clientConf.info.getBuildUrl();
    if (StringUtils.isNotBlank(buildUrl)) {
        builder.url(buildUrl);
    }
    Vcs vcs = new Vcs();
    String vcsRevision = clientConf.info.getVcsRevision();
    if (StringUtils.isNotBlank(vcsRevision)) {
        vcs.setRevision(vcsRevision);
        builder.vcsRevision(vcsRevision);
    }
    String vcsUrl = clientConf.info.getVcsUrl();
    if (StringUtils.isNotBlank(vcsUrl)) {
        vcs.setUrl(vcsUrl);
        builder.vcsUrl(vcsUrl);
    }
    if (!vcs.isEmpty()) {
        builder.vcs(Arrays.asList(vcs));
    }
    String artifactoryPluginVersion = clientConf.info.getArtifactoryPluginVersion();
    if (StringUtils.isNotBlank(artifactoryPluginVersion)) {
        builder.artifactoryPluginVersion(artifactoryPluginVersion);
    } else {
        builder.artifactoryPluginVersion("Unknown");
    }
    String principal = clientConf.info.getPrincipal();
    if (StringUtils.isNotBlank(principal)) {
        builder.principal(principal);
    }
    String parentBuildName = clientConf.info.getParentBuildName();
    if (StringUtils.isNotBlank(parentBuildName)) {
        builder.parentName(parentBuildName);
    }
    String parentBuildNumber = clientConf.info.getParentBuildNumber();
    if (StringUtils.isNotBlank(parentBuildNumber)) {
        builder.parentNumber(parentBuildNumber);
    }
    LicenseControl licenseControl = new LicenseControl(clientConf.info.licenseControl.isRunChecks());
    String notificationRecipients = clientConf.info.licenseControl.getViolationRecipients();
    if (StringUtils.isNotBlank(notificationRecipients)) {
        licenseControl.setLicenseViolationsRecipientsList(notificationRecipients);
    }
    licenseControl.setIncludePublishedArtifacts(clientConf.info.licenseControl.isIncludePublishedArtifacts());
    String scopes = clientConf.info.licenseControl.getScopes();
    if (StringUtils.isNotBlank(scopes)) {
        licenseControl.setScopesList(scopes);
    }
    licenseControl.setAutoDiscover(clientConf.info.licenseControl.isAutoDiscover());
    builder.licenseControl(licenseControl);
    final BlackDuckProperties blackDuckProperties;
    if (clientConf.info.blackDuckProperties.isRunChecks()) {
        blackDuckProperties = clientConf.info.blackDuckProperties.copyBlackDuckProperties();
    } else {
        blackDuckProperties = new BlackDuckProperties();
    }
    Governance governance = new Governance();
    governance.setBlackDuckProperties(blackDuckProperties);
    builder.governance(governance);
    String issueTrackerName = clientConf.info.issues.getIssueTrackerName();
    if (StringUtils.isNotBlank(issueTrackerName)) {
        Issues issues = new Issues();
        issues.setAggregateBuildIssues(clientConf.info.issues.getAggregateBuildIssues());
        issues.setAggregationBuildStatus(clientConf.info.issues.getAggregationBuildStatus());
        issues.setTracker(new IssueTracker(issueTrackerName, clientConf.info.issues.getIssueTrackerVersion()));
        Set<Issue> affectedIssuesSet = clientConf.info.issues.getAffectedIssuesSet();
        if (!affectedIssuesSet.isEmpty()) {
            issues.setAffectedIssues(affectedIssuesSet);
        }
        builder.issues(issues);
    }
    for (Map.Entry<String, String> runParam : clientConf.info.getRunParameters().entrySet()) {
        MatrixParameter matrixParameter = new MatrixParameter(runParam.getKey(), runParam.getValue());
        builder.addRunParameters(matrixParameter);
    }
    if (clientConf.isIncludeEnvVars()) {
        Properties envProperties = new Properties();
        envProperties.putAll(clientConf.getAllProperties());
        envProperties = BuildInfoExtractorUtils.getEnvProperties(envProperties, clientConf.getLog());
        for (Map.Entry<Object, Object> envProp : envProperties.entrySet()) {
            builder.addProperty(envProp.getKey(), envProp.getValue());
        }
    }
    Build build = builder.build();
    String contextUrl = clientConf.publisher.getContextUrl();
    String username = clientConf.publisher.getUsername();
    String password = clientConf.publisher.getPassword();
    ArtifactoryBuildInfoClient client = new ArtifactoryBuildInfoClient(contextUrl, username, password, log);
    try {
        configureProxy(clientConf, client);
        configConnectionTimeout(clientConf, client);
        configRetriesParams(clientConf, client);
        if (clientConf.publisher.isPublishArtifacts()) {
            IncludeExcludePatterns patterns = new IncludeExcludePatterns(clientConf.publisher.getIncludePatterns(), clientConf.publisher.getExcludePatterns());
            deployArtifacts(project, client, deployDetails, patterns);
        }
        if (clientConf.publisher.isPublishBuildInfo()) {
            Utils.sendBuildAndBuildRetention(client, build, clientConf);
        }
        isDidDeploy = true;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}
Also used : IncludeExcludePatterns(org.jfrog.build.extractor.clientConfiguration.IncludeExcludePatterns) ArtifactoryClientConfiguration(org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration) DeployDetails(org.jfrog.build.client.DeployDetails) IOException(java.io.IOException) ArtifactoryBuildInfoClient(org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient) BuildInfoBuilder(org.jfrog.build.api.builder.BuildInfoBuilder) IvyBuildInfoLog(org.jfrog.build.util.IvyBuildInfoLog)

Example 2 with ArtifactoryBuildInfoClient

use of org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient in project build-info by JFrogDev.

the class DeployTask method prepareAndDeploy.

/**
 * This method will be activated only at the "end" of the build, when we reached the root project.
 *
 * @throws java.io.IOException In case the deployment fails.
 */
private void prepareAndDeploy() throws IOException {
    ArtifactoryClientConfiguration accRoot = ArtifactoryPluginUtil.getArtifactoryConvention(getProject()).getClientConfig();
    Map<String, String> propsRoot = accRoot.publisher.getProps();
    // Reset the default properties, they may have changed
    GradleArtifactoryClientConfigUpdater.setMissingBuildAttributes(accRoot, getProject().getRootProject());
    Set<GradleDeployDetails> allDeployDetails = Sets.newTreeSet();
    List<ArtifactoryTask> orderedTasks = findArtifactoryPublishTasks(getProject().getGradle().getTaskGraph());
    for (ArtifactoryTask artifactoryTask : orderedTasks) {
        if (artifactoryTask.getDidWork()) {
            ArtifactoryClientConfiguration.PublisherHandler publisher = ArtifactoryPluginUtil.getPublisherHandler(artifactoryTask.getProject());
            if (publisher != null && publisher.getContextUrl() != null) {
                Map<String, String> moduleProps = new HashMap<String, String>(propsRoot);
                moduleProps.putAll(publisher.getProps());
                publisher.getProps().putAll(moduleProps);
                String contextUrl = publisher.getContextUrl();
                String username = publisher.getUsername();
                String password = publisher.getPassword();
                if (StringUtils.isBlank(username)) {
                    username = "";
                }
                if (StringUtils.isBlank(password)) {
                    password = "";
                }
                artifactoryTask.collectDescriptorsAndArtifactsForUpload();
                if (publisher.isPublishArtifacts()) {
                    ArtifactoryBuildInfoClient client = null;
                    try {
                        client = new ArtifactoryBuildInfoClient(contextUrl, username, password, new GradleClientLogger(log));
                        log.debug("Uploading artifacts to Artifactory at '{}'", contextUrl);
                        IncludeExcludePatterns patterns = new IncludeExcludePatterns(publisher.getIncludePatterns(), publisher.getExcludePatterns());
                        configureProxy(accRoot, client);
                        configConnectionTimeout(accRoot, client);
                        configRetriesParams(accRoot, client);
                        deployArtifacts(artifactoryTask.deployDetails, client, patterns);
                    } finally {
                        if (client != null) {
                            client.close();
                        }
                    }
                }
                allDeployDetails.addAll(artifactoryTask.deployDetails);
            }
        } else {
            log.debug("Task '{}' did no work", artifactoryTask.getPath());
        }
    }
    ArtifactoryBuildInfoClient client = null;
    String contextUrl = accRoot.publisher.getContextUrl();
    String username = accRoot.publisher.getUsername();
    String password = accRoot.publisher.getPassword();
    if (contextUrl != null) {
        if (StringUtils.isBlank(username)) {
            username = "";
        }
        if (StringUtils.isBlank(password)) {
            password = "";
        }
        try {
            client = new ArtifactoryBuildInfoClient(accRoot.publisher.getContextUrl(), accRoot.publisher.getUsername(), accRoot.publisher.getPassword(), new GradleClientLogger(log));
            configureProxy(accRoot, client);
            configConnectionTimeout(accRoot, client);
            configRetriesParams(accRoot, client);
            GradleBuildInfoExtractor gbie = new GradleBuildInfoExtractor(accRoot, allDeployDetails);
            Build build = gbie.extract(getProject().getRootProject());
            exportBuildInfo(build, getExportFile(accRoot));
            if (isPublishBuildInfo(accRoot)) {
                // If export property set always save the file before sending it to artifactory
                exportBuildInfo(build, getExportFile(accRoot));
                if (accRoot.info.isIncremental()) {
                    log.debug("Publishing build info modules to artifactory at: '{}'", contextUrl);
                    client.sendModuleInfo(build);
                } else {
                    log.debug("Publishing build info to artifactory at: '{}'", contextUrl);
                    Utils.sendBuildAndBuildRetention(client, build, accRoot);
                }
            }
            if (isGenerateBuildInfoToFile(accRoot)) {
                try {
                    exportBuildInfo(build, new File(accRoot.info.getGeneratedBuildInfoFilePath()));
                } catch (Exception e) {
                    log.error("Failed writing build info to file: ", e);
                    throw new IOException("Failed writing build info to file", e);
                }
            }
            if (isGenerateDeployableArtifactsToFile(accRoot)) {
                try {
                    exportDeployableArtifacts(allDeployDetails, new File(accRoot.info.getDeployableArtifactsFilePath()));
                } catch (Exception e) {
                    log.error("Failed writing deployable artifacts to file: ", e);
                    throw new RuntimeException("Failed writing deployable artifacts to file", e);
                }
            }
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }
}
Also used : ArtifactoryClientConfiguration(org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration) GradleClientLogger(org.jfrog.gradle.plugin.artifactory.extractor.GradleClientLogger) IncludeExcludePatterns(org.jfrog.build.extractor.clientConfiguration.IncludeExcludePatterns) IOException(java.io.IOException) GradleDeployDetails(org.jfrog.gradle.plugin.artifactory.extractor.GradleDeployDetails) IOException(java.io.IOException) ArtifactoryBuildInfoClient(org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient) GradleBuildInfoExtractor(org.jfrog.gradle.plugin.artifactory.extractor.GradleBuildInfoExtractor) Build(org.jfrog.build.api.Build) File(java.io.File)

Example 3 with ArtifactoryBuildInfoClient

use of org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient in project build-info by JFrogDev.

the class BuildInfoClientBuilder method resolveProperties.

public ArtifactoryBuildInfoClient resolveProperties(ArtifactoryClientConfiguration clientConf) {
    ArtifactoryBuildInfoClient client = resolveClientProps(clientConf);
    resolveTimeout(clientConf, client);
    resolveProxy(clientConf.proxy, client);
    resolveRetriesParams(clientConf, client);
    return client;
}
Also used : ArtifactoryBuildInfoClient(org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient)

Example 4 with ArtifactoryBuildInfoClient

use of org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient in project build-info by JFrogDev.

the class BuildDeploymentHelper method publishBuildInfo.

private void publishBuildInfo(ArtifactoryClientConfiguration clientConf, Build build, File buildInfoAggregated) {
    ArtifactoryBuildInfoClient client = buildInfoClientBuilder.resolveProperties(clientConf);
    logger.info("Artifactory Build Info Recorder: Deploying build info ...");
    try {
        if (buildInfoAggregated != null) {
            String buildInfoJson = client.buildInfoToJsonString(build);
            String buildInfoAggregatedJson = FileUtils.readFileToString(buildInfoAggregated, "UTF-8");
            String buildInfoMerged = buildInfoMergeHelper.mergeJsons(buildInfoAggregatedJson, buildInfoJson);
            client.sendBuildInfo(buildInfoMerged);
        } else {
            Utils.sendBuildAndBuildRetention(client, build, clientConf);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        client.close();
    }
}
Also used : IOException(java.io.IOException) ArtifactoryBuildInfoClient(org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient)

Aggregations

ArtifactoryBuildInfoClient (org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryBuildInfoClient)4 IOException (java.io.IOException)3 ArtifactoryClientConfiguration (org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration)2 IncludeExcludePatterns (org.jfrog.build.extractor.clientConfiguration.IncludeExcludePatterns)2 File (java.io.File)1 Build (org.jfrog.build.api.Build)1 BuildInfoBuilder (org.jfrog.build.api.builder.BuildInfoBuilder)1 DeployDetails (org.jfrog.build.client.DeployDetails)1 IvyBuildInfoLog (org.jfrog.build.util.IvyBuildInfoLog)1 GradleBuildInfoExtractor (org.jfrog.gradle.plugin.artifactory.extractor.GradleBuildInfoExtractor)1 GradleClientLogger (org.jfrog.gradle.plugin.artifactory.extractor.GradleClientLogger)1 GradleDeployDetails (org.jfrog.gradle.plugin.artifactory.extractor.GradleDeployDetails)1