Skip to content

Commit 374e0a0

Browse files
committed
Added support for BuildPulse Cache
1 parent 225d4db commit 374e0a0

File tree

8 files changed

+4224
-0
lines changed

8 files changed

+4224
-0
lines changed

.github/workflows/build_cache.yaml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Build Cache
2+
3+
on:
4+
push:
5+
branches: [ "cache" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build_x64:
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-x64
23+
working-directory: ./src/cache
24+
25+
- name: Upload binaries as build artifacts
26+
uses: actions/[email protected]
27+
with:
28+
name: dist-x64
29+
path: ./src/cache/dist/
30+
retention-days: 730
31+
32+
- name: Set tag name for downstream jobs
33+
id: tag
34+
run: echo "::set-output name=tag::${GITHUB_REF#refs/*/}"
35+
36+
build_arm64:
37+
runs-on: ubuntu-24.04-arm
38+
39+
steps:
40+
- name: Checkout code
41+
uses: actions/checkout@v3
42+
43+
- name: Install Dependencies
44+
run: npm install
45+
working-directory: ./src/cache
46+
47+
- name: Build Cache Binaries
48+
run: npm run build-arm64
49+
working-directory: ./src/cache
50+
51+
- name: Upload binaries as build artifacts
52+
uses: actions/[email protected]
53+
with:
54+
name: dist-arm64
55+
path: ./src/cache/dist/
56+
retention-days: 730
57+
58+
- name: Set tag name for downstream jobs
59+
id: tag
60+
run: echo "::set-output name=tag::${GITHUB_REF#refs/*/}"

.github/workflows/release.yaml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
8+
jobs:
9+
build_x64:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v3
15+
16+
- name: Install Dependencies
17+
run: npm install
18+
working-directory: ./src/cache
19+
20+
- name: Build Cache Binaries
21+
run: npm run build-x64
22+
working-directory: ./src/cache
23+
24+
- name: Create Release
25+
uses: ncipollo/release-action@v1
26+
with:
27+
token: ${{ secrets.GITHUB_TOKEN }}
28+
tag: ${{ github.ref }}-x64
29+
artifacts: ./src/cache/dist/*
30+
draft: true
31+
prerelease: true
32+
33+
build_arm64:
34+
runs-on: ubuntu-24.04-arm
35+
36+
steps:
37+
- name: Checkout code
38+
uses: actions/checkout@v3
39+
40+
- name: Install Dependencies
41+
run: npm install
42+
working-directory: ./src/cache
43+
44+
- name: Build Cache Binaries
45+
run: npm run build-arm64
46+
working-directory: ./src/cache
47+
48+
- name: Create Release
49+
uses: ncipollo/release-action@v1
50+
with:
51+
token: ${{ secrets.GITHUB_TOKEN }}
52+
tag: ${{ github.ref }}-arm64
53+
artifacts: ./src/cache/dist/*
54+
draft: true
55+
prerelease: true

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+
Space-separated paths to files or directories to save to the cache.
21+
type: string
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: ./cache.sh save
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: ./cache.sh restore
57+
1058
upload:
1159
description: Send test results to BuildPulse.
1260

src/cache.sh

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
BUILDPULSE_CACHE_VERSION="v0.11.6"
6+
7+
# Enable double globbing if supported by the shell on the base github runner
8+
if shopt -s globstar; then
9+
echo "This bash shell version supports double globbing: '${BASH_VERSION}'."
10+
11+
else
12+
echo "This bash shell version does not support double globbing: '${BASH_VERSION}'. Please upgrade to bash 4+."
13+
fi
14+
15+
case "$RUNNER_OS" in
16+
Linux)
17+
OS=linux
18+
;;
19+
macOS)
20+
OS=macos
21+
;;
22+
Windows)
23+
OS=win.exe
24+
;;
25+
*)
26+
echo "::error::Unrecognized operating system. Expected RUNNER_OS to be one of \"Linux\", \"macOS\", or \"Windows\", but it was \"$RUNNER_OS\"."
27+
exit 1
28+
esac
29+
30+
ARCH=$(uname -m)
31+
case "$ARCH" in
32+
x86_64) ARCH="x64" ;;
33+
aarch64) ARCH="arm64" ;;
34+
armv7l) ARCH="armv7" ;;
35+
*) ARCH="unknown" ;;
36+
esac
37+
38+
BUILDPULSE_CACHE_BINARY="${BUILDPULSE_CACHE_VERSION}-${ARCH}/cache-${OS}"
39+
40+
BUILDPULSE_CACHE_HOSTS=(
41+
https://github.com/buildpulse/buildpulse-circleci-orb/releases/download/
42+
)
43+
[ -n "${INPUT_CLI_HOST}" ] && BUILDPULSE_CACHE_HOSTS=("${INPUT_CLI_HOST}" "${BUILDPULSE_CACHE_HOSTS[@]}")
44+
45+
getcli() {
46+
local rval=-1
47+
for host in "${BUILDPULSE_CACHE_HOSTS[@]}"; do
48+
url="${host}/${BUILDPULSE_CACHE_BINARY}"
49+
if (set -x; curl -fsSL --retry 3 --retry-connrefused --connect-timeout 5 "$url" > "$1"); then
50+
return 0
51+
else
52+
rval=$?
53+
fi
54+
done;
55+
56+
return $rval
57+
}
58+
59+
if getcli ./buildpulse-cache; then
60+
: # Successfully fetched binary. Great!
61+
else
62+
msg=$(cat <<-eos
63+
::warning::Unable to fetch BuildPulse Cache binary.
64+
65+
If you continue seeing this problem, please get in touch at
66+
https://buildpulse.io/contact so we can look into this issue.
67+
eos
68+
)
69+
70+
echo "${msg//$'\n'/%0A}" # Replace newlines with URL-encoded newlines for proper formatting in GitHub Actions annotations (https://github.com/actions/toolkit/issues/193#issuecomment-605394935)
71+
exit 0
72+
fi
73+
74+
chmod +x ./buildpulse-cache
75+
76+
set -x
77+
78+
ACTION=$1
79+
80+
./builpulse-cache $ACTION

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)