Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve values from GitHub Variables or Secrets #31

Open
mbraunwart-insight opened this issue Feb 8, 2023 · 7 comments
Open

Retrieve values from GitHub Variables or Secrets #31

mbraunwart-insight opened this issue Feb 8, 2023 · 7 comments

Comments

@mbraunwart-insight
Copy link

mbraunwart-insight commented Feb 8, 2023

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!

@rajakarma
Copy link

@mbraunwart-insight did you find a solution?

@mbraunwart-insight
Copy link
Author

mbraunwart-insight commented Apr 3, 2023

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

@rajakarma
Copy link

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

@emiliosanches
Copy link

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

@andy952
Copy link

andy952 commented Nov 13, 2023

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

This was the fix for me! Thanks

@FreyGeospatial
Copy link

FreyGeospatial commented Jun 3, 2024

@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: actions/setup-python#460

@thardy
Copy link

thardy commented Jul 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants