Skip to content

Commit 4310bc9

Browse files
Write published-version output even if root project does not apply maven-publish (#859)
1 parent 876ab17 commit 4310bc9

File tree

3 files changed

+66
-21
lines changed

3 files changed

+66
-21
lines changed

src/integration/groovy/pl/allegro/tech/build/axion/release/SimpleIntegrationTest.groovy

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SimpleIntegrationTest extends BaseIntegrationTest {
4343
def "should set released-version github output after release task"(String task,
4444
String rootProjectVersion,
4545
String subprojectVersion,
46-
String output) {
46+
List<String> output) {
4747
given:
4848
def outputFile = File.createTempFile("github-outputs", ".tmp")
4949
environmentVariablesRule.set("GITHUB_ACTIONS", "true")
@@ -91,23 +91,23 @@ class SimpleIntegrationTest extends BaseIntegrationTest {
9191

9292
then:
9393
def definedEnvVariables = outputFile.getText().lines().collect(toList())
94-
definedEnvVariables.contains(output)
94+
definedEnvVariables == output
9595

9696
cleanup:
9797
environmentVariablesRule.clear("GITHUB_ACTIONS", "GITHUB_OUTPUT")
9898

9999
where:
100100
task | rootProjectVersion | subprojectVersion || output
101-
'release' | "1.0.0" | "1.0.0" || 'released-version=1.0.1'
102-
'release' | "1.0.0" | "2.0.0" || 'released-version={"root-project":"1.0.1","sub-project":"2.0.1"}'
103-
':release' | "1.0.0" | "2.0.0" || 'released-version=1.0.1'
104-
':sub-project:release' | "1.0.0" | "2.0.0" || 'released-version=2.0.1'
101+
'release' | "1.0.0" | "1.0.0" || ['released-version=1.0.1']
102+
'release' | "1.0.0" | "2.0.0" || ['released-version={"root-project":"1.0.1","sub-project":"2.0.1"}']
103+
':release' | "1.0.0" | "2.0.0" || ['released-version=1.0.1']
104+
':sub-project:release' | "1.0.0" | "2.0.0" || ['released-version=2.0.1']
105105
}
106106

107107
def "should set published-version github output after publish task"(String task,
108108
String rootProjectVersion,
109109
String subprojectVersion,
110-
String output) {
110+
List<String> output) {
111111
given:
112112
def outputFile = File.createTempFile("github-outputs", ".tmp")
113113
environmentVariablesRule.set("GITHUB_ACTIONS", "true")
@@ -145,17 +145,59 @@ class SimpleIntegrationTest extends BaseIntegrationTest {
145145

146146
then:
147147
def definedEnvVariables = outputFile.getText().lines().collect(toList())
148-
definedEnvVariables.contains(output)
148+
definedEnvVariables == output
149149

150150
cleanup:
151151
environmentVariablesRule.clear("GITHUB_ACTIONS", "GITHUB_OUTPUT")
152152

153153
where:
154154
task | rootProjectVersion | subprojectVersion || output
155-
'publish' | "1.0.0" | "1.0.0" || 'published-version=1.0.0'
156-
'publish' | "1.0.0" | "2.0.0" || 'published-version={"root-project":"1.0.0","sub-project":"2.0.0"}'
157-
':publish' | "1.0.0" | "2.0.0" || 'published-version=1.0.0'
158-
':sub-project:publish' | "1.0.0" | "2.0.0" || 'published-version=2.0.0'
155+
'publish' | "1.0.0" | "1.0.0" || ['published-version=1.0.0']
156+
'publish' | "1.0.0" | "2.0.0" || ['published-version={"root-project":"1.0.0","sub-project":"2.0.0"}']
157+
':publish' | "1.0.0" | "2.0.0" || ['published-version=1.0.0']
158+
':sub-project:publish' | "1.0.0" | "2.0.0" || ['published-version=2.0.0']
159+
}
160+
161+
def "should set published-version github output even if root project does not apply maven-publish"() {
162+
given:
163+
def outputFile = File.createTempFile("github-outputs", ".tmp")
164+
environmentVariablesRule.set("GITHUB_ACTIONS", "true")
165+
environmentVariablesRule.set("GITHUB_OUTPUT", outputFile.getAbsolutePath())
166+
167+
vanillaSettingsFile("""
168+
rootProject.name = 'root-project'
169+
170+
include 'sub-project'
171+
"""
172+
)
173+
174+
vanillaBuildFile("""
175+
plugins {
176+
id 'pl.allegro.tech.build.axion-release'
177+
}
178+
179+
allprojects {
180+
version = '1.0.0'
181+
}
182+
"""
183+
)
184+
185+
vanillaSubprojectBuildFile("sub-project", """
186+
plugins {
187+
id 'maven-publish'
188+
}
189+
"""
190+
)
191+
192+
when:
193+
runGradle('publish')
194+
195+
then:
196+
def definedEnvVariables = outputFile.getText().lines().collect(toList())
197+
definedEnvVariables == ['published-version=1.0.0']
198+
199+
cleanup:
200+
environmentVariablesRule.clear("GITHUB_ACTIONS", "GITHUB_OUTPUT")
159201
}
160202

161203
def "should return released version on calling cV on repo with release commit"() {

src/main/groovy/pl/allegro/tech/build/axion/release/ReleasePlugin.groovy

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ abstract class ReleasePlugin implements Plugin<Project> {
7878
}
7979

8080
private static setGithubOutputsAfterPublishTask(Project project, Provider<GithubService> githubService) {
81-
String projectName = project.name
82-
Provider<String> projectVersion = project.provider { project.version.toString() }
83-
84-
project.plugins.withId('maven-publish') {
85-
project.tasks.named('publish') { task ->
86-
task.usesService(githubService)
87-
task.doLast {
88-
githubService.get().setOutput('published-version', projectName, projectVersion.get())
81+
project.allprojects { Project p ->
82+
String projectName = p.name
83+
Provider<String> projectVersion = p.provider { p.version.toString() }
84+
85+
p.plugins.withId('maven-publish') {
86+
p.tasks.named('publish') { task ->
87+
task.usesService(githubService)
88+
task.doLast {
89+
githubService.get().setOutput('published-version', projectName, projectVersion.get())
90+
}
8991
}
9092
}
9193
}

src/main/java/pl/allegro/tech/build/axion/release/infrastructure/github/GithubService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void close() {
4444
writeOutput(name, singleValue);
4545
} else {
4646
String jsonValue = new JsonBuilder(valuePerProject).toString();
47-
logger.warn("Multiple values provided for the '{}' GitHub output, it will be formatted as JSON: {}", name, jsonValue);
47+
logger.warn("Multiple values provided for the '{}' GitHub output, it will be formatted as JSON", name);
4848
writeOutput(name, jsonValue);
4949
}
5050
});
@@ -58,6 +58,7 @@ private static void writeOutput(String name, String value) {
5858
String.format("%s=%s\n", name, value).getBytes(),
5959
StandardOpenOption.APPEND
6060
);
61+
logger.lifecycle("Written GitHub output: {}={}", name, value);
6162
} catch (IOException e) {
6263
logger.warn("Unable to the set '{}' GitHub output, cause: {}", name, e.getMessage());
6364
}

0 commit comments

Comments
 (0)