在GitLab CI配置文件脚本中出现“折叠的多行命令错误”

huangapple go评论68阅读模式
英文:

Getting # collapsed multi-line command error in GitLab CI config file scripts

问题

I'm trying to put a multi-line shell script command in the GitLab CI YAML file as shown below:-

test:
  stage: promote-test-reports
  image: "937583223412.dkr.ecr.us-east-1.amazonaws.com/core-infra/linux:centos7"
  before_script:
     - "yum install unzip -y"
  variables:
    GITLAB_PRIVATE_TOKEN: $GITLAB_TOKEN
  allow_failure: true
  script:
  - >
    #!/bin/bash
    cd $CI_PROJECT_DIR
    echo running
    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=$(echo "$JOBS" | jq '.[] | select((.name | inside(["test", "coverage"])) 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    
  when: always
  rules:
    - if: $CI_PIPELINE_SOURCE == 'web'
  artifacts:
    reports:
      junit: "**/testresult.xml"
      coverage_report:
        coverage_format: cobertura
        path: "**/**/coverage.xml"
  coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/'

Error log:-

Complete!
$ #!/bin/bash cd $CI_PROJECT_DIR echo running 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=$(echo "$JOBS" | jq '.[] | select((.name | inside(["test", "coverage"])) and .artifacts_file != null) | .id')
[[ -z "$JOBS" ]] && echo "No jobs in $cp" && continue echo "$JOBS" | while read job # collapsed multi-line command
/scripts-36531461-3838222101/step_script: eval: line 177: syntax error near unexpected token `do'

The while loop is throwing error (syntax error near unexpected token `do') for some reason which I'm not able to figure it out i.e.

echo "$JOBS" | while read job
do


I tried removing the pipe '|' but no vein. Must be some silly mistake I'm doing. Any idea?

<details>
<summary>英文:</summary>

I&#39;m trying to put a multi-line shell script command in the GitLab CI YAML file as shown below:-

    test:
      stage: promote-test-reports
      image: &quot;937583223412.dkr.ecr.us-east-1.amazonaws.com/core-infra/linux:centos7&quot;
      before_script:
         - &quot;yum install unzip -y&quot;
      variables:
        GITLAB_PRIVATE_TOKEN: $GITLAB_TOKEN
      allow_failure: true
      script:
      - &gt;
        #!/bin/bash
        cd $CI_PROJECT_DIR
        echo running
        set -o errexit -o pipefail -o nounset
        API=&quot;${CI_API_V4_URL}/projects/${CI_PROJECT_ID}&quot;
        AUTH_HEADER=&quot;PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}&quot;
        CHILD_PIPELINES=$(curl -sS --header &quot;${AUTH_HEADER}&quot; &quot;${API}/pipelines/${CI_PIPELINE_ID}/bridges&quot;)
        echo &quot;$CHILD_PIPELINES&quot; | jq . &gt; bridges-$CI_PIPELINE_ID.json
        CHILD_PIPELINES=$(echo $CHILD_PIPELINES | jq &quot;.[].downstream_pipeline.id&quot;)  
        echo &quot;$CHILD_PIPELINES&quot; | while read cp  
        do
        # Fetch the IDs of their &quot;build:*&quot; jobs that completed successfully
        JOBS=$(curl -sS --header &quot;${AUTH_HEADER}&quot; &quot;${API}/pipelines/$cp/jobs?scope=success&quot;)
        echo &quot;$JOBS&quot; | jq . &gt;&gt; job-$cp.json
        JOBS=$(echo &quot;$JOBS&quot; | jq &#39;.[] | select(([.name] | inside([&quot;test&quot;, &quot;coverage&quot;])) and .artifacts_file != null) | .id&#39;)
        [[ -z &quot;$JOBS&quot; ]] &amp;&amp; echo &quot;No jobs in $cp&quot; &amp;&amp; continue
        echo &quot;$JOBS&quot; | while read job
          do
            echo &#39;DOWNLOADING ARTIFACT: $job&#39;
            curl -sS -L --header &#39;${AUTH_HEADER}&#39; --output artifacts-$job.zip &#39;${API}/jobs/$job/artifacts&#39;
          done
        done  
        if ls artifacts-*.zip &gt;/dev/null
        then
          unzip -o artifacts-\*.zip
        else
          echo &quot;No artifacts&quot;
        fi
      when: always
      rules:
        - if: $CI_PIPELINE_SOURCE == &#39;web&#39;
      artifacts:
        reports:
          junit: &quot;**/testresult.xml&quot;
          coverage_report:
            coverage_format: cobertura
            path: &quot;**/**/coverage.xml&quot;
      coverage: &#39;/TOTAL\s+\d+\s+\d+\s+(\d+%)/&#39;

Error log:-

    Complete!
    $ #!/bin/bash cd $CI_PROJECT_DIR echo running set -o errexit -o pipefail -o nounset API=&quot;${CI_API_V4_URL}/projects/${CI_PROJECT_ID}&quot; AUTH_HEADER=&quot;PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}&quot; CHILD_PIPELINES=$(curl -sS --header &quot;${AUTH_HEADER}&quot; &quot;${API}/pipelines/${CI_PIPELINE_ID}/bridges&quot;) echo &quot;$CHILD_PIPELINES&quot; | jq . &gt; bridges-$CI_PIPELINE_ID.json CHILD_PIPELINES=$(echo $CHILD_PIPELINES | jq &quot;.[].downstream_pipeline.id&quot;)   echo &quot;$CHILD_PIPELINES&quot; | while read cp   do # Fetch the IDs of their &quot;build:*&quot; jobs that completed successfully JOBS=$(curl -sS --header &quot;${AUTH_HEADER}&quot; &quot;${API}/pipelines/$cp/jobs?scope=success&quot;) echo &quot;$JOBS&quot; | jq . &gt;&gt; job-$cp.json JOBS=$(echo &quot;$JOBS&quot; | jq &#39;.[] | select(([.name] | inside([&quot;test&quot;, &quot;coverage&quot;])) and .artifacts_file != null) | .id&#39;) [[ -z &quot;$JOBS&quot; ]] &amp;&amp; echo &quot;No jobs in $cp&quot; &amp;&amp; continue echo &quot;$JOBS&quot; | while read job # collapsed multi-line command
    /scripts-36531461-3838222101/step_script: eval: line 177: syntax error near unexpected token `do&#39;

The while loop is throwing error (**syntax error near unexpected token `do&#39;**) for some reason which I&#39;m not able to figure it out i.e. 

echo &quot;$JOBS&quot; | while read job
      do

I tried removing the pipe &#39;|&#39; but no vein. Must be some silly mistake I&#39;m doing. Any idea?

</details>


# 答案1
**得分**: 0

终于,能够找出问题出在 API 调用上。

<details>
<summary>英文:</summary>

Finally, able to figure it out that the problem was with the API calls.

</details>



huangapple
  • 本文由 发表于 2023年2月27日 17:39:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定