Skip to content

Commit

Permalink
⚡ Allow setting cache keys on GitHub and CircleCI (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiulpin committed Apr 20, 2023
1 parent 173f7c3 commit 029c83d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 43 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Follow these steps to establish a baseline for your project:
1. Run Qodana [locally](https://www.jetbrains.com/help/qodana/getting-started.html#Analyze+a+project+locally) over your project:

```shell
cd <source-directory>
cd project
qodana scan --show-report
```

Expand Down Expand Up @@ -191,18 +191,19 @@ with:
cache-default-branch-only: true
```

| Name | Description | Default Value |
|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------|
| `args` | Additional [Qodana CLI `scan` command](https://github.com/jetbrains/qodana-cli#scan) arguments, split the arguments with commas (`,`), for example `-i,frontend,--print-problems`. Optional. | - |
| `results-dir` | Directory to store the analysis results. Optional. | `${{ runner.temp }}/qodana/results` |
| `upload-result` | Upload Qodana results as an artifact to the job. Optional. | `true` |
| `artifact-name` | Specify Qodana results artifact name, used for results uploading. Optional. | `qodana-report` |
| `cache-dir` | Directory to store Qodana cache. Optional. | `${{ runner.temp }}/qodana/caches` |
| `use-caches` | Utilize [GitHub caches](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy) for Qodana runs. Optional. | `true` |
| `cache-default-branch-only` | Upload cache for the default branch only. Optional. | `false` |
| `additional-cache-hash` | Allows customizing the generated cache hash. Optional. | `${{ github.sha }}` |
| `use-annotations` | Use annotation to mark the results in the GitHub user interface. Optional. | `true` |
| `pr-mode` | Analyze only changed files in a pull request. Optional. | `true` |
| Name | Description | Default Value |
|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------|
| `args` | Additional [Qodana CLI `scan` command](https://github.com/jetbrains/qodana-cli#scan) arguments, split the arguments with commas (`,`), for example `-i,frontend,--print-problems`. Optional. | - |
| `results-dir` | Directory to store the analysis results. Optional. | `${{ runner.temp }}/qodana/results` |
| `upload-result` | Upload Qodana results as an artifact to the job. Optional. | `true` |
| `artifact-name` | Specify Qodana results artifact name, used for results uploading. Optional. | `qodana-report` |
| `cache-dir` | Directory to store Qodana cache. Optional. | `${{ runner.temp }}/qodana/caches` |
| `use-caches` | Utilize [GitHub caches](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy) for Qodana runs. Optional. | `true` |
| `primary-cache-key` | Set [the primary cache key](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key). Optional. | `qodana-2023.1-${{ github.ref }}-${{ github.sha }}` |
| `additional-cache-key` | Set [the additional cache key](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key). Optional. | `qodana-2023.1-${{ github.ref }}` |
| `cache-default-branch-only` | Upload cache for the default branch only. Optional. | `false` |
| `use-annotations` | Use annotation to mark the results in the GitHub user interface. Optional. | `true` |
| `pr-mode` | Analyze only changed files in a pull request. Optional. | `true` |

[gh:qodana]: https://github.com/JetBrains/qodana-action/actions/workflows/code_scanning.yml
[youtrack]: https://youtrack.jetbrains.com/issues/QD
Expand Down
9 changes: 9 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ inputs:
description: 'Automatically restore and save Qodana caches using GitHub caches'
required: false
default: "true"
primary-cache-key:
description: 'Set the primary cache key'
required: false
default: "qodana-2023.1-${{ github.ref }}-${{ github.sha }}"
additional-cache-key:
description: 'Set the additional cache key'
required: false
default: "qodana-2023.1-${{ github.ref }}"
additional-cache-hash:
description: 'Pass additional cache hash extension'
required: false
default: "${{ github.sha }}"
deprecationMessage: 'Use `primary-cache-key` and `additional-cache-key` to configure cache keys'
cache-default-branch-only:
description: 'Upload cache for the default branch only'
required: false
Expand Down
3 changes: 2 additions & 1 deletion common/qodana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export interface Inputs {
args: string[]
resultsDir: string
cacheDir: string
additionalCacheHash: string
primaryCacheKey: string
additionalCacheKey: string
cacheDefaultBranchOnly: boolean
uploadResult: boolean
artifactName: string
Expand Down
3 changes: 2 additions & 1 deletion scan/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function inputsDefaultFixture(): Inputs {
args: ['--baseline', 'qodana.sarif.json'],
resultsDir: '${{ runner.temp }}/qodana-results',
cacheDir: '${{ runner.temp }}/qodana-caches',
additionalCacheHash: '',
additionalCacheKey: '',
primaryCacheKey: '',
cacheDefaultBranchOnly: false,
uploadResult: true,
artifactName: 'Qodana report',
Expand Down
15 changes: 7 additions & 8 deletions scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63097,7 +63097,8 @@ var require_utils7 = __commonJS({
args: core2.getInput("args").split(",").map((arg) => arg.trim()),
resultsDir: core2.getInput("results-dir"),
cacheDir: core2.getInput("cache-dir"),
additionalCacheHash: core2.getInput("additional-cache-hash"),
primaryCacheKey: core2.getInput("primary-cache-key"),
additionalCacheKey: core2.getInput("additional-cache-key") || core2.getInput("additional-cache-hash"),
cacheDefaultBranchOnly: core2.getBooleanInput("cache-default-branch-only"),
uploadResult: core2.getBooleanInput("upload-result"),
artifactName: core2.getInput("artifact-name"),
Expand Down Expand Up @@ -63171,7 +63172,7 @@ var require_utils7 = __commonJS({
}
__name(uploadReport, "uploadReport");
exports2.uploadReport = uploadReport;
function uploadCaches(cacheDir, additionalCacheHash, execute) {
function uploadCaches(cacheDir, primaryKey, execute) {
return __awaiter2(this, void 0, void 0, function* () {
if (!execute) {
return;
Expand All @@ -63181,7 +63182,6 @@ var require_utils7 = __commonJS({
core2.warning("Cache is not supported on GHES. See https://github.com/actions/cache/issues/505 for more details");
return;
}
const primaryKey = `qodana-${process.env["GITHUB_REF"]}-${additionalCacheHash}`;
try {
yield cache.saveCache([cacheDir], primaryKey);
core2.info(`Cache saved with key ${primaryKey}`);
Expand All @@ -63195,7 +63195,7 @@ var require_utils7 = __commonJS({
}
__name(uploadCaches, "uploadCaches");
exports2.uploadCaches = uploadCaches;
function restoreCaches(cacheDir, additionalCacheHash, execute) {
function restoreCaches(cacheDir, primaryKey, additionalCacheKey, execute) {
return __awaiter2(this, void 0, void 0, function* () {
if (!execute) {
return;
Expand All @@ -63205,8 +63205,7 @@ var require_utils7 = __commonJS({
core2.warning("Cache is not supported on GHES. See https://github.com/actions/cache/issues/505 for more details");
return;
}
const primaryKey = `qodana-${process.env["GITHUB_REF"]}-${additionalCacheHash}`;
const restoreKeys = [`qodana-${process.env["GITHUB_REF"]}-`, `qodana-`];
const restoreKeys = [additionalCacheKey];
try {
const cacheKey = yield cache.restoreCache([cacheDir], primaryKey, restoreKeys);
if (!cacheKey) {
Expand Down Expand Up @@ -63546,13 +63545,13 @@ function main() {
yield io.mkdirP(inputs.cacheDir);
yield Promise.all([
(0, utils_1.prepareAgent)(inputs.args),
(0, utils_1.restoreCaches)(inputs.cacheDir, inputs.additionalCacheHash, inputs.useCaches)
(0, utils_1.restoreCaches)(inputs.cacheDir, inputs.primaryCacheKey, inputs.additionalCacheKey, inputs.useCaches)
]);
const exitCode = yield (0, utils_1.qodana)();
const canUploadCache = (0, utils_1.isNeedToUploadCache)(inputs.useCaches, inputs.cacheDefaultBranchOnly) && (0, qodana_1.isExecutionSuccessful)(exitCode);
yield Promise.all([
(0, utils_1.uploadReport)(inputs.resultsDir, inputs.artifactName, inputs.uploadResult),
(0, utils_1.uploadCaches)(inputs.cacheDir, inputs.additionalCacheHash, canUploadCache),
(0, utils_1.uploadCaches)(inputs.cacheDir, inputs.primaryCacheKey, canUploadCache),
(0, output_1.publishOutput)(exitCode === qodana_1.QodanaExitCode.FailThreshold, inputs.resultsDir, (0, qodana_1.isExecutionSuccessful)(exitCode), inputs.useAnnotations)
]);
if (!(0, qodana_1.isExecutionSuccessful)(exitCode)) {
Expand Down
5 changes: 3 additions & 2 deletions scan/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ async function main(): Promise<void> {
prepareAgent(inputs.args),
restoreCaches(
inputs.cacheDir,
inputs.additionalCacheHash,
inputs.primaryCacheKey,
inputs.additionalCacheKey,
inputs.useCaches
)
])
Expand All @@ -56,7 +57,7 @@ async function main(): Promise<void> {

await Promise.all([
uploadReport(inputs.resultsDir, inputs.artifactName, inputs.uploadResult),
uploadCaches(inputs.cacheDir, inputs.additionalCacheHash, canUploadCache),
uploadCaches(inputs.cacheDir, inputs.primaryCacheKey, canUploadCache),
publishOutput(
exitCode === QodanaExitCode.FailThreshold,
inputs.resultsDir,
Expand Down
19 changes: 11 additions & 8 deletions scan/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export function getInputs(): Inputs {
.map(arg => arg.trim()),
resultsDir: core.getInput('results-dir'),
cacheDir: core.getInput('cache-dir'),
additionalCacheHash: core.getInput('additional-cache-hash'),
primaryCacheKey: core.getInput('primary-cache-key'),
additionalCacheKey:
core.getInput('additional-cache-key') ||
core.getInput('additional-cache-hash'),
cacheDefaultBranchOnly: core.getBooleanInput('cache-default-branch-only'),
uploadResult: core.getBooleanInput('upload-result'),
artifactName: core.getInput('artifact-name'),
Expand Down Expand Up @@ -126,12 +129,12 @@ export async function uploadReport(
/**
* Uploads the cache to GitHub Actions cache from the given path.
* @param cacheDir The path to upload the cache from.
* @param additionalCacheHash Addition to the generated cache hash
* @param primaryKey Addition to the generated cache hash
* @param execute whether to execute promise or not.
*/
export async function uploadCaches(
cacheDir: string,
additionalCacheHash: string,
primaryKey: string,
execute: boolean
): Promise<void> {
if (!execute) {
Expand All @@ -144,7 +147,6 @@ export async function uploadCaches(
)
return
}
const primaryKey = `qodana-${process.env['GITHUB_REF']}-${additionalCacheHash}`
try {
await cache.saveCache([cacheDir], primaryKey)
core.info(`Cache saved with key ${primaryKey}`)
Expand All @@ -163,12 +165,14 @@ export async function uploadCaches(
/**
* Restores the cache from GitHub Actions cache to the given path.
* @param cacheDir The path to restore the cache to.
* @param additionalCacheHash Addition to the generated cache hash.
* @param primaryKey The primary cache key.
* @param additionalCacheKey The additional cache key.
* @param execute whether to execute promise or not.
*/
export async function restoreCaches(
cacheDir: string,
additionalCacheHash: string,
primaryKey: string,
additionalCacheKey: string,
execute: boolean
): Promise<void> {
if (!execute) {
Expand All @@ -181,8 +185,7 @@ export async function restoreCaches(
)
return
}
const primaryKey = `qodana-${process.env['GITHUB_REF']}-${additionalCacheHash}`
const restoreKeys = [`qodana-${process.env['GITHUB_REF']}-`, `qodana-`]
const restoreKeys = [additionalCacheKey]
try {
const cacheKey = await cache.restoreCache(
[cacheDir],
Expand Down
20 changes: 12 additions & 8 deletions src/commands/scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,23 @@ parameters:
description: >
Directory to store Qodana caches.
Optional.
additional-cache-hash:
primary-cache-key:
type: string
default: << pipeline.git.revision >>
default: qodana-2023.1-<< pipeline.git.branch >>-<< pipeline.git.revision >>
description: >
Allows customizing the generated cache hash.
Allows customizing the primary cache hash.
Optional.
additional-cache-key:
type: string
default: qodana-2023.1-<< pipeline.git.branch >>
description: >
Allows customizing the additional cache hash.
Optional.
steps:
- restore_cache:
keys:
- qodana-{{ .Branch }}-2023.1.0-<< parameters.additional-cache-hash >>
- qodana-{{ .Branch }}-2023.1.0-
- qodana-{{ .Branch }}-
- qodana-
- << parameters.primary-cache-key >>
- << parameters.additional-cache-key >>
- run:
name: Qodana Scan
command: |
Expand All @@ -66,7 +70,7 @@ steps:
path: << parameters.results-dir >>
destination: << parameters.artifact-name >>
- save_cache:
key: qodana-{{ .Branch }}-2023.1.0-<< parameters.additional-cache-hash >>
key: < parameters.primary-cache-key >>
paths:
- << parameters.cache-dir >>
- /tmp/cache/qodana-cli/
3 changes: 2 additions & 1 deletion vsts/QodanaScan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4419,7 +4419,8 @@ var require_utils2 = __commonJS({
cacheDir: tl2.getInput("cacheDir", false) || path.join(home, "cache"),
uploadResult: tl2.getBoolInput("uploadResult", false) || true,
artifactName: tl2.getInput("artifactName", false) || "qodana-report",
additionalCacheHash: "",
additionalCacheKey: "",
primaryCacheKey: "",
useAnnotations: false,
useCaches: false,
cacheDefaultBranchOnly: false,
Expand Down
3 changes: 2 additions & 1 deletion vsts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function getInputs(): Inputs {
uploadResult: tl.getBoolInput('uploadResult', false) || true,
artifactName: tl.getInput('artifactName', false) || 'qodana-report',
// Not used by the task
additionalCacheHash: '',
additionalCacheKey: '',
primaryCacheKey: '',
useAnnotations: false,
useCaches: false,
cacheDefaultBranchOnly: false,
Expand Down

0 comments on commit 029c83d

Please sign in to comment.