Skip to content

Commit 0f3e401

Browse files
authored
Add support for @actions/cache (#217)
This pull request introduces support for the `@actions/cache` package for local testing, enhances the `.env.example` file with more comprehensive documentation and variables, and updates documentation to reflect these changes. It also adds new test stubs and minor version bumps for toolkit dependencies. **Local caching and environment variable enhancements:** - Added support for the `@actions/cache` package, enabling local caching of dependencies and files between runs by requiring the `LOCAL_ACTION_CACHE_PATH` environment variable. Also clarified requirements for `@actions/artifact` and `@actions/cache` in the changelog. - Major overhaul of `.env.example` to include detailed documentation, default values, and grouping of variables for `@github/local-action`, GitHub Actions inputs, and runner/environment variables. Added new variables such as `LOCAL_ACTION_CACHE_PATH`, `LOCAL_ACTION_WORKSPACE`, and provided more explicit defaults for local testing. **Documentation and toolkit stub management:** - Added instructions in `CONTRIBUTING.md` for updating toolkit stubs, including how to track reviewed versions and compare toolkit package changes, to ensure consistency and easier maintenance. - Updated the supported version of `@actions/github` in the `README.md` from `6.0.0` to `6.0.1`. **Testing and stub improvements:** - Added a stub for `@actions/io` in `__fixtures__/@actions/io.ts` for use in tests, implementing mock versions of `which` and `mkdirP`. - Added a stub for `isDebug` to the `@actions/core` test fixture.
2 parents 08357c8 + e5a7241 commit 0f3e401

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2791
-218
lines changed

.env.example

Lines changed: 341 additions & 34 deletions
Large diffs are not rendered by default.

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## v6
4+
5+
Adds support for the `@actions/cache` package, allowing for local caching of
6+
dependencies and other files between runs of a GitHub Action. This is achieved
7+
by setting the `LOCAL_ACTION_CACHE_PATH` environment variable to a directory
8+
where cache files will be stored.
9+
10+
For both `@actions/artifact` and `@actions/cache`, the `LOCAL_ACTION_WORKSPACE`
11+
environment variable must be set. Otherwise, calling functions will throw an
12+
error. Similarly, `@actions/artifact` requires the `LOCAL_ACTION_ARTIFACT_PATH`
13+
environment variable to be set, and `@actions/cache` requires the
14+
`LOCAL_ACTION_CACHE_PATH` environment variable to be set.
15+
316
## v5
417

518
Removes support for custom `paths` in the target action's `tsconfig.json`. This

CONTRIBUTING.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,37 @@ npm run test
104104
> [!NOTE]
105105
>
106106
> This requires that you have already run `npm install`
107+
108+
### Toolkit Stub Updates
109+
110+
When updating any of the supported stubs for the GitHub Actions Toolkit, please
111+
make sure to also update the following. This helps ensure that we are using the
112+
latest supported versions of the toolkit packages, and makes comparing changes
113+
across commits easier.
114+
115+
1. The supported version in the [`README.md`](./README.md)
116+
117+
```markdown
118+
| Package | Version |
119+
| ---------------------------------------------------------------------- | -------- |
120+
| [`@actions/artifact`](https://www.npmjs.com/package/@actions/artifact) | `2.3.2` |
121+
| [`@actions/core`](https://www.npmjs.com/package/@actions/core) | `1.11.1` |
122+
| [`@actions/github`](https://www.npmjs.com/package/@actions/github) | `6.0.1` |
123+
```
124+
125+
1. The latest reviewed commit URL in the corresponding `src/stubs/*.ts` file
126+
127+
```typescript
128+
/**
129+
* Last Reviewed Commit: https://github.com/actions/toolkit/blob/f58042f9cc16bcaa87afaa86c2974a8c771ce1ea/packages/artifact/src/artifact.ts
130+
* Last Reviewed Date: 2025-09-10
131+
*/
132+
```
133+
134+
When comparing commits to see what has been updated in the toolkit packages, be
135+
sure to compare against the last reviewed commit in the corresponding stub file.
136+
This can be done more easily using the following URL format:
137+
138+
```plain
139+
https://github.com/actions/toolkit/compare/<old commit>..main
140+
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ currently implemented by this tool.
2121
| ---------------------------------------------------------------------- | -------- |
2222
| [`@actions/artifact`](https://www.npmjs.com/package/@actions/artifact) | `2.3.2` |
2323
| [`@actions/core`](https://www.npmjs.com/package/@actions/core) | `1.11.1` |
24-
| [`@actions/github`](https://www.npmjs.com/package/@actions/github) | `6.0.0` |
24+
| [`@actions/github`](https://www.npmjs.com/package/@actions/github) | `6.0.1` |
2525

2626
## Changelog
2727

__fixtures__/@actions/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { jest } from '@jest/globals'
33
export const debug = jest.fn().mockImplementation(() => {})
44
export const error = jest.fn().mockImplementation(() => {})
55
export const info = jest.fn().mockImplementation(() => {})
6+
export const isDebug = jest.fn().mockImplementation(() => false)
67
export const getInput = jest.fn().mockImplementation(() => {})
78
export const setOutput = jest.fn().mockImplementation(() => {})
89
export const setFailed = jest.fn().mockImplementation(() => {})

__fixtures__/@actions/io.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { jest } from '@jest/globals'
2+
3+
export const which = jest.fn().mockImplementation(() => {})
4+
export const mkdirP = jest.fn().mockImplementation(() => {})
5+
6+
export default {
7+
which,
8+
mkdirP
9+
}

__fixtures__/fs.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import { jest } from '@jest/globals'
22

33
export const accessSync = jest.fn()
4+
export const copyFileSync = jest.fn()
45
export const createWriteStream = jest.fn()
56
export const createReadStream = jest.fn()
67
export const existsSync = jest.fn()
78
export const mkdirSync = jest.fn()
9+
export const readdirSync = jest.fn()
810
export const readFileSync = jest.fn()
911
export const rmSync = jest.fn()
1012
export const statSync = jest.fn()
13+
export const writeFileSync = jest.fn()
1114

1215
export default {
1316
accessSync,
17+
copyFileSync,
1418
createWriteStream,
1519
createReadStream,
1620
existsSync,
1721
mkdirSync,
22+
readdirSync,
1823
readFileSync,
1924
rmSync,
20-
statSync
25+
statSync,
26+
writeFileSync
2127
}

__tests__/stubs/artifact/internal/client.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const { DefaultArtifactClient } = await import(
6767
describe('DefaultArtifactClient', () => {
6868
beforeEach(() => {
6969
// Set environment variables
70+
process.env.LOCAL_ACTION_WORKSPACE = '/tmp/artifacts'
7071
process.env.LOCAL_ACTION_ARTIFACT_PATH = '/tmp/artifacts'
7172

7273
// Reset metadata
@@ -80,10 +81,23 @@ describe('DefaultArtifactClient', () => {
8081
jest.resetAllMocks()
8182

8283
// Unset environment variables
84+
delete process.env.LOCAL_ACTION_WORKSPACE
8385
delete process.env.LOCAL_ACTION_ARTIFACT_PATH
8486
})
8587

8688
describe('uploadArtifact', () => {
89+
it('Throws if LOCAL_ACTION_WORKSPACE is not set', async () => {
90+
delete process.env.LOCAL_ACTION_WORKSPACE
91+
92+
const client = new DefaultArtifactClient()
93+
94+
await expect(
95+
client.uploadArtifact('artifact-name', ['file1', 'file2'], 'root')
96+
).rejects.toThrow(
97+
'LOCAL_ACTION_WORKSPACE must be set when interacting with @actions/artifact!'
98+
)
99+
})
100+
87101
it('Throws if LOCAL_ACTION_ARTIFACT_PATH is not set', async () => {
88102
delete process.env.LOCAL_ACTION_ARTIFACT_PATH
89103

@@ -114,6 +128,16 @@ describe('DefaultArtifactClient', () => {
114128
})
115129

116130
describe('downloadArtifact', () => {
131+
it('Throws if LOCAL_ACTION_WORKSPACE is not set', async () => {
132+
delete process.env.LOCAL_ACTION_WORKSPACE
133+
134+
const client = new DefaultArtifactClient()
135+
136+
await expect(client.downloadArtifact(1)).rejects.toThrow(
137+
'LOCAL_ACTION_WORKSPACE must be set when interacting with @actions/artifact!'
138+
)
139+
})
140+
117141
it('Throws if LOCAL_ACTION_ARTIFACT_PATH is not set', async () => {
118142
delete process.env.LOCAL_ACTION_ARTIFACT_PATH
119143

@@ -157,6 +181,16 @@ describe('DefaultArtifactClient', () => {
157181
})
158182

159183
describe('listArtifacts', () => {
184+
it('Throws if LOCAL_ACTION_WORKSPACE is not set', async () => {
185+
delete process.env.LOCAL_ACTION_WORKSPACE
186+
187+
const client = new DefaultArtifactClient()
188+
189+
await expect(client.listArtifacts()).rejects.toThrow(
190+
'LOCAL_ACTION_WORKSPACE must be set when interacting with @actions/artifact!'
191+
)
192+
})
193+
160194
it('Throws if LOCAL_ACTION_ARTIFACT_PATH is not set', async () => {
161195
delete process.env.LOCAL_ACTION_ARTIFACT_PATH
162196

@@ -200,6 +234,16 @@ describe('DefaultArtifactClient', () => {
200234
})
201235

202236
describe('getArtifact', () => {
237+
it('Throws if LOCAL_ACTION_WORKSPACE is not set', async () => {
238+
delete process.env.LOCAL_ACTION_WORKSPACE
239+
240+
const client = new DefaultArtifactClient()
241+
242+
await expect(client.getArtifact('artifact-name')).rejects.toThrow(
243+
'LOCAL_ACTION_WORKSPACE must be set when interacting with @actions/artifact!'
244+
)
245+
})
246+
203247
it('Throws if LOCAL_ACTION_ARTIFACT_PATH is not set', async () => {
204248
delete process.env.LOCAL_ACTION_ARTIFACT_PATH
205249

@@ -243,6 +287,16 @@ describe('DefaultArtifactClient', () => {
243287
})
244288

245289
describe('deleteArtifact', () => {
290+
it('Throws if LOCAL_ACTION_WORKSPACE is not set', async () => {
291+
delete process.env.LOCAL_ACTION_WORKSPACE
292+
293+
const client = new DefaultArtifactClient()
294+
295+
await expect(client.deleteArtifact('artifact-name')).rejects.toThrow(
296+
'LOCAL_ACTION_WORKSPACE must be set when interacting with @actions/artifact!'
297+
)
298+
})
299+
246300
it('Throws if LOCAL_ACTION_ARTIFACT_PATH is not set', async () => {
247301
delete process.env.LOCAL_ACTION_ARTIFACT_PATH
248302

0 commit comments

Comments
 (0)