コード例 #1
0
  @Override
  @NotNull
  public TaskResult execute(@NotNull CommonTaskContext commonTaskContext) throws TaskException {

    final TaskResultBuilder taskResultBuilder = TaskResultBuilder.newBuilder(commonTaskContext);
    final TaskContext taskContext = Narrow.to(commonTaskContext, TaskContext.class);
    final BuildLogger buildLogger = taskContext.getBuildLogger();

    // move this crazy resolution out of here.
    resolveContext(taskContext, commonTaskContext);

    final String rubyRuntimeLabel =
        RubyBuildConfigurationPlugin.getRubyRuntime(getBuildDefinition());

    try {

      if (rubyRuntimeLabel == null) {
        throw new RuntimeLocatorException(
            "A ruby runtime has not been chosen for this plan.  Please see the miscellaneous tab to choose a plan-wide ruby runtime.");
      }

      final RubyLabel rubyLabel = RubyLabel.fromString(rubyRuntimeLabel);

      final ConfigurationMap config = commonTaskContext.getConfigurationMap();
      Map<String, String> envVars = buildEnvironment(rubyLabel, config);

      List<String> commandsList = buildCommandList(rubyLabel, config);

      ExternalProcess externalProcess =
          getProcessService()
              .createExternalProcess(
                  commonTaskContext,
                  new ExternalProcessBuilder()
                      .env(envVars)
                      .command(commandsList)
                      .workingDirectory(commonTaskContext.getWorkingDirectory()));

      externalProcess.execute();

      taskResultBuilder.checkReturnCode(externalProcess, 0);
    } catch (IllegalArgumentException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    } catch (PathNotFoundException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    } catch (RuntimeLocatorException e) {
      buildLogger.addErrorLogEntry("Could not run ruby task: " + e.getMessage(), e);
      taskResultBuilder.failed();
    }

    return taskResultBuilder.build();
  }
コード例 #2
0
  private File getZpkFromCustomDefinition() throws Exception {
    String customZpkPath = taskContext.getConfigurationMap().get("customzpk");
    if (StringUtils.isEmpty(customZpkPath)) {
      throw new Exception("No custom ZPK file specified.");
    }

    File zpk = new File(customZpkPath);

    if (zpk.isAbsolute()) {
      logger.addBuildLogEntry("Absolute path found");
      logger.addBuildLogEntry("Checking... ");
      return zpk;
    }

    return new File(getWorkingDir() + "/" + customZpkPath);
  }
コード例 #3
0
  public String getBuildNr() {
    Map<String, VariableDefinitionContext> definitions =
        taskContext.getCommonContext().getVariableContext().getDefinitions();

    String buildNumber = definitions.get("buildNumber").getValue();
    logger.addBuildLogEntry("buildNumber found: " + buildNumber);

    return buildNumber;
  }
コード例 #4
0
  private String getZpkAbsolutePath(File zpk) throws Exception {
    if (!zpk.exists()) {
      throw new Exception(
          "Cannot find ZPK file according to detected path [" + zpk.getAbsolutePath() + "]");
    }

    logger.addBuildLogEntry("ZPK found: " + zpk.getAbsolutePath());
    zpkPath = zpk.getAbsolutePath();
    return zpk.getAbsolutePath();
  }
 /**
  * Deploy all collected artifacts to Artifactory
  *
  * @param deployDetailsSet details for the artifacts we want to deploy
  * @throws IOException
  */
 private void deploy(Set<DeployDetails> deployDetailsSet) throws IOException {
   for (DeployDetails deployDetails : deployDetailsSet) {
     buildLogger.addBuildLogEntry(
         "Deploying: "
             + deployDetails.getArtifactPath()
             + " to: "
             + deployDetails.getTargetRepository());
     client.deployArtifact(deployDetails);
   }
 }
コード例 #6
0
  private File getZpkFromArtifactDownload() throws Exception {
    Iterator<TaskDefinition> taskDefinitionIterator =
        taskContext.getCommonContext().getTaskDefinitions().iterator();

    while (taskDefinitionIterator.hasNext()) {
      TaskDefinition taskDefinition = taskDefinitionIterator.next();
      if (!taskDefinition.getPluginKey().equals(ARTIFACT_DOWNLOADER_KEY)) continue;

      logger.addBuildLogEntry("Artifact Downloader Task found");
      String zpkDir =
          taskDefinition
              .getConfiguration()
              .get("localPath_0")
              .replace("${bamboo.buildNumber}", getBuildNr());
      logger.addBuildLogEntry("ZPK directory configuration found: " + zpkDir);

      String zpk = getWorkingDir() + "/" + zpkDir + "/" + getZpkFileName();

      return new File(zpk);
    }

    throw new Exception("No artifact downloader task found.");
  }
コード例 #7
0
  public String getZpkPath() throws Exception {
    if (StringUtils.isNotEmpty(zpkPath)) {
      return zpkPath;
    }
    File zpk;
    logger.addBuildLogEntry("Searching for ZPK file from custom definition");
    try {
      zpk = getZpkFromCustomDefinition();
      return getZpkAbsolutePath(zpk);
    } catch (Exception e) {
      logger.addBuildLogEntry(e.getMessage());
    }

    logger.addBuildLogEntry("Searching for ZPK file from Artifact Download");
    try {
      zpk = getZpkFromArtifactDownload();
      return getZpkAbsolutePath(zpk);
    } catch (Exception e) {
      logger.addBuildLogEntry(e.getMessage());
    }

    throw new Exception(
        "Cannot find ZPK file. Please check your custom ZPK file settings or the configuration of the Artifact Download Task.");
  }
コード例 #8
0
 protected void log(String message) {
   log.info(buildLogger.addBuildLogEntry("[RELEASE] " + message));
 }
  @NotNull
  @Override
  public TaskResult execute(@NotNull DeploymentTaskContext deploymentTaskContext)
      throws TaskException {

    buildLogger = deploymentTaskContext.getBuildLogger();
    ServerConfigManager serverConfigManager = ServerConfigManager.getInstance();
    String serverId =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + AbstractBuildContext.SERVER_ID_PARAM);
    if (StringUtils.isBlank(serverId)) {
      // Compatibility with version 1.8.0
      serverId = deploymentTaskContext.getConfigurationMap().get("artifactoryServerId");
    }
    ServerConfig serverConfig = serverConfigManager.getServerConfigById(Long.parseLong(serverId));
    if (serverConfig == null) {
      buildLogger.addErrorLogEntry(
          "Could not find Artifactpry server. Please check the Artifactory server in the task configuration.");
      return TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
    }

    repositoryKey =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + ArtifactoryDeploymentConfiguration.DEPLOYMENT_REPOSITORY);
    if (StringUtils.isBlank(repositoryKey)) {
      // Compatibility with version 1.8.0
      repositoryKey =
          deploymentTaskContext
              .getConfigurationMap()
              .get(ArtifactoryDeploymentConfiguration.DEPLOYMENT_REPOSITORY);
    }
    artifactsRootDirectory = deploymentTaskContext.getRootDirectory().getAbsolutePath();

    // Get the deployer credentials configured in the task configuration
    String username =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + ArtifactoryDeploymentConfiguration.USERNAME);
    String password =
        deploymentTaskContext
            .getConfigurationMap()
            .get(
                ArtifactoryDeploymentConfiguration.DEPLOYMENT_PREFIX
                    + ArtifactoryDeploymentConfiguration.PASSWORD);
    // If deployer credentials were not configured in the task configuration, use the credentials
    // configured
    // globally
    if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
      username = serverConfig.getUsername();
      password = serverConfig.getPassword();
    }
    TaskResult result;
    client =
        new ArtifactoryBuildInfoClient(
            serverConfig.getUrl(), username, password, new BuildInfoLog(log));

    try {
      RuntimeTaskDefinition artifactDownloadTask =
          TaskUtils.findDownloadArtifactsTask(
              deploymentTaskContext.getCommonContext().getRuntimeTaskDefinitions());
      FilesCollector filesCollector =
          new FilesCollector(artifactsRootDirectory, artifactDownloadTask);
      Map<String, Set<File>> artifacts = filesCollector.getCollectedFiles();
      Set<DeployDetails> deployDetailsSet = createDeploymentDetailsForArtifacts(artifacts);
      deploy(deployDetailsSet);
      result = TaskResultBuilder.newBuilder(deploymentTaskContext).success().build();
    } catch (Exception e) {
      buildLogger.addErrorLogEntry(
          "Error while deploying artifacts to Artifactory: " + e.getMessage());
      result = TaskResultBuilder.newBuilder(deploymentTaskContext).failedWithError().build();
    } finally {
      client.shutdown();
    }
    return result;
  }
コード例 #10
0
  @Override
  @NotNull
  public TaskResult execute(@NotNull TaskContext context) throws TaskException {
    BuildLogger logger = getBuildLogger(context);
    final ErrorMemorisingInterceptor errorLines = new ErrorMemorisingInterceptor();
    logger.getInterceptorStack().add(errorLines);
    Map<String, String> combinedMap = Maps.newHashMap();
    combinedMap.putAll(context.getConfigurationMap());
    combinedMap.putAll(context.getBuildContext().getBuildDefinition().getCustomConfiguration());
    IvyBuildContext buildContext = new IvyBuildContext(combinedMap);
    File rootDirectory = context.getRootDirectory();
    long serverId = buildContext.getArtifactoryServerId();
    try {
      ivyDependenciesDir = extractIvyDependencies(serverId, rootDirectory, buildContext);
      log.info(logger.addBuildLogEntry("Ivy dependency directory found at: " + ivyDependenciesDir));
    } catch (IOException ioe) {
      ivyDependenciesDir = null;
      logger.addBuildLogEntry(
          new ErrorLogEntry(
              "Error occurred while preparing Artifactory Ivy Runner dependencies. Build Info support is "
                  + "disabled: "
                  + ioe.getMessage()));
      log.error(
          "Error occurred while preparing Artifactory Ivy Runner dependencies. "
              + "Build Info support is disabled.",
          ioe);
    }
    if (ivyDependenciesDir == null) {
      String message = "Ivy dependency directory not found.";
      logger.addErrorLogEntry(message);
      log.error(message);
    }
    String executable = getExecutable(buildContext);
    if (StringUtils.isBlank(executable)) {
      log.error(logger.addErrorLogEntry("Cannot find ivy executable"));
      return TaskResultBuilder.create(context).failed().build();
    }
    Map<String, String> globalEnv = environmentVariableAccessor.getEnvironment();
    Map<String, String> environment = Maps.newHashMap(globalEnv);
    if (StringUtils.isNotBlank(ivyDependenciesDir)) {
      ArtifactoryBuildInfoPropertyHelper propertyHelper = new IvyPropertyHelper();
      propertyHelper.init(context.getBuildContext());
      buildInfoPropertiesFile =
          propertyHelper.createFileAndGetPath(
              buildContext,
              context.getBuildLogger(),
              environmentVariableAccessor.getEnvironment(context),
              globalEnv);
      if (StringUtils.isNotBlank(buildInfoPropertiesFile)) {
        activateBuildInfoRecording = true;
        environment.put(BuildInfoConfigProperties.PROP_PROPS_FILE, buildInfoPropertiesFile);
      }
    }
    List<String> command = Lists.newArrayList(executable);
    if (activateBuildInfoRecording) {
      command.add("-lib");
      command.add(Commandline.quoteArgument(ivyDependenciesDir));
      command.add("-listener");
      command.add(
          Commandline.quoteArgument("org.jfrog.build.extractor.listener.ArtifactoryBuildListener"));
    }
    String buildFile = buildContext.getBuildFile();
    if (StringUtils.isNotBlank(buildFile)) {
      command.addAll(Arrays.asList("-f", buildFile));
    }
    String targets = buildContext.getTargets();
    if (StringUtils.isNotBlank(targets)) {
      String[] targetTokens = StringUtils.split(targets, ' ');
      command.addAll(Arrays.asList(targetTokens));
    }

    String antOpts = buildContext.getAntOpts();
    if (StringUtils.isNotBlank(antOpts)) {
      environment.put("ANT_OPTS", antOpts);
    }
    if (StringUtils.isNotBlank(buildContext.getEnvironmentVariables())) {
      environment.putAll(
          environmentVariableAccessor.splitEnvironmentAssignments(
              buildContext.getEnvironmentVariables(), false));
    }
    String subDirectory = buildContext.getWorkingSubDirectory();
    if (StringUtils.isNotBlank(subDirectory)) {
      rootDirectory = new File(rootDirectory, subDirectory);
    }
    ExternalProcessBuilder processBuilder =
        new ExternalProcessBuilder()
            .workingDirectory(rootDirectory)
            .command(command)
            .env(environment);
    try {
      return TaskResultBuilder.create(context)
          .checkReturnCode(processService.executeProcess(context, processBuilder))
          .build();
    } finally {
      context.getBuildContext().getBuildResult().addBuildErrors(errorLines.getErrorStringList());
    }
  }