0

I tried to save a bash script into a github env and hope to be able to execute it later

- name: save to env
  run: |
    echo "SCRIPT=$(cat .github/script.sh)" >> $GITHUB_ENV

I got this error

Error: Unable to process file command 'env' successfully.
Error: Invalid format '# Store the current version number in a variable'

I script.sh content (partially)

#!/bin/bash

# Store the current version number in a variable
current_version=$(jq '.dependencies."main"' package.json | tr -d '"')

I have no issue running sh script.sh in my local, what's wrong with my github action config?

Vienna J
  • 71
  • 5
  • point of clarification: are you sure you mean to `cat` the entire contents of the script (and not the results of executing the script) into the env var `SCRIPT`? i.e, you currently have `SCRIPT=#!/bin/bash\n\nStore the current...blah blah`, which is nearly impossible to properly escape... Or do you actually want just the _results_ of running the script? i.e., should `SCRIPT` to be set to the result of _running_ the script? eg, `echo "SCRIPT=$(.github/script.sh)" >> $GITHUB_ENV` ? – michael Jan 13 '23 at 10:14
  • following up: or perhaps you want `$GITHUB_ENV` to contain the line verbatim, `"SCRIPT=$(cat .github/script.sh)"` ? If so, change it to `echo "SCRIPT=\$(cat .github/script.sh)"` (escape the `$`) or `echo 'SCRIPT=$(cat .github/script.sh)'` (single quotes) , because what is happening now is the entire script is being dumped into the env var `$SCRIPT` – michael Jan 13 '23 at 10:16
  • I changed to single quotes, then i run ```run: echo ${{env. SCRIPT }}``` it print nothing – Vienna J Jan 13 '23 at 10:35
  • Probably because `$GITHUB_ENV` has already been evaluated when the Github Action runs. But this is fundamentally unclear; please don't make us guess what your code should do. – tripleee Jan 13 '23 at 10:42
  • I removed the '#' in my sh script, it seems fine now – Vienna J Jan 13 '23 at 10:59

0 Answers0