Alternative syntax for accessing GitHub secrets in actions
If you have worked at all with GitHub actions, you might be familiar with the common syntax for how to access them:
${{ secrets.SuperSecret }}
But did you know there’s an alternative syntax?
${{ secrets[SuperSecret] }}
This can be useful in areas where the secret context is not accessible. For example, if you have a matrix strategy where you need different secrets for different jobs, you can set the secret name in the matrix definition, and use this syntax to access it in the job itself.
jobs:
example_matrix:
strategy:
matrix:
secret: secret1, secret2
steps:
- run: echo ${{ secret[matrix.secret] }}
The same strategy can be applied if you have secrets based on environment names. Consider you have the a secret called SuperSecret_<environment>
, you can use something like:
on:
workflow_call:
inputs:
environment:
type: string
jobs:
example:
runs-on: ubuntu-latest
steps:
- run: echo ${{ secret[format('SuperSecret_{0}', inputs.environment)] }}