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

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

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:-

  1. test:
  2. stage: promote-test-reports
  3. image: "937583223412.dkr.ecr.us-east-1.amazonaws.com/core-infra/linux:centos7"
  4. before_script:
  5. - "yum install unzip -y"
  6. variables:
  7. GITLAB_PRIVATE_TOKEN: $GITLAB_TOKEN
  8. allow_failure: true
  9. script:
  10. - >
  11. #!/bin/bash
  12. cd $CI_PROJECT_DIR
  13. echo running
  14. set -o errexit -o pipefail -o nounset
  15. API="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}"
  16. AUTH_HEADER="PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}"
  17. CHILD_PIPELINES=$(curl -sS --header "${AUTH_HEADER}" "${API}/pipelines/${CI_PIPELINE_ID}/bridges")
  18. echo "$CHILD_PIPELINES" | jq . > bridges-$CI_PIPELINE_ID.json
  19. CHILD_PIPELINES=$(echo $CHILD_PIPELINES | jq ".[].downstream_pipeline.id")
  20. echo "$CHILD_PIPELINES" | while read cp
  21. do
  22. # Fetch the IDs of their "build:*" jobs that completed successfully
  23. JOBS=$(curl -sS --header "${AUTH_HEADER}" "${API}/pipelines/$cp/jobs?scope=success")
  24. echo "$JOBS" | jq . >> job-$cp.json
  25. JOBS=$(echo "$JOBS" | jq '.[] | select((.name | inside(["test", "coverage"])) and .artifacts_file != null) | .id')
  26. [[ -z "$JOBS" ]] && echo "No jobs in $cp" && continue
  27. echo "$JOBS" | while read job
  28. do
  29. echo 'DOWNLOADING ARTIFACT: $job'
  30. curl -sS -L --header '${AUTH_HEADER}' --output artifacts-$job.zip '${API}/jobs/$job/artifacts'
  31. done
  32. done
  33. if ls artifacts-*.zip >/dev/null
  34. then
  35. unzip -o artifacts-*.zip
  36. else
  37. echo "No artifacts"
  38. fi
  39. when: always
  40. rules:
  41. - if: $CI_PIPELINE_SOURCE == 'web'
  42. artifacts:
  43. reports:
  44. junit: "**/testresult.xml"
  45. coverage_report:
  46. coverage_format: cobertura
  47. path: "**/**/coverage.xml"
  48. coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/'

Error log:-

  1. Complete!
  2. $ #!/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')
  3. [[ -z "$JOBS" ]] && echo "No jobs in $cp" && continue echo "$JOBS" | while read job # collapsed multi-line command
  4. /scripts-36531461-3838222101/step_script: eval: line 177: syntax error near unexpected token `do'
  5. 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

  1. I tried removing the pipe '|' but no vein. Must be some silly mistake I'm doing. Any idea?
  2. <details>
  3. <summary>英文:</summary>
  4. I&#39;m trying to put a multi-line shell script command in the GitLab CI YAML file as shown below:-
  5. test:
  6. stage: promote-test-reports
  7. image: &quot;937583223412.dkr.ecr.us-east-1.amazonaws.com/core-infra/linux:centos7&quot;
  8. before_script:
  9. - &quot;yum install unzip -y&quot;
  10. variables:
  11. GITLAB_PRIVATE_TOKEN: $GITLAB_TOKEN
  12. allow_failure: true
  13. script:
  14. - &gt;
  15. #!/bin/bash
  16. cd $CI_PROJECT_DIR
  17. echo running
  18. set -o errexit -o pipefail -o nounset
  19. API=&quot;${CI_API_V4_URL}/projects/${CI_PROJECT_ID}&quot;
  20. AUTH_HEADER=&quot;PRIVATE-TOKEN: ${GITLAB_PRIVATE_TOKEN}&quot;
  21. CHILD_PIPELINES=$(curl -sS --header &quot;${AUTH_HEADER}&quot; &quot;${API}/pipelines/${CI_PIPELINE_ID}/bridges&quot;)
  22. echo &quot;$CHILD_PIPELINES&quot; | jq . &gt; bridges-$CI_PIPELINE_ID.json
  23. CHILD_PIPELINES=$(echo $CHILD_PIPELINES | jq &quot;.[].downstream_pipeline.id&quot;)
  24. echo &quot;$CHILD_PIPELINES&quot; | while read cp
  25. do
  26. # Fetch the IDs of their &quot;build:*&quot; jobs that completed successfully
  27. JOBS=$(curl -sS --header &quot;${AUTH_HEADER}&quot; &quot;${API}/pipelines/$cp/jobs?scope=success&quot;)
  28. echo &quot;$JOBS&quot; | jq . &gt;&gt; job-$cp.json
  29. JOBS=$(echo &quot;$JOBS&quot; | jq &#39;.[] | select(([.name] | inside([&quot;test&quot;, &quot;coverage&quot;])) and .artifacts_file != null) | .id&#39;)
  30. [[ -z &quot;$JOBS&quot; ]] &amp;&amp; echo &quot;No jobs in $cp&quot; &amp;&amp; continue
  31. echo &quot;$JOBS&quot; | while read job
  32. do
  33. echo &#39;DOWNLOADING ARTIFACT: $job&#39;
  34. curl -sS -L --header &#39;${AUTH_HEADER}&#39; --output artifacts-$job.zip &#39;${API}/jobs/$job/artifacts&#39;
  35. done
  36. done
  37. if ls artifacts-*.zip &gt;/dev/null
  38. then
  39. unzip -o artifacts-\*.zip
  40. else
  41. echo &quot;No artifacts&quot;
  42. fi
  43. when: always
  44. rules:
  45. - if: $CI_PIPELINE_SOURCE == &#39;web&#39;
  46. artifacts:
  47. reports:
  48. junit: &quot;**/testresult.xml&quot;
  49. coverage_report:
  50. coverage_format: cobertura
  51. path: &quot;**/**/coverage.xml&quot;
  52. coverage: &#39;/TOTAL\s+\d+\s+\d+\s+(\d+%)/&#39;
  53. Error log:-
  54. Complete!
  55. $ #!/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
  56. /scripts-36531461-3838222101/step_script: eval: line 177: syntax error near unexpected token `do&#39;
  57. 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.
  58. echo &quot;$JOBS&quot; | while read job
  59. do
  60. I tried removing the pipe &#39;|&#39; but no vein. Must be some silly mistake I&#39;m doing. Any idea?
  61. </details>
  62. # 答案1
  63. **得分**: 0
  64. 终于,能够找出问题出在 API 调用上。
  65. <details>
  66. <summary>英文:</summary>
  67. Finally, able to figure it out that the problem was with the API calls.
  68. </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:

确定