Skip to content

Commit b37ac96

Browse files
committed
Allow providing custom hash as input
1 parent f22d46a commit b37ac96

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ order to determine the repo state.
4444
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ----------------- |
4545
| bucket-name | Name of the S3 bucket to use for storing cache files. The job needs to have AWS credentials configured to allow read/write from this bucket. | `true` | |
4646
| key-prefix | Key prefix to add to the cache files key. By default the job ID is used. The full key used for the cache files is `cache/${repoOwner}/${repoName}/${keyPrefix}/${treeHash}` | `false` | ${{ github.job }} |
47+
| custom-hash | Hash calculated on the user's side used as part of the cache key for the job run. | `false` | |
4748
| aws-region | AWS region for the S3 bucket used for cache files. available. | `true` | |
4849
| aws-access-key-id | Access Key ID for an IAM user with permissions to read/write to the S3 bucket used for cache files. | `true` | |
4950
| aws-secret-access-key | Secret Access Key for an IAM user with permissions to read/write to the S3 bucket used for cache files. | `true` | |

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ inputs:
1414
used for the cache files is `cache/${repoOwner}/${repoName}/${keyPrefix}/${treeHash}`
1515
default: ${{ github.job }}
1616
required: false
17+
custom-hash:
18+
description:
19+
Hash calculated on the user's side used as part of the cache key for the job run.
20+
required: false
1721
aws-region:
1822
description: AWS region for the S3 bucket used for cache files. available.
1923
required: true

src/restore.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,27 @@ describe(`S3 Cache Action - Restore cache`, () => {
7575
})
7676
}
7777
)
78+
79+
test(
80+
strip`
81+
When a custom hash is provided
82+
Then it should use the custom hash instead of the auto-generated one
83+
`,
84+
async () => {
85+
const treeHash = 'cba2d570993b9c21e3de282e5ba56d1638fb32de'
86+
const customHash = '070e5b3591d571974f31f594d6f841ea'
87+
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(treeHash)
88+
mockedUtils.fileExistsInS3.mockResolvedValue(true)
89+
90+
const output = await restoreS3Cache({
91+
bucket: 'my-other-bucket',
92+
keyPrefix: 'horse',
93+
repo: {owner: 'my-org', repo: 'my-repo'},
94+
awsOptions,
95+
customHash
96+
})
97+
98+
expect(output.treeHash).toBe(customHash)
99+
}
100+
)
78101
})

src/restore.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import {getCurrentRepoTreeHash, fileExistsInS3, runAction, AWSOptions} from './u
1313
runAction(async () => {
1414
const bucket = getInput('bucket-name', {required: true})
1515
const keyPrefix = getInput('key-prefix')
16+
const customHash = getInput('custom-hash')
1617
const repo = github.context.repo
1718
const awsOptions = {
1819
region: getInput('aws-region'),
1920
accessKeyId: getInput('aws-access-key-id'),
2021
secretAccessKey: getInput('aws-secret-access-key')
2122
}
2223

23-
const output = await restoreS3Cache({bucket, keyPrefix, repo, awsOptions})
24+
const output = await restoreS3Cache({bucket, keyPrefix, customHash, repo, awsOptions})
2425

2526
// Saving key and hash in "state" which can be retrieved by the
2627
// "post" run of the action (save.ts)
@@ -35,17 +36,20 @@ runAction(async () => {
3536
type RestoreS3CacheActionArgs = {
3637
bucket: string
3738
keyPrefix: string
39+
customHash?: string
3840
repo: {owner: string; repo: string}
3941
awsOptions: AWSOptions
4042
}
4143

4244
export async function restoreS3Cache({
4345
bucket,
4446
keyPrefix,
47+
customHash,
4548
repo,
4649
awsOptions
4750
}: RestoreS3CacheActionArgs) {
48-
const treeHash = await getCurrentRepoTreeHash()
51+
const currentRepoTreeHash = await getCurrentRepoTreeHash()
52+
const treeHash = customHash ?? currentRepoTreeHash
4953

5054
const key = `cache/${repo.owner}/${repo.repo}/${keyPrefix}/${treeHash}`
5155
const fileExists = await fileExistsInS3({key, bucket, awsOptions})

0 commit comments

Comments
 (0)