Skip to content

Commit b097318

Browse files
fix: gradle builds (#3250)
Fixes the Gradle builds #2727 I think the first attempt to fix (now reverted) was mostly correct, but in this PR I correct the directory comparison conditional. - #3083 - #3089 Also adds some documentation for handling multi-project builds, which seem to now be the default when initializing a new Gradle app. - https://docs.gradle.org/current/samples/sample_building_java_applications.html#review_the_project_files ## Testing Tested against my own sample project * https://github.com/ramonpetgrave64/my-example-gradle-project/pull/1/files/af3b52a88d6bf053d04f3456a8bb78f6d32c4061 * https://github.com/ramonpetgrave64/my-example-gradle-project/actions/runs/7850051301 Modified the `slsa-framwork/example-package` e2e tests against my own fork. The actual builds and provenance generation succeed, except for the verify stage, which should fail because my fork `https://github.com/ramonpetgrave64/slsa-github-generator/.github/workflows/builder_gradle_slsa3.yml@refs/heads/main` is not a "trusted builder". * ebffcc9 * main...ramonpetgrave64:slsa-github-generator:67a2f7b7efb421e55c3a787161d5968681f3db15 * https://github.com/ramonpetgrave64/example-package/actions/runs/7850413736/job/21425770965 --------- Signed-off-by: Ramon Petgrave <[email protected]> Signed-off-by: Ramon Petgrave <[email protected]>
1 parent a39709d commit b097318

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

actions/gradle/publish/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,7 @@ Closing the staging repository:
280280
Releasing:
281281

282282
![releasing the Gradle artefacts](/actions/gradle/publish/images/gradle-publisher-release-closed-repository.png)
283+
284+
### Multi-Project Builds
285+
286+
See the same guidance in the [build docs](../../../internal/builders/gradle/README.md#multi-project-builds) for consolidating files from multi-project builds.

internal/builders/gradle/README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ workflow the "Gradle builder" from now on.
1919
- [Limitations](#limitations)
2020
- [Generating Provenance](#generating-provenance)
2121
- [Getting Started](#getting-started)
22+
- [Multi-Project Builds](#multi-project-builds)
2223
- [Private Repositories](#private-repositories)
2324
- [Verification](#verification)
2425

@@ -53,6 +54,7 @@ The Gradle builder currently has the following limitations:
5354

5455
1. The project must be buildable by way of `./gradlew build`. If you need the option for flags, profiles or something else to define more granular builds, please open an issue.
5556
2. The project must include a gradle wrapper (`gradlew`). The Gradle builder does not include an installation of gradle.
57+
3. The project's build scripts must place the artifacts into `./build`, relative to the `directory` workflow input. If you are doing [multi-project builds](https://docs.gradle.org/current/userguide/intro_multi_project_builds.html), you may need to follow the [example below](#multi-project-builds)
5658

5759
## Generating Provenance
5860

@@ -83,13 +85,42 @@ jobs:
8385
actions: read
8486
uses: slsa-framework/slsa-github-generator/.github/workflows/[email protected]
8587
with:
86-
artifact-list: ./artifact1.jar,./artifact2.jar
88+
artifact-list: >-
89+
./build/artifact1.jar,
90+
./build/artifact2.jar
8791
```
8892
8993
Now, when you invoke this workflow, the Gradle builder will build both your artifacts and the provenance files for them.
9094
9195
The Gradle builder requires you to specify the artifacts that you wish to attest to. To do so, you add a comma-separated list of paths to the artifacts as shown in the example. The paths are relative from the root of your project directory.
9296
97+
#### Multi-Project Builds
98+
99+
If you are using [multi-project builds](https://docs.gradle.org/current/userguide/intro_multi_project_builds.html), where each of your sub-projects' `src` are in separate subfolders, then you will need to add a task to copy over the artifact files to the root `./build` folder.
100+
101+
See this example to add to your sub-projects' `build.gradle.kts` file.
102+
103+
```kotlin
104+
tasks.register<Copy>("copySubProjectBuild") {
105+
from(layout.buildDirectory)
106+
into("${rootProject.projectDir}/build/${project.name}")
107+
}
108+
109+
tasks.named("build") {
110+
finalizedBy("copySubProjectBuild")
111+
}
112+
```
113+
114+
This, for example, will move `./app1/build/` and `./app2/build/` to `./build/app1/` and `./build/app2/`. You must then alter your input to `artifact-list`.
115+
116+
```yaml
117+
...
118+
artifact-list: >-
119+
./build/app1/libs/app.jar,
120+
./build/app2/libs/app.jar,
121+
...
122+
```
123+
93124
### Private Repositories
94125

95126
The builder records all provenance signatures in the [Rekor](https://github.com/sigstore/rekor) public transparency log. This record includes the repository name. To acknowledge you're aware that your repository name will be public, set the flag `rekor-log-public: true` when calling the builder:

internal/builders/gradle/action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ runs:
124124
env:
125125
PROJECT_ROOT: ${{ steps.run_gradle_builder.outputs.validated_project_root }}
126126
run: |
127-
mv "${PROJECT_ROOT}"/build "${GITHUB_WORKSPACE}"/
127+
# Ensure that directories are not the same before moving them, preventing an error when running action from the root of the repository.
128+
[[ "${PROJECT_ROOT}" -ef "${GITHUB_WORKSPACE}" ]] || mv "${PROJECT_ROOT}"/build "${GITHUB_WORKSPACE}"/
128129
- name: Upload build dir
129130
id: upload-build-dir
130131
uses: slsa-framework/slsa-github-generator/.github/actions/secure-upload-folder@main

internal/builders/maven/action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ runs:
105105
&& mvn package -Drun.hash.jarfile=true
106106
# NOTE: SLSA_OUTPUTS_ARTIFACTS_FILE is a relative path and the project_root may
107107
# not be in GITHUB_WORKSPACE, so we need to move the file.
108-
mv $(dirname "${SLSA_OUTPUTS_ARTIFACTS_FILE}") "${GITHUB_WORKSPACE}/../"
108+
# The following checks if the directories are different before executing the command, fixing an error when SLSA is generated from the root of a repository.
109+
[[ $(dirname "${SLSA_OUTPUTS_ARTIFACTS_FILE}") -ef "${GITHUB_WORKSPACE}/../" ]] || mv $(dirname "${SLSA_OUTPUTS_ARTIFACTS_FILE}") "${GITHUB_WORKSPACE}/../"
109110
mv target "${GITHUB_WORKSPACE}/"
110111
111112
# rng generates a random number to avoid name collision in artifacts

0 commit comments

Comments
 (0)