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

Write published-version output even if root project does not apply maven-publish #859

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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 @@ -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