Example usage for com.amazonaws.services.codebuild.model Build getArn

List of usage examples for com.amazonaws.services.codebuild.model Build getArn

Introduction

In this page you can find the example usage for com.amazonaws.services.codebuild.model Build getArn.

Prototype


public String getArn() 

Source Link

Document

The Amazon Resource Name (ARN) of the build.

Usage

From source file:CodeBuilder.java

License:Open Source License

public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
    if (!awsClientInitFailureMessage.equals("")) {
        LoggingHelper.log(listener, configuredImproperlyError, awsClientInitFailureMessage);
        return false;
    }/* w w  w . j  ava  2s  .  co m*/
    if (!Validation.checkCodeBuilderConfig(this)) {
        LoggingHelper.log(listener, configuredImproperlyError, generalConfigInvalidError);
        return false;
    }

    final AWSCodeBuildClient cbClient;
    try {
        cbClient = awsClientFactory.getCodeBuildClient();
    } catch (Exception e) {
        LoggingHelper.log(listener, e.getMessage());
        return false;
    }

    try {
        retrieveArtifactAndSourceInfo(cbClient);
    } catch (Exception e) {
        logErrorAndNullifyBuildComponents(listener, e.getMessage(), "");
        return false;
    }

    if (SourceControlType.JenkinsSource.toString().equals(sourceControlType)) {
        if (!Validation.checkSourceTypeS3(this.projectSourceType)) {
            LoggingHelper.log(listener, invalidProjectError, "");
            return false;
        }

        String sourceS3Bucket = Utils.getS3BucketFromObjectArn(this.projectSourceLocation);
        String sourceS3Key = Utils.getS3KeyFromObjectArn(this.projectSourceLocation);
        LoggingHelper.log(listener, "Source S3 bucket is " + sourceS3Bucket);
        if (!Validation.checkBucketIsVersioned(sourceS3Bucket, awsClientFactory)) {
            LoggingHelper.log(listener, notVersionsedS3BucketError, "");
            return false;
        }

        if (s3DataManager == null) {
            s3DataManager = new S3DataManager(build.getProject().getFullName(), build.getWorkspace(),
                    build.getFullDisplayName(), awsClientFactory.getS3Client(), sourceS3Bucket, sourceS3Key);
        }
        try {
            LoggingHelper.log(listener, "Uploading source to S3.");
            UploadToS3Output uploadToS3Output = s3DataManager.uploadSourceToS3(build, launcher, listener);
            // Override source version to object version id returned by S3
            LoggingHelper.log(listener, "Source upload finished.");
            if (uploadToS3Output.getObjectVersionId() != null) {
                this.sourceVersion = uploadToS3Output.getObjectVersionId();
            } else {
                LoggingHelper.log(listener, notVersionsedS3BucketError, "");
                return false;
            }
            LoggingHelper.log(listener, "S3 object version id for uploaded source is " + this.sourceVersion);
        } catch (Exception e) {
            logErrorAndNullifyBuildComponents(listener, e.getMessage(), "");
            return false;
        }
    }

    StartBuildRequest startBuildRequest = new StartBuildRequest().withProjectName(this.projectName)
            .withSourceVersion(sourceVersion);
    LoggingHelper.log(listener, "Starting build with projectName " + this.projectName + " and source version "
            + this.sourceVersion);
    final StartBuildResult sbResult;
    try {
        sbResult = cbClient.startBuild(startBuildRequest);
    } catch (Exception e) {
        logErrorAndNullifyBuildComponents(listener, e.getMessage(), "");
        return false;
    }

    Build currentBuild;
    String buildId = sbResult.getBuild().getId();
    boolean haveInitializedAction = false;

    if (action == null) {
        action = new CodeBuildAction(build); //the entity that creates the codebuild dashboard.
    }

    //poll buildResult for build status until it's complete.
    do {
        try {
            List<Build> buildsForId = cbClient.batchGetBuilds(new BatchGetBuildsRequest().withIds(buildId))
                    .getBuilds();

            if (buildsForId.size() != 1) {
                throw new Exception("Multiple builds mapped to this build id.");
            }

            currentBuild = buildsForId.get(0);

            if (!haveInitializedAction) {
                if (logMonitor == null) {
                    logMonitor = new CloudWatchMonitor(awsClientFactory.getCloudWatchLogsClient());
                }

                updateDashboard(currentBuild);

                //only need to set these once, the others will need to be updated below as the build progresses.
                String buildARN = currentBuild.getArn();
                action.setBuildARN(buildARN);
                action.setStartTime(currentBuild.getStartTime().toString());
                action.setS3ArtifactURL(
                        generateS3ArtifactURL(this.s3DashboardURL, artifactLocation, artifactType));
                action.setS3BucketName(artifactLocation);

                build.addAction(action);
                haveInitializedAction = true;
            }
            Thread.sleep(5000L);
            logMonitor.pollForLogs();
            updateDashboard(currentBuild);

        } catch (Exception e) {
            logErrorAndNullifyBuildComponents(listener, e.getMessage(), "");
            return failActionAndReturnFalse();
        }
    } while (currentBuild.getBuildStatus().equals(StatusType.IN_PROGRESS.toString()));

    Boolean jenkinsBuildResult;
    if (currentBuild.getBuildStatus().equals(StatusType.SUCCEEDED.toString().toUpperCase(Locale.ENGLISH))) {
        action.setJenkinsBuildSucceeds(true);
        jenkinsBuildResult = true;
    } else {
        action.setJenkinsBuildSucceeds(false);
        jenkinsBuildResult = false;
    }

    nullifyBuildComponents();
    return jenkinsBuildResult;
}