Skip to content

Commit

Permalink
Allow providing custom hash as input
Browse files Browse the repository at this point in the history
  • Loading branch information
macko911 committed Aug 29, 2024
1 parent f22d46a commit b37ac96
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ order to determine the repo state.
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ----------------- |
| 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` | |
| 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 }} |
| custom-hash | Hash calculated on the user's side used as part of the cache key for the job run. | `false` | |
| aws-region | AWS region for the S3 bucket used for cache files. available. | `true` | |
| 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` | |
| 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` | |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
used for the cache files is `cache/${repoOwner}/${repoName}/${keyPrefix}/${treeHash}`
default: ${{ github.job }}
required: false
custom-hash:
description:
Hash calculated on the user's side used as part of the cache key for the job run.
required: false
aws-region:
description: AWS region for the S3 bucket used for cache files. available.
required: true
Expand Down
23 changes: 23 additions & 0 deletions src/restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,27 @@ describe(`S3 Cache Action - Restore cache`, () => {
})
}
)

test(
strip`
When a custom hash is provided
Then it should use the custom hash instead of the auto-generated one
`,
async () => {
const treeHash = 'cba2d570993b9c21e3de282e5ba56d1638fb32de'
const customHash = '070e5b3591d571974f31f594d6f841ea'
mockedUtils.getCurrentRepoTreeHash.mockResolvedValue(treeHash)
mockedUtils.fileExistsInS3.mockResolvedValue(true)

const output = await restoreS3Cache({
bucket: 'my-other-bucket',
keyPrefix: 'horse',
repo: {owner: 'my-org', repo: 'my-repo'},
awsOptions,
customHash
})

expect(output.treeHash).toBe(customHash)
}
)
})
8 changes: 6 additions & 2 deletions src/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import {getCurrentRepoTreeHash, fileExistsInS3, runAction, AWSOptions} from './u
runAction(async () => {
const bucket = getInput('bucket-name', {required: true})
const keyPrefix = getInput('key-prefix')
const customHash = getInput('custom-hash')
const repo = github.context.repo
const awsOptions = {
region: getInput('aws-region'),
accessKeyId: getInput('aws-access-key-id'),
secretAccessKey: getInput('aws-secret-access-key')
}

const output = await restoreS3Cache({bucket, keyPrefix, repo, awsOptions})
const output = await restoreS3Cache({bucket, keyPrefix, customHash, repo, awsOptions})

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

export async function restoreS3Cache({
bucket,
keyPrefix,
customHash,
repo,
awsOptions
}: RestoreS3CacheActionArgs) {
const treeHash = await getCurrentRepoTreeHash()
const currentRepoTreeHash = await getCurrentRepoTreeHash()
const treeHash = customHash ?? currentRepoTreeHash

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

0 comments on commit b37ac96

Please sign in to comment.