I'm writing a script in my GitLab CI config YAML file like below:-
abc:
stage: abc
image: "centos7"
variables:
GITLAB_PRIVATE_TOKEN: $GITLAB_TOKEN
CI_JOBS: "test, coverage"
allow_failure: true
script:
- 'cd $CI_PROJECT_DIR'
- 'set -o errexit -o pipefail -o nounset'
- 'API="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}"'
- 'AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}"'
- 'CHILD_PIPELINES=$(curl -sS --header "${AUTH_HEADER}" "${API}/pipelines/${CI_PIPELINE_ID}/bridges")'
- 'echo "$CHILD_PIPELINES" | jq . > bridges-$CI_PIPELINE_ID.json'
- 'CHILD_PIPELINES=$(echo $CHILD_PIPELINES | jq ".[].downstream_pipeline.id")'
- |
echo "$CHILD_PIPELINES" | while read cp
do
# Fetch the IDs of their "build:*" jobs that completed successfully
JOBS=$(curl -sS --header "${AUTH_HEADER}" "${API}/pipelines/$cp/jobs?scope=success")
echo "$JOBS" | jq . >> job-$cp.json
JOBS_TO_COPY_FROM=$(echo $CI_JOBS | sed 's/, /,/g' | jq -Rc 'split(",")')
echo $JOBS_TO_COPY_FROM
JOBS=$(echo "$JOBS" | jq '.[] | select(([.name] | inside($JOBS_TO_COPY_FROM)) and .artifacts_file != null) | .id')
[[ -z "$JOBS" ]] && echo "No jobs in $cp" && continue
echo "$JOBS" | while read job
do
echo "DOWNLOADING ARTIFACT: $job"
curl -sS -L --header "${AUTH_HEADER}" --output artifacts-$job.zip "${API}/jobs/$job/artifacts"
done
done
- |
if ls artifacts-*.zip >/dev/null
then
unzip -o artifacts-\*.zip
else
echo "No artifacts"
fi
My expectation is to have inside (["test", "coverage"))
while using the jq
. If I hardcode then it works but now I'm trying to read from a variable and have replaced it like inside($JOBS_TO_COPY_FROM))
but getting below error;-
$ echo "$CHILD_PIPELINES" | while read cp # collapsed multi-line command
["test","coverage"]
jq: error: $JOBS_TO_COPY_FROM is not defined at <top-level>, line 1:
.[] | select(([.name] | inside($JOBS_TO_COPY_FROM)) and .artifacts_file != null) | .id
jq: 1 compile error