'How to save github secrets content in a file in github action

I was trying to do the following :

echo ${{secrets.key}} > myfile

But unfortunately, this doesn't work since myfile would be empty after this when i checked. How do i save the content of github secret into a file ?



Solution 1:[1]

Check that the secrets.key actually exists before doing the above.

Solution 2:[2]

You can first pass the secret to an env var, then echo it to the file, remember to quote the env var to prevent line feed from breaking the command.

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: print secrets
      run: |
          echo "$MY_SECRET" >> config.ini
          cat config.ini
      shell: bash
      env:
        MY_SECRET: ${{secrets.KEY}}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yong
Solution 2 Reorx