Skip to content

Commit

Permalink
add convert_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahkadar authored and oNaiPs committed Feb 5, 2023
1 parent 09f4a81 commit b9de755
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ steps:
- run: echo "Value of PREFIXED_MY_SECRET: $PREFIXED_MY_SECRET"
```

**Only export secrets that start with a given string:**

```yaml
steps:
- uses: actions/checkout@v3
- uses: oNaiPs/secrets-to-env-action@v1
with:
secrets: ${{ toJSON(secrets) }}
starts_with: PREFIX_
- run: env
# observe that only vars with PREFIX_ were exported
```

**Only apply string conversions (see below) for secrets that start with a given string:**

```yaml
steps:
- uses: actions/checkout@v3
- uses: oNaiPs/secrets-to-env-action@v1
with:
secrets: ${{ toJSON(secrets) }}
starts_with: PREFIX_
starts_with_convert_prefix: false
convert: lower
- run: env | grep 'PREFIX_'
```

**Convert:**

Converts all exported secrets according to a [template](https://github.com/blakeembrey/change-case#core).
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
starts_with:
required: false
description: 'Only export secrets that start with the provided string'
starts_with_convert_prefix:
required: false
description: 'When used with starts_with, only convert the non-matching part of the secret name'
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async function run(): Promise<void> {
const excludeListStr: string = core.getInput('exclude')
const convert: string = core.getInput('convert')
const startsWith: string = core.getInput('starts_with')
const startsWithConvertPrefix = core.getInput('starts_with_convert_prefix')

let secrets: Record<string, string>
try {
Expand Down Expand Up @@ -79,7 +80,13 @@ with:
).join(', ')}`
)
}
newKey = convertTypes[convert](newKey)
if (startsWith && startsWithConvertPrefix === 'false') {
newKey = `${startsWith}${convertTypes[convert](
newKey.replace(startsWith, '')
)}`
} else {
newKey = convertTypes[convert](newKey)
}
}

if (process.env[newKey]) {
Expand Down

0 comments on commit b9de755

Please sign in to comment.