Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom tree hash input #31

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 5 additions & 3 deletions dist/restore/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading