Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit 7a67f39

Browse files
authored
Add basic support for GitHub Dependency Graph (#782)
2 parents d7761f1 + bc190ca commit 7a67f39

25 files changed

+47933
-29948
lines changed

.eslintrc.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"import/no-namespace": "off",
1313
"i18n-text/no-en": "off",
1414
"no-unused-vars": "off",
15+
"no-shadow": "off",
1516
"sort-imports": "off",
1617
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
1718
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
@@ -30,6 +31,7 @@
3031
"@typescript-eslint/no-misused-new": "error",
3132
"@typescript-eslint/no-namespace": "error",
3233
"@typescript-eslint/no-non-null-assertion": "off",
34+
"@typescript-eslint/no-shadow": "error",
3335
"@typescript-eslint/no-unnecessary-qualifier": "error",
3436
"@typescript-eslint/no-unnecessary-type-assertion": "error",
3537
"@typescript-eslint/no-useless-constructor": "error",

.github/workflows/ci-full-check.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ jobs:
2929
with:
3030
cache-key-prefix: ${{github.run_number}}-
3131

32+
dependency-graph:
33+
uses: ./.github/workflows/integ-test-dependency-graph.yml
34+
with:
35+
cache-key-prefix: ${{github.run_number}}-
36+
3237
execution-with-caching:
3338
uses: ./.github/workflows/integ-test-execution-with-caching.yml
3439
with:

.github/workflows/ci-quick-check.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ jobs:
5050
runner-os: '["ubuntu-latest"]'
5151
download-dist: true
5252

53+
dependency-graph:
54+
needs: build-distribution
55+
uses: ./.github/workflows/integ-test-dependency-graph.yml
56+
with:
57+
runner-os: '["ubuntu-latest"]'
58+
download-dist: true
59+
5360
execution-with-caching:
5461
needs: build-distribution
5562
uses: ./.github/workflows/integ-test-execution-with-caching.yml
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Test execution with caching
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
cache-key-prefix:
7+
type: string
8+
runner-os:
9+
type: string
10+
default: '["ubuntu-latest", "windows-latest", "macos-latest"]'
11+
download-dist:
12+
type: boolean
13+
default: false
14+
15+
env:
16+
DOWNLOAD_DIST: ${{ inputs.download-dist }}
17+
GRADLE_BUILD_ACTION_CACHE_KEY_PREFIX: dependency-graph-${{ inputs.cache-key-prefix }}
18+
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
19+
20+
jobs:
21+
groovy-generate:
22+
strategy:
23+
matrix:
24+
os: ${{fromJSON(inputs.runner-os)}}
25+
runs-on: ${{ matrix.os }}
26+
steps:
27+
- name: Checkout sources
28+
uses: actions/checkout@v3
29+
- name: Download distribution if required
30+
uses: ./.github/actions/download-dist
31+
- name: Setup Gradle for dependency-graph generate
32+
uses: ./
33+
with:
34+
dependency-graph: generate
35+
- name: Run gradle build
36+
run: ./gradlew build
37+
working-directory: .github/workflow-samples/groovy-dsl
38+
39+
kotlin-generate:
40+
strategy:
41+
matrix:
42+
os: ${{fromJSON(inputs.runner-os)}}
43+
runs-on: ${{ matrix.os }}
44+
steps:
45+
- name: Checkout sources
46+
uses: actions/checkout@v3
47+
- name: Download distribution if required
48+
uses: ./.github/actions/download-dist
49+
- name: Setup Gradle for dependency-graph generate
50+
uses: ./
51+
with:
52+
dependency-graph: generate-and-submit
53+
- name: Run gradle build
54+
run: ./gradlew build
55+
working-directory: .github/workflow-samples/kotlin-dsl
56+
57+
submit:
58+
needs: [groovy-generate, kotlin-generate]
59+
runs-on: "ubuntu-latest"
60+
steps:
61+
- name: Checkout sources
62+
uses: actions/checkout@v3
63+
- name: Download distribution if required
64+
uses: ./.github/actions/download-dist
65+
- name: Submit dependency graphs
66+
uses: ./
67+
with:
68+
dependency-graph: download-and-submit

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,120 @@ You can use the `gradle-build-action` on GitHub Enterprise Server, and benefit f
408408
- Easily run your build with different versions of Gradle
409409
- Save/restore of Gradle User Home (requires GHES v3.5+ : GitHub Actions cache was introduced in GHES 3.5)
410410
- Support for GitHub Actions Job Summary (requires GHES 3.6+ : GitHub Actions Job Summary support was introduced in GHES 3.6). In earlier versions of GHES the build-results summary and caching report will be written to the workflow log, as part of the post-action step.
411+
412+
# GitHub Dependency Graph support (Experimental)
413+
414+
The `gradle-build-action` has experimental support for submitting a [GitHub Dependency Graph](https://docs.github.com/en/code-security/supply-chain-security/understanding-your-software-supply-chain/about-the-dependency-graph) snapshot via the [GitHub Dependency Submission API](https://docs.github.com/en/rest/dependency-graph/dependency-submission?apiVersion=2022-11-28).
415+
416+
The dependency graph snapshot is generated via integration with the [GitHub Dependency Graph Gradle Plugin](https://plugins.gradle.org/plugin/org.gradle.github-dependency-graph-gradle-plugin), and saved as a workflow artifact. The generated snapshot files can be submitted either in the same job, or in a subsequent job (in the same or a dependent workflow).
417+
418+
You enable GitHub Dependency Graph support by setting the `dependency-graph` action parameter. Valid values are:
419+
420+
|<div style="width:290px">Option</div> | Behaviour |
421+
| --- |---|
422+
| `disabled` | Do not generate a dependency graph for any build invocations.<p>This is the default. |
423+
| `generate` | Generate a dependency graph snapshot for each build invocation, saving as a workflow artifact. |
424+
| `generate-and-submit` | As per `generate`, but any generated dependency graph snapshots will be submitted at the end of the job. |
425+
| `download-and-submit` | Download any previously saved dependency graph snapshots, submitting them via the Dependency Submission API. This can be useful to collect all snapshots in a matrix of builds and submit them in one step. |
426+
427+
- 'disabled': Do not generate a dependency graph for any build invocations. This is the default.
428+
- 'generate': Generate a dependency graph snapshot for each build invocation, saving as a workflow artifact.
429+
- 'generate-and-submit': As per 'generate', but any generated dependency graph snapshots will be submitted at the end of the job.
430+
- 'download-and-submit': Download any previously saved dependency graph snapshots, submitting them via the Dependency Submission API. This can be useful to collect all snapshots in a matrix of builds and submit them in one step.
431+
432+
Dependency Graph _submission_ (but not generation) requires the `contents: write` permission, which may need to be explicitly enabled in the workflow file.
433+
434+
Example of a simple workflow that generates and submits a dependency graph:
435+
```yaml
436+
name: Submit dependency graph
437+
on:
438+
push:
439+
440+
permissions:
441+
contents: write
442+
443+
jobs:
444+
build:
445+
runs-on: ubuntu-latest
446+
steps:
447+
- uses: actions/checkout@v3
448+
- name: Setup Gradle to generate and submit dependency graphs
449+
uses: gradle/gradle-build-action@dependency-graph
450+
with:
451+
dependency-graph: generate-and-submit
452+
- name: Run a build, generating the dependency graph snapshot which will be submitted
453+
run: ./gradlew build
454+
```
455+
456+
### Running multiple builds in a single Job
457+
458+
GitHub tracks dependency snapshots based on the `job.correlator` value that is embedded in the snapshot. When a newer snapshot for an existing correlator is submitted, the previous snapshot is replaced. Snapshots with different `job.correlator` values are additive to the overall dependency graph for the repository.
459+
460+
The `gradle-build-action` will generate a `job.correlator` value based on the workflow name, job id and matrix values. However, if your job steps contains multiple Gradle invocations, then a unique correlator value must be assigned to each. You assign a correlator by setting the `GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR` environment variable.
461+
462+
```yaml
463+
name: dependency-graph
464+
jobs:
465+
build:
466+
runs-on: ubuntu-latest
467+
steps:
468+
- uses: actions/checkout@v3
469+
- name: Setup Gradle to generate and submit dependency graphs
470+
uses: gradle/gradle-build-action@dependency-graph
471+
with:
472+
dependency-graph: generate-and-submit
473+
- name: Run first build using the default job correlator 'dependency-graph-build'
474+
run: ./gradlew build
475+
- name: Run second build providing a unique job correlator
476+
run: ./gradlew test
477+
env:
478+
GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR: dependency-graph-test
479+
480+
```
481+
482+
### Dependency snapshots generated for pull requests
483+
484+
This `contents: write` permission is not available for any workflow that is triggered by a pull request submitted from a forked repository, since it would permit a malicious pull request to make repository changes.
485+
486+
Because of this restriction, it is not possible to `generate-and-submit` a dependency graph generated for a pull-request that comes from a repository fork. In order to do so, 2 workflows will be required:
487+
1. The first workflow runs directly against the pull request sources and will generate the dependency graph snapshot.
488+
2. The second workflow is triggered on `workflow_run` of the first workflow, and will submit the previously saved dependency snapshots.
489+
490+
Note: when `download-and-submit` is used in a workflow triggered via [workflow_run](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run), the action will download snapshots saved in the triggering workflow.
491+
492+
***Main workflow file***
493+
```yaml
494+
name: run-build-and-generate-dependency-snapshot
495+
496+
jobs:
497+
build:
498+
runs-on: ubuntu-latest
499+
steps:
500+
- uses: actions/checkout@v3
501+
- name: Setup Gradle to generate and submit dependency graphs
502+
uses: gradle/gradle-build-action@v2
503+
with:
504+
dependency-graph: generate # Only generate in this job
505+
- name: Run a build, generating the dependency graph snapshot which will be submitted
506+
run: ./gradlew build
507+
```
508+
509+
***Dependent workflow file***
510+
```yaml
511+
name: submit-dependency-snapshot
512+
513+
on:
514+
workflow_run:
515+
workflows: ['run-build-and-generate-dependency-snapshot']
516+
types: [completed]
517+
518+
jobs:
519+
submit-snapshots:
520+
runs-on: ubuntu-latest
521+
steps:
522+
- name: Retrieve dependency graph artifact and submit
523+
uses: gradle/gradle-build-action@v2
524+
with:
525+
dependency-graph: download-and-submit
526+
```
527+

action.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ inputs:
5858
required: false
5959
default: true
6060

61+
dependency-graph:
62+
description: Specifies if a GitHub dependency snapshot should be generated for each Gradle build, and if so, how. Valid values are 'disabled' (default), 'generate', 'generate-and-submit' and 'download-and-submit'.
63+
required: false
64+
default: 'disabled'
65+
6166
# EXPERIMENTAL & INTERNAL ACTION INPUTS
6267
# The following action properties allow fine-grained tweaking of the action caching behaviour.
6368
# These properties are experimental and not (yet) designed for production use, and may change without notice in a subsequent release of `gradle-build-action`.
@@ -75,6 +80,11 @@ inputs:
7580
required: false
7681
default: false
7782

83+
github-token:
84+
description: The GitHub token used to authenticate when submitting via the Dependency Submission API.
85+
default: ${{ github.token }}
86+
required: false
87+
7888
outputs:
7989
build-scan-url:
8090
description: Link to the build scan if any
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: 'Clear dependency graph for a correlator'
2+
3+
inputs:
4+
job-correlator:
5+
required: true
6+
7+
runs:
8+
using: "composite"
9+
steps:
10+
- name: Set current timestamp as env variable
11+
shell: bash
12+
run: echo "NOW=$(date -Iseconds)" >> $GITHUB_ENV
13+
- name: Submit empty dependency graph
14+
shell: bash
15+
run: |
16+
curl -L \
17+
-X POST \
18+
-H "Accept: application/vnd.github+json" \
19+
-H "Authorization: Bearer ${{ github.token }}" \
20+
-H "X-GitHub-Api-Version: 2022-11-28" \
21+
https://api.github.com/repos/${{ github.repository }}/dependency-graph/snapshots \
22+
-d '{ "version" : 0, "job" : { "id" : "${{ github.run_id }}", "correlator" : "${{ inputs.job-correlator }} " }, "sha" : "${{ github.sha }}", "ref" : "${{ github.ref }}", "detector" : { "name" : "GitHub Dependency Graph Gradle Plugin", "version" : "0.0.3", "url" : "https://github.com/gradle/github-dependency-graph-gradle-plugin" }, "manifests" : {}, "scanned" : "${{ env.NOW }}" }'
23+
- run: echo "::notice ::Cleared dependency graph for job correlator '${{ inputs.job-correlator }}'"
24+
shell: bash

actions/dependency-graph-generate/action.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

actions/dependency-graph-submit/action.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)