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

Added option to convert secrets to base64 #303

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ steps:
# E.g. secret with MY_SECRET would become PREFIX_my_secret
```

**Converts all exported secrets to a base64 string (default is false):**

```yaml
steps:
- uses: actions/checkout@v3
- uses: oNaiPs/secrets-to-env-action@v1
with:
secrets: ${{ toJSON(secrets) }}
value_as_base64: true
- run: echo "Value of my_secret: $my_secret"
```

## How it works

This action uses the input in `secrets` to read all the secrets in the JSON format, and exporting all the variables one by one.
Expand Down
17 changes: 17 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function mockInputs(inputs: {[key: string]: string}) {

describe('secrets-to-env-action', () => {
let inputSecrets: {[key: string]: string}
let inputSecretsBase64: {[key: string]: string}
let newSecrets: {[key: string]: string}

beforeEach(() => {
Expand All @@ -27,6 +28,12 @@ describe('secrets-to-env-action', () => {
my_low_secret_1: 'low_value_1'
}

inputSecretsBase64 = {
MY_SECRET_1: 'VkFMVUVfMQ==',
MY_SECRET_2: 'VkFMVUVfMg==',
my_low_secret_1: 'bG93X3ZhbHVlXzE='
}

newSecrets = {}
jest
.mocked(core.exportVariable)
Expand Down Expand Up @@ -243,4 +250,14 @@ describe('secrets-to-env-action', () => {

expect(newSecrets).toEqual(filteredNewSecrets)
})

it('converts to base64', () => {
mockInputs({
secrets: JSON.stringify(inputSecrets),
value_as_base64: 'true'
})
main()

expect(newSecrets).toEqual(inputSecretsBase64)
})
})
9 changes: 6 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'Export all secrets to env variables'
description: 'Utility action that exports all github secrets to environment variables'
author: 'Jose Pereira @oNaiPs'
name: 'Export all variables from other contexts to env variables'
description: 'Utility action that exports all github secrets and vars to environment variables'
author: 'Danilo Körber'
inputs:
secrets:
required: true
Expand All @@ -23,6 +23,9 @@ inputs:
override:
required: false
description: 'Either to override or not the variable if it already exists'
value_as_base64:
required: false
description: 'Either to convewrt or not the value to base64'
runs:
using: 'node20'
main: 'dist/index.js'
Expand Down
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default async function run(): Promise<void> {
: true
const overrideStr: string = core.getInput('override')
const override = overrideStr.length ? overrideStr === 'true' : true
const valueAsBase64Str: string = core.getInput('value_as_base64')
const valueAsBase64 = valueAsBase64Str.length
? valueAsBase64Str === 'true'
: false

let secrets: Record<string, string>
try {
Expand Down Expand Up @@ -99,7 +103,11 @@ with:
}
}

core.exportVariable(newKey, secrets[key])
let newValue = valueAsBase64
? Buffer.from(secrets[key]).toString('base64')
: secrets[key]

core.exportVariable(newKey, newValue)
core.info(`Exported secret ${newKey}`)
}
} catch (error) {
Expand Down