Skip to content

Commit b7f4865

Browse files
committed
Add secrets
1 parent a4f4ca7 commit b7f4865

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This GitHub Action allows you to collect environment variables with a specific p
1111
prefix: 'API_'
1212
output: '.env.api'
1313
remove-prefix: true
14+
secrets: ${{ toJson(secrets) }}
1415
```
1516
1617
#### Inputs
@@ -43,6 +44,7 @@ jobs:
4344
prefix: 'API_'
4445
output: '.env.api'
4546
remove-prefix: true
47+
secrets: ${{ toJson(secrets) }}
4648
```
4749
4850
#### Example

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ inputs:
1313
output:
1414
description: The file to write to
1515
required: true
16+
secrets:
17+
description: The secrets to collect. This is a JSON object stringified.
18+
required: false
19+
type: string
20+
default: "{}"
1621
remove-prefix:
1722
description: The prefix to remove
1823
required: false

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "collect-env",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Collect env values and write to a file",
55
"author": "JeongMin Oh",
66
"license": "Apache-2.0",

src/__tests__/run.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ test('should write filtered environment variables to file', async () => {
2828
process.env.OTHER_VAR = 'other'
2929

3030
mockGetInput.mockImplementation((name: string) => {
31-
switch (name as 'prefix' | 'output') {
31+
switch (name as 'prefix' | 'output' | 'secrets') {
3232
case 'prefix':
3333
return testPrefix
3434
case 'output':
3535
return testOutput
36+
case 'secrets':
37+
return '{"TEST_SECRET": "secret123"}'
3638
}
3739
})
3840

@@ -46,7 +48,7 @@ test('should write filtered environment variables to file', async () => {
4648
expect(mockGetBooleanInput).toHaveBeenCalledWith('remove-prefix')
4749
expect(mockWriteFile).toHaveBeenCalledWith(
4850
testOutput,
49-
'TEST_VAR1=value1\nTEST_VAR2=value2',
51+
'TEST_SECRET=secret123\nTEST_VAR1=value1\nTEST_VAR2=value2',
5052
)
5153
})
5254

@@ -58,11 +60,13 @@ test('should handle writeFile error', async () => {
5860
process.env.TEST_VAR = 'value'
5961

6062
mockGetInput.mockImplementation((name: string) => {
61-
switch (name as 'prefix' | 'output') {
63+
switch (name as 'prefix' | 'output' | 'secrets') {
6264
case 'prefix':
6365
return testPrefix
6466
case 'output':
6567
return testOutput
68+
case 'secrets':
69+
return '{"TEST_SECRET": "secret123"}'
6670
}
6771
})
6872

@@ -80,11 +84,13 @@ test('should handle empty environment variables', async () => {
8084
const testOutput = 'empty.env'
8185

8286
mockGetInput.mockImplementation((name: string) => {
83-
switch (name as 'prefix' | 'output') {
87+
switch (name as 'prefix' | 'output' | 'secrets') {
8488
case 'prefix':
8589
return testPrefix
8690
case 'output':
8791
return testOutput
92+
case 'secrets':
93+
return '{"TEST_SECRET": "secret123"}'
8894
}
8995
})
9096

@@ -94,6 +100,7 @@ test('should handle empty environment variables', async () => {
94100
await run()
95101

96102
expect(mockWriteFile).toHaveBeenCalledWith(testOutput, '')
103+
expect(mockGetInput).toHaveBeenCalledWith('secrets')
97104
})
98105

99106
test('should filter environment variables by prefix', async () => {
@@ -106,11 +113,13 @@ test('should filter environment variables by prefix', async () => {
106113
process.env.API_TIMEOUT = '5000'
107114

108115
mockGetInput.mockImplementation((name: string) => {
109-
switch (name as 'prefix' | 'output') {
116+
switch (name as 'prefix' | 'output' | 'secrets') {
110117
case 'prefix':
111118
return testPrefix
112119
case 'output':
113120
return testOutput
121+
case 'secrets':
122+
return '{"TEST_SECRET": "secret123"}'
114123
}
115124
})
116125

@@ -136,11 +145,13 @@ test('should remove prefix from environment variables', async () => {
136145
process.env.API_TIMEOUT = '5000'
137146

138147
mockGetInput.mockImplementation((name: string) => {
139-
switch (name as 'prefix' | 'output') {
148+
switch (name as 'prefix' | 'output' | 'secrets') {
140149
case 'prefix':
141150
return testPrefix
142151
case 'output':
143152
return testOutput
153+
case 'secrets':
154+
return '{"TEST_SECRET": "secret123"}'
144155
}
145156
})
146157

src/run.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import { writeFile } from 'node:fs/promises'
2-
import { debug, getBooleanInput, getInput, setFailed } from '@actions/core'
2+
import { getBooleanInput, getInput, setFailed } from '@actions/core'
33

44
export async function run() {
55
const prefix = getInput('prefix')
66
const output = getInput('output')
7+
const secrets = JSON.parse(getInput('secrets'))
78
const removePrefix = getBooleanInput('remove-prefix')
8-
debug(`prefix: ${prefix}`)
9-
debug(`output: ${output}`)
10-
debug(`removePrefix: ${removePrefix}`)
11-
debug(`process.env: ${JSON.stringify(Object.keys(process.env), null, 2)}`)
12-
debug(`process.env: ${JSON.stringify(Object.entries(process.env), null, 2)}`)
139

1410
try {
1511
await writeFile(
1612
output,
17-
Object.keys(process.env)
18-
.filter((key) => key.startsWith(prefix))
13+
Object.entries({ ...secrets, ...process.env })
14+
.filter(([key]) => key.startsWith(prefix))
1915
.map(
20-
(key) =>
21-
`${removePrefix ? key.substring(prefix.length) : key}=${process.env[key]}`,
16+
([key, value]) =>
17+
`${removePrefix ? key.substring(prefix.length) : key}=${value}`,
2218
)
2319
.join('\n'),
2420
)

0 commit comments

Comments
 (0)