private void resolveContext(TaskContext taskContext, CommonTaskContext commonTaskContext) { // This is a hack to ultimately resolve the BuildDefinition. Unfortunately this is not well // encapsulated and the lack of // information provided to the deploy task makes it difficult to navigate back to. if (taskContext != null) { // taskContext is null for deploy tasks setBuildContext(taskContext.getBuildContext().getParentBuildContext()); setBuildDefinition(getBuildContext().getBuildDefinition()); } else { // this is a deploy task final DeploymentTaskContext deploymentTaskContext = Narrow.to(commonTaskContext, DeploymentTaskContext.class); long environmentId = deploymentTaskContext.getDeploymentContext().getEnvironmentId(); DeploymentProject deploymentProject = getDeploymentProjectService().getDeploymentProjectForEnvironment(environmentId); Plan plan = getPlanManager().getPlanByKey(deploymentProject.getPlanKey()); setBuildDefinition(plan.getBuildDefinition()); } }
@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; }