0

I am trying to dynamically build a bash command to be run in a github action. My step looks like this:

      - name: Build PREPROD
        run: |
          # Generate dart-define values from the preprod.env file
          DART_DEFINES_PREPROD="$(scripts/some_script.sh)"

          echo $DART_DEFINES_PREPROD

          # Build the apk
          flutter build apk ${DART_DEFINES_PREPROD}

What my script in scripts/some_script.sh does, is convert the content of an environment file like this one:

APP_NAME=nameOfMyApp preprod
APP_SUFFIX=.preprod

into bash arguments that look like this (this is what appears when the echo $DART_DEFINES_PREPROD is run in my job

--dart-define APP_NAME="nameOfMyApp preprod" --dart-define APP_SUFFIX=".preprod"

So to me, it should be running a command like

flutter build apk --dart-define APP_NAME="nameOfMyApp preprod" --dart-define APP_SUFFIX=".preprod"

which is what I want, and is valid when I run this locally on my machine.

Yet, when the step pasted above is ran on the github Action agent, the result is the following error:

Target file "preprod"" not found.
Error: Process completed with exit code 1.

And I just don't understand what happens here. I've tried many other syntaxes that all produces the same result.

I know the issue is coming from the space in my APP_NAME variable, because if I replace it by something else without any space, my step is working fine. But since that value is included in double quotes, I juste don't understand why it is interpreted as two commands, instead of juste one information.

Any help / clue is appreciated ! Thanks !

Update 1:

Ok so following @"Benjamin W." comment, I read your links (thanks) and I think I'm doing the right thing, yet it still fails. Here is my updated code:

# For clarity I removed the .sh script file to show what I'm doing inside of it
          dart_defines_args=()
          input="scripts/envs/preprod.env"
          while IFS='=' read -r name value
          do
              dart_defines_args+="--dart-define "
              dart_defines_args+="$name=\"$value\" "

          done < "$input"

          echo "${dart_defines_args[@]}"

          # Build the apk
          flutter build apk ${dart_defines_args[@]}

I also did the same test with

flutter build apk "${dart_defines_args[@]}"

But I still end up with a

Target file "--dart-define APP_NAME="nameOfMyApp preprod" --dart-define APP_SUFFIX=".preprod"

or

Target file "preprod"" not found.

depending on whether or not I have double quotes around ${dart_defines_args[@]}

Miiite
  • 1,517
  • 10
  • 16
  • 1
    You should use an array and then quote its expansion; see, for example, [Bash doesn't parse quotes when converting a string to arguments](https://stackoverflow.com/q/15857027/3266847), and also [BashFAQ/050](https://mywiki.wooledge.org/BashFAQ/050). This requires changes to `some_script.sh`. – Benjamin W. Jan 19 '22 at 15:58
  • I updated my initial question with additional code I added based on the info you provided. I must be missing something really obvious, but my knowledge of bash is too limited to really see what – Miiite Jan 19 '22 at 17:36

1 Answers1

1

I think the problem is about variable expansion.

Try change:

flutter build apk ${DART_DEFINES_PREPROD}

for

flutter build apk "${DART_DEFINES_PREPROD}"
Juranir Santos
  • 370
  • 2
  • 6
  • I already did a test with this syntax. I get a slightly different error: ``` Target file "--dart-define APP_NAME='the-ring.io preprod' --dart-define APP_SUFFIX='.preprod' not found. ``` – Miiite Jan 19 '22 at 16:36