replace-tokens icon indicating copy to clipboard operation
replace-tokens copied to clipboard

Retrieve values from GitHub Variables or Secrets

Open mbraunwart opened this issue 2 years ago • 7 comments

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 avatar Feb 08 '23 13:02 mbraunwart

@mbraunwart-insight did you find a solution?

rajakarma avatar Apr 03 '23 13:04 rajakarma

@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!

mbraunwart avatar Apr 03 '23 13:04 mbraunwart

Excellent! I will try this. Thanks for quick response @mbraunwart-insight !

rajakarma avatar Apr 03 '23 13:04 rajakarma

any way to merge toJson(vars) and toJson(secrets)?

emiliosanches avatar Jul 31 '23 23:07 emiliosanches

y = json.loads('''${{ toJson(vars) }}''')

This was the fix for me! Thanks

andy952 avatar Nov 13 '23 11:11 andy952

@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

FreyGeospatial avatar Jun 03 '24 16:06 FreyGeospatial

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.

thardy avatar Jul 02 '24 14:07 thardy