Issue
I'm trying to figure out how to declare a variable that is an array and use it as arguments passed to a shell script. Or find out how to be a bit more DRY by learning how to
Here's what I have tried that causes /bin/sh: eval: line X: syntax error: bad substitution
:
variables:
EXTRA_ARGS: (. "${CI_COMMIT_SHORT_SHA} pipeline-${CI_PIPELINE_ID} latest" "pipeline-${CI_PIPELINE_ID}" $CI_COMMIT_REF_NAME $CI_COMMIT_REF_SLUG)
build:
stage: build
script:
- path/to/script.sh uniqueParam1 uniqueParam2 ${EXTRA_ARGS[@]}
- path/to/script.sh uniqueParam3 uniqueParam4 ${EXTRA_ARGS[@]}
- path/to/script.sh uniqueParam5 uniqueParam6 ${EXTRA_ARGS[@]}
And the follow runs fine, but as you can tell, I'm repeating the extra arguments:
build:
stage: build
script:
- path/to/script.sh uniqueParam1 uniqueParam2 . "${CI_COMMIT_SHORT_SHA} pipeline-${CI_PIPELINE_ID} latest" "pipeline-${CI_PIPELINE_ID}" $CI_COMMIT_REF_NAME $CI_COMMIT_REF_SLUG
- path/to/script.sh uniqueParam3 uniqueParam4 . "${CI_COMMIT_SHORT_SHA} pipeline-${CI_PIPELINE_ID} latest" "pipeline-${CI_PIPELINE_ID}" $CI_COMMIT_REF_NAME $CI_COMMIT_REF_SLUG
- path/to/script.sh uniqueParam5 uniqueParam6 . "${CI_COMMIT_SHORT_SHA} pipeline-${CI_PIPELINE_ID} latest" "pipeline-${CI_PIPELINE_ID}" $CI_COMMIT_REF_NAME $CI_COMMIT_REF_SLUG
I've also tried to remove the .
dot, as an argument, but the error persists.
For my sake of mind I tried to run the concept in bash, and ran with success as follows:
#!/bin/bash
TEST1="Hello"
TEST2="human!"
ARGS=(. $TEST1 $TEST2 "${TEST2} ${TEST1}" "param1" "param2")
echo "${ARGS[*]}"
Outputs:
. Hello human! human! Hello param1 param2
Also tried to put it in the scripts as:
script:
- EXTRA_ARGS=(. "${CI_COMMIT_SHORT_SHA} pipeline-${CI_PIPELINE_ID} latest" "pipeline-${CI_PIPELINE_ID}" $CI_COMMIT_REF_NAME $CI_COMMIT_REF_SLUG)
And also doesn't work:
/bin/sh: eval: line 92: syntax error: unexpected "("
Also tried to escape \(
but did not work.
Solution
As pointed out by the first comment, the issue is related to the Docker image that is based in Alpine that has bourne shell and not bash by default.
https://gitlab.com/gitlab-org/gitlab-runner/issues/1758
Answered By - punkbit Answer Checked By - David Marino (WPSolving Volunteer)