0

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
vinod827
  • 1,123
  • 1
  • 21
  • 45
  • You should pass `JOBS_TO_COPY_FROM` as an `--arg` to [tag:jq]. Please see the marked duplicate on how to do this. – 0stone0 Mar 10 '23 at 14:05
  • thanks, I tried passing the --arg like this JOBS=$(echo "$JOBS" | jq -r --arg JOBS_TO_COPY_FROM "$JOBS_TO_COPY_FROM" '.[] | select(([.name] | inside($JOBS_TO_COPY_FROM)) and .artifacts_file != null) | .id') however now getting new error:- jq: error (at :1): string ("[\"test\",...) and array (["pages"]) cannot have their containment checked – vinod827 Mar 10 '23 at 15:19
  • Probably ask a new question about your new problem, but please search for duplicates before you ask a question. Searching for the literal error message often works fairly well. https://stackoverflow.com/questions/70187417/array-and-string-cannot-have-their-containment-checked-error-when-trying-to-sear is my first Duck Duck Go hit for "cannot have their containment checked". – tripleee Mar 13 '23 at 06:13

0 Answers0