Skip to content

Commit

Permalink
TCAP-307 - Release management runs on each step separately
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Apr 8, 2018
1 parent c4ccc9d commit a9c7000
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import org.jfrog.teamcity.agent.listener.AgentListenerBuildInfoHelper;
import org.jfrog.teamcity.agent.listener.AgentListenerReleaseHelper;
import org.jfrog.teamcity.agent.listener.AgentListenerXrayScanHelper;
import org.jfrog.teamcity.agent.release.ReleaseParameters;
import org.jfrog.teamcity.agent.util.AgentUtils;
import org.jfrog.teamcity.common.RunnerParameterKeys;

import java.util.List;
import java.util.Map;

import static org.jfrog.teamcity.common.ConstantValues.PROP_SKIP_LOG_MESSAGE;
import static org.jfrog.teamcity.common.ConstantValues.ARTIFACTORY_PLUGIN_VERSION;
import static org.jfrog.teamcity.common.ConstantValues.PROP_SKIP_LOG_MESSAGE;

public class ArtifactoryAgentListener extends AgentLifeCycleAdapter {

Expand Down Expand Up @@ -119,8 +119,7 @@ public void runnerFinished(@NotNull BuildRunnerContext runner, @NotNull BuildFin
} catch (Throwable t) {
logException(runner, t);
} finally {
ReleaseParameters releaseParams = new ReleaseParameters(runner.getBuild());
if (releaseParams.isReleaseBuild()) {
if (AgentUtils.isReleaseManagementEnabled(runner)) {
BuildInterruptReason buildInterruptReason =
runner.getBuild().getInterruptReason();
boolean buildSuccessful = !buildStatus.isFailed() && (buildInterruptReason == null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jfrog.teamcity.agent.api.Gavc;
import org.jfrog.teamcity.agent.listener.AgentListenerBuildInfoHelper;
import org.jfrog.teamcity.agent.release.ReleaseParameters;
import org.jfrog.teamcity.agent.util.AgentUtils;
import org.jfrog.teamcity.agent.util.InfoCollectionException;
import org.jfrog.teamcity.agent.util.RepositoryHelper;
import org.jfrog.teamcity.common.ReleaseManagementParameterKeys;
Expand Down Expand Up @@ -91,8 +92,8 @@ protected void appendRunnerSpecificDetails(BuildInfoBuilder builder, File mavenB
throw new InfoCollectionException("Error while parsing Maven build info report.", e);
}

ReleaseParameters releaseParams = new ReleaseParameters(runnerContext.getBuild());
if (releaseParams.isReleaseBuild()) {
if (AgentUtils.isReleaseManagementEnabled(runnerContext)) {
ReleaseParameters releaseParams = new ReleaseParameters(runnerContext.getBuild());
builder.addStatus(new PromotionStatusBuilder(Promotion.STAGED)
.timestampDate(new Date(Long.parseLong(runnerParams.get(BUILD_STARTED))))
.comment(releaseParams.getStagingComment())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jfrog.build.extractor.clientConfiguration.ArtifactoryClientConfiguration;
import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask;
import org.jfrog.teamcity.agent.release.ReleaseParameters;
import org.jfrog.teamcity.agent.util.AgentUtils;
import org.jfrog.teamcity.agent.util.ArtifactoryClientConfigurationBuilder;
import org.jfrog.teamcity.common.ConstantValues;
import org.jfrog.teamcity.common.RunTypeUtils;
Expand Down Expand Up @@ -101,8 +102,8 @@ private void addBuildInfoTaskAndInitScript(BuildRunnerContext runnerContext) {
ArtifactoryClientConfiguration clientConf = ArtifactoryClientConfigurationBuilder.create(runnerContext);
try {

ReleaseParameters releaseParams = new ReleaseParameters(build);
if (releaseParams.isReleaseBuild()) {
if (AgentUtils.isReleaseManagementEnabled(runnerContext)) {
ReleaseParameters releaseParams = new ReleaseParameters(build);
clientConf.info.setReleaseEnabled(true);
String comment = releaseParams.getStagingComment();
if (StringUtils.isNotBlank(comment)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jfrog.teamcity.agent.release.ReleaseParameters;
import org.jfrog.teamcity.agent.release.vcs.AbstractVcsCoordinator;
import org.jfrog.teamcity.agent.release.vcs.VcsCoordinator;
import org.jfrog.teamcity.agent.util.AgentUtils;
import org.jfrog.teamcity.common.ConstantValues;
import org.jfrog.teamcity.common.RunTypeUtils;

Expand All @@ -51,8 +52,8 @@ public void beforeRunnerStart(BuildRunnerContext runner) throws IOException, Int
AgentRunningBuild build = runner.getBuild();
BuildProgressLogger logger = build.getBuildLogger();

ReleaseParameters releaseParams = new ReleaseParameters(build);
if (releaseParams.isReleaseBuild()) {
if (AgentUtils.isReleaseManagementEnabled(runner)) {
ReleaseParameters releaseParams = new ReleaseParameters(build);
logger.progressStarted("[RELEASE] Release build triggered");
vcsCoordinator = AbstractVcsCoordinator.createVcsCoordinator(runner);
vcsCoordinator.prepare();
Expand All @@ -72,14 +73,14 @@ public void beforeRunnerStart(BuildRunnerContext runner) throws IOException, Int

public void runnerFinished(BuildRunnerContext runner, BuildFinishedStatus buildStatus) throws Exception {
AgentRunningBuild build = runner.getBuild();
ReleaseParameters releaseParams = new ReleaseParameters(build);
// fire successful build event build to release management
if (!releaseParams.isReleaseBuild() || buildStatus.isFailed()) {
if (!AgentUtils.isReleaseManagementEnabled(runner) || buildStatus.isFailed()) {
return;
}

vcsCoordinator.afterSuccessfulReleaseVersionBuild();

ReleaseParameters releaseParams = new ReleaseParameters(build);
if (!releaseParams.isNoVersionChange()) {
vcsCoordinator.beforeDevelopmentVersionChange();
// change poms versions to next development version
Expand Down
16 changes: 16 additions & 0 deletions agent/src/main/java/org/jfrog/teamcity/agent/util/AgentUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jfrog.teamcity.agent.util;

import jetbrains.buildServer.agent.BuildRunnerContext;
import org.jfrog.teamcity.agent.release.ReleaseParameters;
import org.jfrog.teamcity.common.RunnerParameterKeys;

public class AgentUtils {

public static boolean isReleaseManagementEnabled(BuildRunnerContext runner) {
ReleaseParameters releaseParams = new ReleaseParameters(runner.getBuild());
if (!releaseParams.isReleaseBuild()) {
return false;
}
return Boolean.parseBoolean(runner.getRunnerParameters().get(RunnerParameterKeys.ENABLE_RELEASE_MANAGEMENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void testBeforeRunnerStart() throws Exception {
EasyMock.expect(buildAgentConfiguration.getAgentPluginsDirectory()).andReturn(Files.createTempDir());

EasyMock.expect(agentRunningBuild.getAgentConfiguration()).andReturn(buildAgentConfiguration);
EasyMock.expect(runner.getBuild()).andReturn(agentRunningBuild).times(3);
EasyMock.expect(runner.getBuild()).andReturn(agentRunningBuild).times(4);

watcher.addNewArtifactsPath(EasyMock.isA(String.class));
EasyMock.expectLastCall();
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<plugin.version>${project.version}</plugin.version>
<teamcity.version>7.1.0</teamcity.version>
<build.info.version>2.10.0</build.info.version>
<build.info.maven3.version>2.10.0</build.info.maven3.version>
<build.info.gradle.version>4.7.0</build.info.gradle.version>
<build.info.ivy.version>2.10.0</build.info.ivy.version>
<build.info.version>2.10.1</build.info.version>
<build.info.maven3.version>2.10.1</build.info.maven3.version>
<build.info.gradle.version>4.7.1</build.info.gradle.version>
<build.info.ivy.version>2.10.1</build.info.ivy.version>
</properties>

<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,27 @@ public boolean isAvailable(@NotNull HttpServletRequest request) {
}

SFinishedBuild lastFinishedBuild = buildType.getLastChangesFinished();
return (lastFinishedBuild != null) && (getFirstReleaseManagementEnabledRunner(buildType) != null);
return (lastFinishedBuild != null) && (!getReleaseManagementRunners(buildType).isEmpty());
}

@Override
public void fillModel(@NotNull Map<String, Object> model, @NotNull HttpServletRequest request,
@NotNull SBuildType buildType, @Nullable SUser user) {
String buildTypeId = buildType.getBuildTypeId();
SBuildRunnerDescriptor buildRunner = getFirstReleaseManagementEnabledRunner(buildType);
if (buildRunner == null) {
setIncludeUrl("releaseManagementErrorTab.jsp");
model.put("errorMessage", "Unable to find a runner configured with release management.");
List<SBuildRunnerDescriptor> buildRunner = getReleaseManagementRunners(buildType);
if (buildRunner.isEmpty()) {
putErr(model, "Unable to find a runner configured with release management.");
return;
}
if (buildRunner.size() > 1) {
putErr(model, "Only one build step is allowed for release management.");
return;
}

// fill default values to the model
ReleaseManagementConfigModel managementConfig = getReleaseManagementConfigModel();

Map<String, String> parameters = buildRunner.getParameters();
Map<String, String> parameters = buildRunner.get(0).getParameters();
if (parameters.containsKey(RunnerParameterKeys.GIT_RELEASE_BRANCH_NAME_PREFIX)) {
managementConfig.setGitReleaseBranchNamePrefix(
parameters.get(RunnerParameterKeys.GIT_RELEASE_BRANCH_NAME_PREFIX));
Expand Down Expand Up @@ -163,13 +166,19 @@ private boolean containsGitOrSvnVcsRoot(SBuildType buildType) {
return false;
}

private SBuildRunnerDescriptor getFirstReleaseManagementEnabledRunner(SBuildType buildType) {
private List<SBuildRunnerDescriptor> getReleaseManagementRunners(SBuildType buildType) {
List<SBuildRunnerDescriptor> releaseManagementRunners = Lists.newArrayList();
for (SBuildRunnerDescriptor buildRunner : buildType.getBuildRunners()) {
Map<String, String> runnerParameters = buildRunner.getParameters();
if (Boolean.valueOf(runnerParameters.get(RunnerParameterKeys.ENABLE_RELEASE_MANAGEMENT))) {
return buildRunner;
releaseManagementRunners.add(buildRunner);
}
}
return null;
return releaseManagementRunners;
}

private void putErr(@NotNull Map<String, Object> model, String msg) {
setIncludeUrl("releaseManagementErrorTab.jsp");
model.put("errorMessage", msg);
}
}

0 comments on commit a9c7000

Please sign in to comment.