Skip to content

Commit

Permalink
Add override flag support
Browse files Browse the repository at this point in the history
  • Loading branch information
sscotth authored and oNaiPs committed Jun 22, 2023
1 parent d7c796e commit 13568fe
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ jobs:
true
shell: bash

test-override:
runs-on: ubuntu-latest
env:
SECRET_1: DONT_OVERRIDE
steps:
- uses: actions/checkout@v3
- name: Export secrets to env variables (override)
uses: ./
with:
override: false
secrets: ${{ toJSON(secrets) }}
- name: Verify secrets to env variables (override)
run: |
[[ "${SECRET_1}" != "DONT_OVERRIDE" ]] && echo "Override test failed" && exit 1
true
shell: bash

test-convert:
runs-on: ubuntu-latest
steps:
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ steps:
- run: echo "Value of PREFIXED_MY_SECRET: $PREFIXED_MY_SECRET"
```

**Override:**

Overrides already existing variables (default is true)

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

**Convert:**

Converts all exported secrets according to a [template](https://github.com/blakeembrey/change-case#core).
Expand Down
31 changes: 31 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,35 @@ describe('secrets-to-env-action', () => {
PREFIX_my_secret_2: inputSecrets.MY_SECRET_2
})
})

it('overrides variables', () => {
process.env = {
MY_SECRET_1: 'OVERRIDE'
}

mockInputs({
secrets: JSON.stringify(inputSecrets),
override: 'true'
})
main()

expect(newSecrets).toEqual(inputSecrets)
})

it('does not override variables', () => {
process.env = {
MY_SECRET_1: 'DONT_OVERRIDE'
}

mockInputs({
secrets: JSON.stringify(inputSecrets),
override: 'false'
})
main()

const filteredNewSecrets = Object.assign({}, newSecrets)
delete filteredNewSecrets.MY_SECRET_1

expect(newSecrets).toEqual(filteredNewSecrets)
})
})
10 changes: 9 additions & 1 deletion dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default async function run(): Promise<void> {
const convertPrefix = convertPrefixStr.length
? convertPrefixStr === 'true'
: true
const overrideStr: string = core.getInput('override')
const override = overrideStr.length ? overrideStr === 'true' : true

let secrets: Record<string, string>
try {
Expand Down Expand Up @@ -89,7 +91,12 @@ with:
}

if (process.env[newKey]) {
core.warning(`Will re-write "${newKey}" environment variable.`)
if (override) {
core.warning(`Will re-write "${newKey}" environment variable.`)
} else {
core.info(`Skip overwriting secret ${newKey}`)
continue
}
}

core.exportVariable(newKey, secrets[key])
Expand Down

0 comments on commit 13568fe

Please sign in to comment.