Skip to content

Commit

Permalink
Write published-version output even if root project does not apply ma…
Browse files Browse the repository at this point in the history
…ven-publish (#859)
  • Loading branch information
radoslaw-panuszewski authored Nov 13, 2024
1 parent 876ab17 commit 4310bc9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SimpleIntegrationTest extends BaseIntegrationTest {
def "should set released-version github output after release task"(String task,
String rootProjectVersion,
String subprojectVersion,
String output) {
List<String> output) {
given:
def outputFile = File.createTempFile("github-outputs", ".tmp")
environmentVariablesRule.set("GITHUB_ACTIONS", "true")
Expand Down Expand Up @@ -91,23 +91,23 @@ class SimpleIntegrationTest extends BaseIntegrationTest {

then:
def definedEnvVariables = outputFile.getText().lines().collect(toList())
definedEnvVariables.contains(output)
definedEnvVariables == output

cleanup:
environmentVariablesRule.clear("GITHUB_ACTIONS", "GITHUB_OUTPUT")

where:
task | rootProjectVersion | subprojectVersion || output
'release' | "1.0.0" | "1.0.0" || 'released-version=1.0.1'
'release' | "1.0.0" | "2.0.0" || 'released-version={"root-project":"1.0.1","sub-project":"2.0.1"}'
':release' | "1.0.0" | "2.0.0" || 'released-version=1.0.1'
':sub-project:release' | "1.0.0" | "2.0.0" || 'released-version=2.0.1'
'release' | "1.0.0" | "1.0.0" || ['released-version=1.0.1']
'release' | "1.0.0" | "2.0.0" || ['released-version={"root-project":"1.0.1","sub-project":"2.0.1"}']
':release' | "1.0.0" | "2.0.0" || ['released-version=1.0.1']
':sub-project:release' | "1.0.0" | "2.0.0" || ['released-version=2.0.1']
}

def "should set published-version github output after publish task"(String task,
String rootProjectVersion,
String subprojectVersion,
String output) {
List<String> output) {
given:
def outputFile = File.createTempFile("github-outputs", ".tmp")
environmentVariablesRule.set("GITHUB_ACTIONS", "true")
Expand Down Expand Up @@ -145,17 +145,59 @@ class SimpleIntegrationTest extends BaseIntegrationTest {

then:
def definedEnvVariables = outputFile.getText().lines().collect(toList())
definedEnvVariables.contains(output)
definedEnvVariables == output

cleanup:
environmentVariablesRule.clear("GITHUB_ACTIONS", "GITHUB_OUTPUT")

where:
task | rootProjectVersion | subprojectVersion || output
'publish' | "1.0.0" | "1.0.0" || 'published-version=1.0.0'
'publish' | "1.0.0" | "2.0.0" || 'published-version={"root-project":"1.0.0","sub-project":"2.0.0"}'
':publish' | "1.0.0" | "2.0.0" || 'published-version=1.0.0'
':sub-project:publish' | "1.0.0" | "2.0.0" || 'published-version=2.0.0'
'publish' | "1.0.0" | "1.0.0" || ['published-version=1.0.0']
'publish' | "1.0.0" | "2.0.0" || ['published-version={"root-project":"1.0.0","sub-project":"2.0.0"}']
':publish' | "1.0.0" | "2.0.0" || ['published-version=1.0.0']
':sub-project:publish' | "1.0.0" | "2.0.0" || ['published-version=2.0.0']
}

def "should set published-version github output even if root project does not apply maven-publish"() {
given:
def outputFile = File.createTempFile("github-outputs", ".tmp")
environmentVariablesRule.set("GITHUB_ACTIONS", "true")
environmentVariablesRule.set("GITHUB_OUTPUT", outputFile.getAbsolutePath())

vanillaSettingsFile("""
rootProject.name = 'root-project'
include 'sub-project'
"""
)

vanillaBuildFile("""
plugins {
id 'pl.allegro.tech.build.axion-release'
}
allprojects {
version = '1.0.0'
}
"""
)

vanillaSubprojectBuildFile("sub-project", """
plugins {
id 'maven-publish'
}
"""
)

when:
runGradle('publish')

then:
def definedEnvVariables = outputFile.getText().lines().collect(toList())
definedEnvVariables == ['published-version=1.0.0']

cleanup:
environmentVariablesRule.clear("GITHUB_ACTIONS", "GITHUB_OUTPUT")
}

def "should return released version on calling cV on repo with release commit"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ abstract class ReleasePlugin implements Plugin<Project> {
}

private static setGithubOutputsAfterPublishTask(Project project, Provider<GithubService> githubService) {
String projectName = project.name
Provider<String> projectVersion = project.provider { project.version.toString() }

project.plugins.withId('maven-publish') {
project.tasks.named('publish') { task ->
task.usesService(githubService)
task.doLast {
githubService.get().setOutput('published-version', projectName, projectVersion.get())
project.allprojects { Project p ->
String projectName = p.name
Provider<String> projectVersion = p.provider { p.version.toString() }

p.plugins.withId('maven-publish') {
p.tasks.named('publish') { task ->
task.usesService(githubService)
task.doLast {
githubService.get().setOutput('published-version', projectName, projectVersion.get())
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void close() {
writeOutput(name, singleValue);
} else {
String jsonValue = new JsonBuilder(valuePerProject).toString();
logger.warn("Multiple values provided for the '{}' GitHub output, it will be formatted as JSON: {}", name, jsonValue);
logger.warn("Multiple values provided for the '{}' GitHub output, it will be formatted as JSON", name);
writeOutput(name, jsonValue);
}
});
Expand All @@ -58,6 +58,7 @@ private static void writeOutput(String name, String value) {
String.format("%s=%s\n", name, value).getBytes(),
StandardOpenOption.APPEND
);
logger.lifecycle("Written GitHub output: {}={}", name, value);
} catch (IOException e) {
logger.warn("Unable to the set '{}' GitHub output, cause: {}", name, e.getMessage());
}
Expand Down

0 comments on commit 4310bc9

Please sign in to comment.