public BuildProcess createBuildProcess()

in aws-codebuild-agent/src/main/java/jetbrains/buildServer/aws/codebuild/CodeBuildRunner.java [42:191]


  public BuildProcess createBuildProcess(@NotNull final AgentRunningBuild runningBuild, @NotNull final BuildRunnerContext context) throws RunBuildException {
    return new SyncBuildProcessAdapter() {
      @NotNull
      @Override
      protected BuildFinishedStatus runImpl() throws RunBuildException {
        final Map<String, String> runnerParameters = validateParams();
        final String projectName = getProjectName(runnerParameters);
        final String buildId = AWSCommonParams.withAWSClients(runnerParameters, new AWSCommonParams.WithAWSClients<String, RunBuildException>() {
          @Nullable
          @Override
          public String run(@NotNull AWSClients clients) throws RunBuildException {
            return clients.createCodeBuildClient().startBuild(
              new StartBuildRequest()
                .withProjectName(projectName)
                .withSourceVersion(getSourceVersion(projectName))
                .withBuildspecOverride(getBuildSpec(runnerParameters))
                .withArtifactsOverride(getArtifacts())
                .withTimeoutInMinutesOverride(getTimeoutMinutesInt(runnerParameters))
                .withEnvironmentVariablesOverride(getEnvironmentVariables())).getBuild().getId();
          }
        });

        runningBuild.addSharedSystemProperty(String.format(CodeBuildConstants.BUILD_ID_SYSTEM_PROPERTY_FORMAT, context.getId()), buildId);

        final String region = runnerParameters.get(AWSCommonParams.REGION_NAME_PARAM);
        runningBuild.getBuildLogger().message(projectName + " build " + getBuildLink(buildId, region) + " started");
        runningBuild.getBuildLogger().message("View the entire log in the AWS CloudWatch console " + getBuildLogLink(buildId, projectName, region));

        final CodeBuildBuildContext c = new CodeBuildBuildContext(buildId, projectName, runnerParameters);
        if (isWaitStep(runnerParameters)) {
          startContext(c, runningBuild);
          try {
            while (!finished(c, runningBuild)) {
              if (isInterrupted()) {
                CodeBuildRunner.this.interrupt(c, runningBuild);
                break;
              }
              try {
                Thread.sleep(CodeBuildConstants.POLL_INTERVAL);
              } catch (InterruptedException e) {
                break;
              }
            }
          } finally {
            log(runningBuild, getBlockEnd(c));
          }
        } else if (isWaitBuild(runnerParameters)) {
          myCodeBuildBuilds.add(c);
        }
        return isInterrupted() ? BuildFinishedStatus.INTERRUPTED : BuildFinishedStatus.FINISHED_SUCCESS;
      }

      @Nullable
      private String getSourceVersion(@NotNull String projectName) throws RunBuildException {
        final Map<String, String> params = context.getRunnerParameters();
        if (isUseBuildRevision(params)) {
          final ProjectInfo project = getProject(params, projectName);
          if (project == null) {
            throw new RunBuildException("No AWS CodeBuild project " + projectName + " found. Please check the settings.");
          }
          if (SourceType.GITHUB.toString().equals(project.getSourceType())) {
            final String vcsRootId = runningBuild.getSharedConfigParameters().get(CodeBuildConstants.GIT_HUB_VCS_ROOT_ID_CONFIG_PARAM);

            if (StringUtil.isEmptyOrSpaces(vcsRootId) || CodeBuildConstants.UNKNOWN_GIT_HUB_VCS_ROOT_ID.equals(vcsRootId)) {
              throw new RunBuildException("Failed to find the GitHub VCS root ID and use it to resolve " + CodeBuildConstants.SOURCE_VERSION_LABEL + " AWS CodeBuild setting");
            }

            final String sysPropName = "build.vcs.number." + vcsRootId;
            final String sourceVersion = context.getBuildParameters().getSystemProperties().get(sysPropName);

            if (StringUtil.isEmptyOrSpaces(sourceVersion)) {
              throw new RunBuildException("Can't use empty %" + sysPropName + "% system property value as " + CodeBuildConstants.SOURCE_VERSION_LABEL + " AWS CodeBuild setting");
            }

            runningBuild.getBuildLogger().message("Using %" + sysPropName + "% system property value " + sourceVersion + " as the AWS CodeBuild source version");
            return sourceVersion;

          } else if (SourceType.S3.toString().equals(project.getSourceType())) {
            final File revision = prepareRevision();
            if (revision == null) {
              throw new RunBuildException("Unable to upload sources to the AWS S3: build checkout directory " + runningBuild.getCheckoutDirectory() + " is empty");
            }
            try {
              return AWSCommonParams.withAWSClients(params, new AWSCommonParams.WithAWSClients<String, RuntimeException>() {
                @Nullable
                @Override
                public String run(@NotNull AWSClients clients) throws RuntimeException {
                  return clients.createS3Client().putObject(getBucketName(project.getSourceLocation()), getObjectKey(project.getSourceLocation()), revision).getVersionId();
                }
              });
            } finally {
              FileUtil.delete(revision);
            }
          } else {
            throw new RunBuildException(CodeBuildConstants.USE_BUILD_REVISION_LABEL + " setting is supported only for Amazon S3 and GitHub AWS CodeBuild project source provider and can't be combined with " + project.getSourceType() + " source provider");
          }
        } else {
          return CodeBuildUtil.getSourceVersion(params);
        }
      }

      @Nullable
      private File prepareRevision() throws RunBuildException {
        final File[] files = runningBuild.getCheckoutDirectory().listFiles();
        if (files == null || files.length == 0) return null;
        if (files.length == 1 && files[0].getName().endsWith(".zip")) {
          return files[0];
        } else {
          final File revision = new File(runningBuild.getBuildTempDirectory() + "/" + runningBuild.getCheckoutDirectory().getName() + ".zip");
          try {
            ArchiveUtil.packZip(runningBuild.getCheckoutDirectory(), new ZipOutputStream(new FileOutputStream(revision)));
          } catch (FileNotFoundException e) {
            throw new RunBuildException("Failed to package the checkout directory content", e);
          }
          return revision;
        }
      }

      @NotNull
      private Collection<EnvironmentVariable> getEnvironmentVariables() {
        runningBuild.getBuildLogger().message("Will pass build system properties as Environment variables to the AWS CodeBuild");
        return CollectionsUtil.convertCollection(context.getBuildParameters().getSystemProperties().entrySet(), new Converter<EnvironmentVariable, Map.Entry<String, String>>() {
          @Override
          public EnvironmentVariable createFrom(@NotNull Map.Entry<String, String> e) {
            return new EnvironmentVariable().withName(e.getKey()).withValue(e.getValue());
          }
        });
      }

      @Nullable
      private ProjectArtifacts getArtifacts() {
        final Map<String, String> params = context.getRunnerParameters();
        return isUploadS3Artifacts(params) ?
          new ProjectArtifacts()
            .withType(ArtifactsType.S3)
            .withPackaging(isZipS3Artifacts(params) ? ArtifactPackaging.ZIP : ArtifactPackaging.NONE)
            .withName(getArtifactS3Name(params))
            .withLocation(getArtifactS3Bucket(params))
          : null;
      }

      @NotNull
      private Map<String, String> validateParams() throws RunBuildException {
        final Map<String, String> runnerParameters = context.getRunnerParameters();
        final Map<String, String> invalids = ParametersValidator.validateSettings(runnerParameters, false);
        if (invalids.isEmpty()) return runnerParameters;
        throw new RunBuildException(StringUtil.join(invalids.values(), "\n"), null, ErrorData.BUILD_RUNNER_ERROR_TYPE);
      }
    };
  }