Skip to content

Commit 9c9dd68

Browse files
committed
Added support for BuildPulse Cache
1 parent 1d7095f commit 9c9dd68

File tree

8 files changed

+4242
-0
lines changed

8 files changed

+4242
-0
lines changed

.github/workflows/build_cache.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Build Cache
2+
3+
on:
4+
push:
5+
branches: [ "cache" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
- name: Install Dependencies
18+
run: npm install
19+
working-directory: ./src/cache
20+
21+
- name: Build Cache Binaries
22+
run: npm run build
23+
working-directory: ./src/cache
24+

src/@orb.yml

+48
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,54 @@ display:
77
home_url: https://github.com/Workshop64/buildpulse-circleci-orb
88

99
commands:
10+
save_cache:
11+
description: Save a file or directory to BuildPulse Cache
12+
parameters:
13+
key:
14+
description: |
15+
The key to use when saving the cache. This key should be unique to the
16+
files or directories being saved.
17+
type: string
18+
paths:
19+
description: |
20+
Paths to files or directories to save to the cache.
21+
type: array
22+
upload_chunk_size:
23+
description: |
24+
The maximum size of each chunk of the file to upload to the cache.
25+
type: integer
26+
default: 33554432 # 32 MB = 32 * 1024 * 1024
27+
steps:
28+
- run:
29+
name: Save a file or directory to BuildPulse Cache
30+
environment:
31+
INPUT_KEY: << parameters.key >>
32+
INPUT_PATHS: << parameters.paths >>
33+
INPUT_UPLOAD_CHUNK_SIZE: << parameters.upload_chunk_size >>
34+
command: << include(save_cache.sh) >>
35+
36+
restore_cache:
37+
description: Restore a file or directory from BuildPulse Cache
38+
parameters:
39+
key:
40+
description: |
41+
The key to use when restoring the cache. This key should be unique to the
42+
files or directories being restored.
43+
type: string
44+
fail_on_cache_miss:
45+
description: |
46+
The key to use when restoring the cache. This key should be unique to the
47+
files or directories being restored.
48+
type: boolean
49+
default: false
50+
steps:
51+
- run:
52+
name: Restore a file or directory from BuildPulse Cache
53+
environment:
54+
INPUT_KEY: << parameters.key >>
55+
INPUT_FAIL_ON_CACHE_MISS: << parameters.fail_on_cache_miss >>
56+
command: << include(restore_cache.sh) >>
57+
1058
upload:
1159
description: Send test results to BuildPulse.
1260

src/cache/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist/
2+
node_modules/
3+
4+
.DS_STORE

src/cache/index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env node
2+
3+
const cache = require('@buildpulse/cache');
4+
const { Command } = require('commander');
5+
6+
async function save() {
7+
const primaryKey = process.env.INPUT_KEY;
8+
const cachePaths = process.env.INPUT_PATHS.split(",");
9+
const uploadChunkSize = parseInt(process.env.INPUT_UPLOAD_CHUNK_SIZE);
10+
const enableCrossOsArchive = true;
11+
12+
const cacheId = await cache.saveCache(
13+
cachePaths,
14+
primaryKey,
15+
{ uploadChunkSize: uploadChunkSize },
16+
enableCrossOsArchive
17+
);
18+
19+
if (cacheId != -1) {
20+
console.log(`Cache saved with key: ${primaryKey}`);
21+
}
22+
}
23+
24+
async function restore() {
25+
const primaryKey = process.env.INPUT_KEY;
26+
const failOnCacheMiss = process.env.INPUT_FAIL_ON_CACHE_MISS === "true";
27+
const enableCrossOsArchive = true;
28+
const cachePaths = [];
29+
const restoreKeys = [];
30+
31+
const cacheKey = await cache.restoreCache(
32+
cachePaths,
33+
primaryKey,
34+
restoreKeys,
35+
{ lookupOnly: false },
36+
enableCrossOsArchive
37+
);
38+
39+
40+
if (!cacheKey) {
41+
if (failOnCacheMiss) {
42+
throw new Error(
43+
`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`
44+
);
45+
}
46+
console.log(`Cache not found for input keys: ${primaryKey}`);
47+
48+
return;
49+
}
50+
}
51+
52+
const program = new Command();
53+
54+
program
55+
.command('save')
56+
.description('Save a file or directory to BuildPulse Cache')
57+
.action(save);
58+
59+
program
60+
.command('restore')
61+
.description('Restore a file or directory from BuildPulse Cache')
62+
.action(restore);
63+
64+
program.parse(process.argv);

0 commit comments

Comments
 (0)