Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project version updated after release task #807

Open
wants to merge 4 commits into
base: gradle76
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ class BaseIntegrationTest extends RepositoryBasedTest {

BuildResult runGradle(String... arguments) {
def args = []
args.addAll(arguments)
args.add("--stacktrace")
args.add("--configuration-cache")
args.add("--warning-mode=fail")
args.addAll(arguments)
runGradleWithoutConfigurationCache(args)
}

BuildResult runGradleWithoutConfigurationCache(List<String> args) {
try {
return gradle().withArguments(args).build()
}
catch (e) {
throw new RuntimeException("Gradle build failed", e)
}

finally {
def ccDir = new File(temporaryFolder, "build/reports/configuration-cache")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,79 @@ class SimpleIntegrationTest extends BaseIntegrationTest {
cleanup:
environmentVariablesRule.clear("GITHUB_ACTIONS", "GITHUB_OUTPUT")
}

def "should not update project.version after release when updateProjectVersionAfterRelease option is not set"() {
given:
buildFile("""
task assemble {
inputs.property("version", project.version)
doLast {
println("Assembling project: " + version)
}
}
""")

def result = runGradleWithoutConfigurationCache(
['currentVersion', 'release', 'assemble', '-Prelease.localOnly', '-Prelease.disableChecks']
)

expect:
result.output.contains('Project version: 0.1.0-SNAPSHOT')
result.output.contains('Creating tag: v0.1.0')
result.output.contains('Assembling project: 0.1.0-SNAPSHOT')
result.task(":currentVersion").outcome == TaskOutcome.SUCCESS
result.task(":release").outcome == TaskOutcome.SUCCESS
result.task(":assemble").outcome == TaskOutcome.SUCCESS
}

def "should update project.version after release when updateProjectVersionAfterRelease option is set"() {
given:
buildFile("""
scmVersion {
updateProjectVersionAfterRelease = true
}

task assemble {
inputs.property("version", project.version)
doLast {
println("Assembling project: " + version)
}
}
""")

def result = runGradleWithoutConfigurationCache(
['currentVersion', 'release', 'assemble', '-Prelease.localOnly', '-Prelease.disableChecks']
)

expect:
result.output.contains('Project version: 0.1.0-SNAPSHOT')
result.output.contains('Creating tag: v0.1.0')
result.output.contains('Project version will be updated after release.')
result.output.contains('Assembling project: 0.1.0')
result.task(":currentVersion").outcome == TaskOutcome.SUCCESS
result.task(":release").outcome == TaskOutcome.SUCCESS
result.task(":assemble").outcome == TaskOutcome.SUCCESS
}

def "should not fail build when using updateProjectVersionAfterRelease and configuration cache is enabled"() {
given:
buildFile("""
scmVersion {
updateProjectVersionAfterRelease = true
}

task assemble {
inputs.property("version", project.version)
doLast {
println("Assembling project: " + version)
}
}
""")

when:
def result = runGradle('release', '--stacktrace', '-Prelease.localOnly', '-Prelease.disableChecks')

then:
result.task(":release").outcome == TaskOutcome.SUCCESS
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pl.allegro.tech.build.axion.release

class ConfigurationCacheConfiguration {
boolean updateProjectVersionAfterRelease
boolean configurationCacheEnabled
public Closure<String> updateProjectVersion

ConfigurationCacheConfiguration(boolean updateProjectVersionAfterRelease, Closure<String> updateProjectVersion) {
this.updateProjectVersionAfterRelease = updateProjectVersionAfterRelease
this.updateProjectVersion = updateProjectVersion
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pl.allegro.tech.build.axion.release


import org.gradle.api.tasks.TaskAction
import pl.allegro.tech.build.axion.release.domain.Releaser
import pl.allegro.tech.build.axion.release.domain.scm.ScmService
import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext

abstract class CreateReleaseTask extends BaseAxionTask {
Expand All @@ -10,11 +12,16 @@ abstract class CreateReleaseTask extends BaseAxionTask {
void release() {
VersionResolutionContext context = resolutionContext()
Releaser releaser = context.releaser()
ScmService scmService = context.scmService()
ReleaseBranchesConfiguration releaseBranchesConfiguration = new ReleaseBranchesConfiguration(
context.scmService().isReleaseOnlyOnReleaseBranches(),
scmService.isReleaseOnlyOnReleaseBranches(),
context.repository().currentPosition().getBranch(),
context.scmService().getReleaseBranchNames()
scmService.getReleaseBranchNames()
)
ConfigurationCacheConfiguration configurationCacheConfiguration = new ConfigurationCacheConfiguration(
scmService.isUpdateProjectVersionAfterRelease(),
(version) -> project.setVersion(version)
)
releaser.release(context.rules(), releaseBranchesConfiguration)
releaser.release(context.rules(), releaseBranchesConfiguration, configurationCacheConfiguration)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package pl.allegro.tech.build.axion.release


import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.util.GradleVersion
import pl.allegro.tech.build.axion.release.domain.SnapshotDependenciesChecker
import pl.allegro.tech.build.axion.release.domain.VersionConfig
import pl.allegro.tech.build.axion.release.util.FileLoader
Expand All @@ -27,9 +29,9 @@ abstract class ReleasePlugin implements Plugin<Project> {
})

project.tasks.register(VERIFY_RELEASE_TASK, VerifyReleaseTask) {
group = 'Release'
description = 'Verifies code is ready for release.'
snapshotDependencies.addAll(
it.group = 'Release'
it.description = 'Verifies code is ready for release.'
it.snapshotDependencies.addAll(
project.provider({
SnapshotDependenciesChecker checker = new SnapshotDependenciesChecker();
return checker.snapshotVersions(project)
Expand All @@ -38,15 +40,41 @@ abstract class ReleasePlugin implements Plugin<Project> {
}

project.tasks.register(RELEASE_TASK, ReleaseTask) {
group = 'Release'
description = 'Performs release - creates tag and pushes it to remote.'
dependsOn(VERIFY_RELEASE_TASK)
it.group = 'Release'
it.description = 'Performs release - creates tag and pushes it to remote.'
it.dependsOn(VERIFY_RELEASE_TASK)
if (versionConfig.updateProjectVersionAfterRelease.get() && !project.tasks.matching { it.name == "assemble" }.isEmpty()) {
if (GradleVersion.current() >= GradleVersion.version("7.4")) {
it.notCompatibleWithConfigurationCache(
"Configuration cache is enabled and `scmVersion.updateProjectVersionAfterRelease` " +
"is set to `true`. This is not supported. Set `scmVersion.updateProjectVersionAfterRelease` to `false` " +
"and remember to run release task separately."
)
}
it.doLast {
logger.quiet("Project version will be updated after release.")
}
it.finalizedBy(project.tasks.named("assemble"))
}
}

project.tasks.register(CREATE_RELEASE_TASK, CreateReleaseTask) {
group = 'Release'
description = 'Performs first stage of release - creates tag.'
dependsOn(VERIFY_RELEASE_TASK)
it.group = 'Release'
it.description = 'Performs first stage of release - creates tag.'
it.dependsOn(VERIFY_RELEASE_TASK)
if (versionConfig.updateProjectVersionAfterRelease.get() && !project.tasks.matching { it.name == "assemble" }.isEmpty()) {
if (GradleVersion.current() >= GradleVersion.version("7.4")) {
it.notCompatibleWithConfigurationCache(
"Configuration cache is enabled and `scmVersion.updateProjectVersionAfterRelease` " +
"is set to `true`. This is not supported. Set `scmVersion.updateProjectVersionAfterRelease` to `false` " +
"and remember to run release task separately."
)
}
it.doLast {
logger.quiet("Project version will be updated after release.")
}
it.finalizedBy(project.tasks.named("assemble"))
}
}

project.tasks.register(PUSH_RELEASE_TASK, PushReleaseTask) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package pl.allegro.tech.build.axion.release


import org.gradle.api.tasks.TaskAction
import pl.allegro.tech.build.axion.release.domain.Releaser
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResult
import pl.allegro.tech.build.axion.release.domain.scm.ScmPushResultOutcome
import pl.allegro.tech.build.axion.release.domain.scm.ScmService
import pl.allegro.tech.build.axion.release.infrastructure.di.VersionResolutionContext

import java.nio.file.Files
Expand All @@ -16,13 +18,24 @@ abstract class ReleaseTask extends BaseAxionTask {
void release() {
VersionResolutionContext context = resolutionContext()
Releaser releaser = context.releaser()
ScmService service = context.scmService()

ReleaseBranchesConfiguration releaseBranchesConfiguration = new ReleaseBranchesConfiguration(
context.scmService().isReleaseOnlyOnReleaseBranches(),
service.isReleaseOnlyOnReleaseBranches(),
context.repository().currentPosition().getBranch(),
context.scmService().getReleaseBranchNames()
service.getReleaseBranchNames()
)

ScmPushResult result = releaser.releaseAndPush(context.rules(), releaseBranchesConfiguration)
ConfigurationCacheConfiguration configurationCacheConfiguration = new ConfigurationCacheConfiguration(
service.isUpdateProjectVersionAfterRelease(),
(version) -> project.setVersion(version)
)

ScmPushResult result = releaser.releaseAndPush(
context.rules(),
releaseBranchesConfiguration,
configurationCacheConfiguration
)

if (result.outcome === ScmPushResultOutcome.FAILED) {
def status = result.failureStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class VersionConfig extends BaseExtension {
private static final String IGNORE_UNCOMMITTED_CHANGES_PROPERTY = 'release.ignoreUncommittedChanges'
private static final String FORCE_SNAPSHOT_PROPERTY = 'release.forceSnapshot'
private static final String USE_HIGHEST_VERSION_PROPERTY = 'release.useHighestVersion'
private static final String UPDATE_PROJECT_VERSION_AFTER_RELEASE_PROPERTY = 'release.updateProjectVersionAfterRelease'
private static final String LOCAL_ONLY = "release.localOnly"
private static final String FORCE_VERSION_PROPERTY = 'release.version'
private static final String DEPRECATED_FORCE_VERSION_PROPERTY = 'release.forceVersion'
Expand All @@ -43,6 +44,7 @@ abstract class VersionConfig extends BaseExtension {
getLocalOnly().convention(false)
getIgnoreUncommittedChanges().convention(true)
getUseHighestVersion().convention(false)
getUpdateProjectVersionAfterRelease().convention(false)
getUnshallowRepoOnCI().convention(false)
getIgnoreGlobalGitConfig().convention(false)
getReleaseBranchNames().convention(gradlePropertyAsSet(RELEASE_BRANCH_NAMES_PROPERTY).orElse(['master', 'main'] as Set))
Expand Down Expand Up @@ -127,6 +129,9 @@ abstract class VersionConfig extends BaseExtension {
@Internal
abstract Property<Boolean> getUseHighestVersion();

@Internal
abstract Property<Boolean> getUpdateProjectVersionAfterRelease();

Provider<Boolean> ignoreUncommittedChanges() {
gradlePropertyPresent(IGNORE_UNCOMMITTED_CHANGES_PROPERTY)
.orElse(ignoreUncommittedChanges)
Expand All @@ -140,6 +145,10 @@ abstract class VersionConfig extends BaseExtension {
gradlePropertyPresent(USE_HIGHEST_VERSION_PROPERTY).orElse(useHighestVersion)
}

Provider<Boolean> updateProjectVersionAfterRelease() {
gradlePropertyPresent(UPDATE_PROJECT_VERSION_AFTER_RELEASE_PROPERTY).orElse(updateProjectVersionAfterRelease)
}

Provider<Boolean> localOnly() {
gradlePropertyPresent(LOCAL_ONLY).orElse(localOnly)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ScmPropertiesFactory {
config.getUnshallowRepoOnCI().get(),
config.getReleaseBranchNames().get(),
config.getReleaseOnlyOnReleaseBranches().get(),
config.getIgnoreGlobalGitConfig().get()
config.getIgnoreGlobalGitConfig().get(),
config.getUpdateProjectVersionAfterRelease().get(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.zafarkhaja.semver.Version;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import pl.allegro.tech.build.axion.release.ConfigurationCacheConfiguration;
import pl.allegro.tech.build.axion.release.ReleaseBranchesConfiguration;
import pl.allegro.tech.build.axion.release.domain.hooks.ReleaseHooksRunner;
import pl.allegro.tech.build.axion.release.domain.properties.Properties;
Expand All @@ -25,7 +26,11 @@ public Releaser(VersionService versionService, ScmService repository, ReleaseHoo
this.hooksRunner = hooksRunner;
}

public Optional<String> release(Properties properties, ReleaseBranchesConfiguration releaseBranchesConfiguration) {
public Optional<String> release(
Properties properties,
ReleaseBranchesConfiguration releaseBranchesConfiguration,
ConfigurationCacheConfiguration configurationCacheConfiguration
) {
if (releaseBranchesConfiguration.shouldRelease()) {
String message = String.format(
"Release step skipped since 'releaseOnlyOnReleaseBranches' option is set, and '%s' was not in 'releaseBranchNames' list [%s]",
Expand All @@ -42,6 +47,10 @@ public Optional<String> release(Properties properties, ReleaseBranchesConfigurat

Version version = versionContext.getVersion();

if (configurationCacheConfiguration.isUpdateProjectVersionAfterRelease()) {
configurationCacheConfiguration.updateProjectVersion.call(version.toString());
}

if (versionContext.isSnapshot()) {
String tagName = properties.getTag().getSerialize().apply(properties.getTag(), version.toString());

Expand All @@ -58,8 +67,12 @@ public Optional<String> release(Properties properties, ReleaseBranchesConfigurat
}
}

public ScmPushResult releaseAndPush(Properties rules, ReleaseBranchesConfiguration releaseBranchesConfiguration) {
Optional<String> releasedTagName = release(rules, releaseBranchesConfiguration);
public ScmPushResult releaseAndPush(
Properties rules,
ReleaseBranchesConfiguration releaseBranchesConfiguration,
ConfigurationCacheConfiguration configurationCacheEnabled
) {
Optional<String> releasedTagName = release(rules, releaseBranchesConfiguration, configurationCacheEnabled);

if (releasedTagName.isEmpty()) {
return new ScmPushResult(ScmPushResultOutcome.SKIPPED, Optional.empty(), Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ScmProperties {
private final Set<String> releaseBranchNames;
private final boolean releaseOnlyOnReleaseBranches;
private final boolean ignoreGlobalGitConfig;
private final boolean updateProjectVersionAfterRelease;

public ScmProperties(
String type,
Expand All @@ -35,7 +36,8 @@ public ScmProperties(
Boolean unshallowRepoOnCI,
Set<String> releaseBranchNames,
boolean releaseOnlyOnReleaseBranches,
boolean ignoreGlobalGitConfig
boolean ignoreGlobalGitConfig,
boolean updateProjectVersionAfterRelease
) {
this.type = type;
this.directory = directory;
Expand All @@ -51,6 +53,7 @@ public ScmProperties(
this.releaseBranchNames = releaseBranchNames;
this.releaseOnlyOnReleaseBranches = releaseOnlyOnReleaseBranches;
this.ignoreGlobalGitConfig = ignoreGlobalGitConfig;
this.updateProjectVersionAfterRelease = updateProjectVersionAfterRelease;
}

public ScmPushOptions pushOptions() {
Expand Down Expand Up @@ -112,4 +115,8 @@ public boolean isReleaseOnlyOnReleaseBranches() {
public boolean isIgnoreGlobalGitConfig() {
return ignoreGlobalGitConfig;
}

public boolean isUpdateProjectVersionAfterRelease() {
return updateProjectVersionAfterRelease;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public Set<String> getReleaseBranchNames() {
public boolean isReleaseOnlyOnReleaseBranches(){
return scmProperties.isReleaseOnlyOnReleaseBranches();
}

public boolean isUpdateProjectVersionAfterRelease() {
return scmProperties.isUpdateProjectVersionAfterRelease();
}
}
Loading