Skip to content

Commit

Permalink
Add build scan url as GitHub workflow step output (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jprinet authored Jul 19, 2024
1 parent 501ac18 commit 23b7c25
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
41 changes: 24 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,34 @@ jobs:
run: ./mvnw clean package
[...]
```
_Note:_<br>
When authenticated access is required to publish a Build Scan®, it is recommended to provide as input `develocity-access-key` to the `maven-setup` step. This triggers a request for a [short-lived access token](https://docs.gradle.com/develocity/api-manual/#short_lived_access_tokens) instead of relying on the `DEVELOCITY_ACCESS_KEY` environment variable.

> [!NOTE]
> When authenticated access is required to publish a Build Scan®, it is recommended to provide as input `develocity-access-key` to the `maven-setup` step. This triggers a request for a [short-lived access token](https://docs.gradle.com/develocity/api-manual/#short_lived_access_tokens) instead of relying on the `DEVELOCITY_ACCESS_KEY` environment variable.
### Implementation details

The action enables two features:
- Display a summary of the Maven builds as a GitHub workflow summary or as a pull-request comment
- Capture unpublished Build Scan® data as a workflow artifact per job with prefix `build-scan-data-maven`, which can then be published in a dependent workflow.

#### Summary

By default, a summary will be added to the GitHub workflow calling the action (can be skipped with `add-job-summary` is set to `false`):
#### Workflow Summary
By default, a summary will be added to the GitHub workflow calling the action (can be skipped if `add-job-summary` is set to `false`):

![workflow](./doc/summary-workflow.png)

#### Pull-request Comment

By default, a comment will be added to the pull-request with the summary (can be skipped if `add-pr-comment` is set to `false`):

![comment](./doc/summary-comment.png)

_Note:_<br>
- The job name is computed by appending `github.job` to the current matrix value (if present) but can be overridden with `job-name` input.
- The pull-request comment is overwriting the previous summary comment if present, this means that if several jobs have a setup-maven step,
> [!NOTE]
> - The job name is computed by appending `github.job` to the current matrix value (if present) but can be overridden with `job-name` input.
> - The pull-request comment is overwriting the previous summary comment if present, this means that if several jobs have a setup-maven step,
only the last will have its summary commented in the PR. It is recommended to disable the pull-request summary in this case (`add-pr-comment: false`).

#### Raw Summary data

Additionally, the summary details will be accessible in `$RUNNER_TEMP/build-scan-data-maven/build-metadata.json` with the format below:

```json
Expand Down Expand Up @@ -94,12 +98,15 @@ The _capture_ can be _enabled_/_disabled_ separately:
The process is handled by a [Maven extension](https://maven.apache.org/guides/mini/guide-using-extensions.html) `maven-build-scan-capture-extension.jar` which is running during each Maven invocation.
The extension is automatically registered by configuring the environment `MAVEN_OPTS=-Dmaven.ext.classpath=<PATH_TO_EXTENSION>`.

_Note:_<br>
If `MAVEN_OPTS` environment variable is set in the step invoking the `mvn` command, the extension won't be registered.
> [!NOTE]
> If `MAVEN_OPTS` environment variable is set in the step invoking the `mvn` command, the extension won't be registered.
Make sure to use `MAVEN_OPTS: ${{ env.MAVEN_OPTS }} <EXTRA_PARAMETERS>` construction to append the extra parameters and have the extension registered.
The captured files are added as workflow artifact (one artifact per job).

When a Build Scan link is captured, it is added as output to the step invoking Maven.
The output name is `build-scan-url` and can be used in subsequent steps of the workflow.

**Event Triggers**:

- `pull_request`: To capture unpublished Build Scan®
Expand Down Expand Up @@ -130,8 +137,8 @@ The following permissions are required for this action to operate:
When submitting a pull request from a forked GitHub repository, a GitHub workflow that validates the change cannot publish a Build Scan®
as workflows from forked repositories do not have access to secrets. An access token is required to publish a Build Scan®.

_Note:_<br>
The `Approve and Run` manual step documented [here](https://docs.github.com/en/actions/managing-workflow-runs/approving-workflow-runs-from-public-forks) must be enabled on the GitHub repository configuration to meet legal requirements (this is the default configuration).
> [!NOTE]
> The `Approve and Run` manual step documented [here](https://docs.github.com/en/actions/managing-workflow-runs/approving-workflow-runs-from-public-forks) must be enabled on the GitHub repository configuration to meet legal requirements (this is the default configuration).
### Maven workflow

Expand Down Expand Up @@ -180,11 +187,11 @@ jobs:
develocity-access-key: ${{ secrets.<DEVELOCITY_ACCESS_KEY> }}
```
_Note:_<br>
Some parameters need to be adjusted here:
- The workflow name (here `PR Build`) triggered when a pull-request is submitted
- The Develocity URL (here `https://<MY_DEVELOCITY_URL>`)
- The secret name holding the Develocity access key (here `<DEVELOCITY_ACCESS_KEY>`)
> [!NOTE]
> Some parameters need to be adjusted here:
> - The workflow name (here `PR Build`) triggered when a pull-request is submitted
> - The Develocity URL (here `https://<MY_DEVELOCITY_URL>`)
> - The secret name holding the Develocity access key (here `<DEVELOCITY_ACCESS_KEY>`)

#### Implementation details

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;
Expand Down Expand Up @@ -84,12 +87,25 @@ private String getMavenVersion() {
void captureBuildScanLink(PublishedBuildScan publishedBuildScan) {
if(configuration.isCaptureBuildScanLinks(buildState.isBuildFailure())) {
LOGGER.info("Capturing build scan link");
buildState.setBuildScanLink(publishedBuildScan.getBuildScanUri().toString());
String buildScanLink = publishedBuildScan.getBuildScanUri().toString();
buildState.setBuildScanLink(buildScanLink);
addToGitHubOutput("build-scan-url", buildScanLink);
} else {
LOGGER.debug("Build scan link capture disabled");
}
}

private void addToGitHubOutput(String key, String value) {
try {
String githubOutput = System.getenv("GITHUB_OUTPUT");
if (githubOutput != null) {
Files.writeString(Paths.get(githubOutput), key + "=" + value + "\n", StandardCharsets.UTF_8, StandardOpenOption.APPEND);
}
} catch (IOException e) {
LOGGER.info("Unable to add " + key + " to GitHub output " + e.getMessage());
}
}

void captureBuildScanMetadata() {
if(configuration.isBuildScanRepublication()) {
appendBuildScanLinkToMetadataFile();
Expand Down

0 comments on commit 23b7c25

Please sign in to comment.