diff --git a/README.md b/README.md index 61a9e28..735cd36 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index df516a9..d1558f4 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -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(() => { @@ -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) @@ -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) + }) }) diff --git a/action.yml b/action.yml index 72782ec..3179de9 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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' diff --git a/src/main.ts b/src/main.ts index d5d69c4..519fd86 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,6 +34,10 @@ export default async function run(): Promise { : 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 try { @@ -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) {