replace-tokens
replace-tokens copied to clipboard
Retrieve values from GitHub Variables or Secrets
With the addition of GitHub Variables and the already existing secrets. Is there any limitation to accessing the API and retrieving the values for these parameters automatically without defining the parameters that need replaced? Similar to how the replace tokens extension works with variable groups in ADO?
Thanks!
@mbraunwart-insight did you find a solution?
@rajakarma
I ended up writing a small script to set the environment variable vars as environment variables on the runner for consumption by replace-tokens.
- run: |
import json
import os
env_file = os.getenv('GITHUB_ENV')
y = json.loads('''${{ inputs.vars }}''')
# y = json.loads('''${{ toJson(vars) }}''')
with open(env_file, "a") as file:
for key, value in y.items():
file.write(f"{key}={value}\n")
shell: python
- uses: cschleiden/replace-tokens@v1
with:
files: ${{ inputs.files }}
tokenPrefix: ${{ inputs.tokenPrefix }}
tokenSuffix: ${{ inputs.tokenSuffix }}
When calling this small action I made (or you can make the update in the job reference itself) but ${{ inputs.vars }}
is a reference to ${{ toJson(vars) }}
.
This has worked for me so far!
Excellent! I will try this. Thanks for quick response @mbraunwart-insight !
any way to merge toJson(vars) and toJson(secrets)?
y = json.loads('''${{ toJson(vars) }}''')
This was the fix for me! Thanks
@rajakarma
I ended up writing a small script to set the environment variable vars as environment variables on the runner for consumption by replace-tokens.
- run: | import json import os env_file = os.getenv('GITHUB_ENV') y = json.loads('''${{ inputs.vars }}''') # y = json.loads('''${{ toJson(vars) }}''') with open(env_file, "a") as file: for key, value in y.items(): file.write(f"{key}={value}\n") shell: python - uses: cschleiden/replace-tokens@v1 with: files: ${{ inputs.files }} tokenPrefix: ${{ inputs.tokenPrefix }} tokenSuffix: ${{ inputs.tokenSuffix }}
When calling this small action I made (or you can make the update in the job reference itself) but
${{ inputs.vars }}
is a reference to${{ toJson(vars) }}
.This has worked for me so far!
Unfortunately this is not an option for me as I cannot install python for use in GitHub Actions. So far as I'm aware, self-hosted runners can only setup python for github actions if they run Ubuntu, but I am running Amazon Linux 2023. If it's possible to transliterate this into bash, I could make it work. Though, I'm not as literate in that language for scripting.
see issue: https://github.com/actions/setup-python/issues/460
I was looking for hours for an action that could handle this. The qetza/replacetokens-action@v1 action can do this out of the box. I'd prefer not to have to write my own script for something the package should handle natively. Out of all the actions I tried, this is the only one I had success with.
- name: Replace tokens in config.json
uses: qetza/replacetokens-action@v1
with:
sources: ./config.json
token-pattern: 'custom'
token-prefix: '#{'
token-suffix: '}'
variables: '[${{ toJSON(vars) }},${{ toJSON(secrets) }}]' # use all env variables & secrets
Could something similar be used with this package? I tried the above syntax directly, but it didn't work.