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[@]}