簡體   English   中英

cURL command to GitLab API in GitLab pipeline succeeds with [0 bytes data] when cURL contains variable

[英]cURL command to GitLab API in GitLab pipeline succeeds with [0 bytes data] when cURL contains variable

我有一個 GitLab 管道,它應該生成一個文件last_changes.txt ,其中包含myrepository目錄結構的校驗和,並將文件提交到myrepository的新分支。 myrepository是與正在運行的 gitlab 管道不同的存儲庫。

校驗和是用cksum databases/* | sort cksum databases/* | sort並存儲在變量中。 This variable is then submitted in the cURL command to the GitLab API to update an existing file in a repository ( https://docs.gitlab.com/ee/api/repository_files.html#update-existing-file-in-repository ) .

管道如下所示:

write-status:
  stage: post-build
  image: myrepo.domain.com/myimage
  script:
    - git clone --branch $CI_COMMIT_BRANCH https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.domain.com/project/myrepository.git
    - cd myrepository
    - |
      CHANGED_FILES=$(cksum databases/* | sort) 
      echo $CHANGED_FILES
      curl -v -w --request PUT --header 'PRIVATE-TOKEN: myPrivateToken' \
      --header "Content-Type: application/json" \
      --data "{\"branch\":\"newchanges\", \"start_branch\":\"main\", \"content\":\"${CHANGED_FILES}\", \"commit_message\":\"update file with checksum\"}" \
      "https://gitlab.domain.com/api/v4/projects/2808/repository/files/ressources%2Flast_changes1%2Etxt"    
  when: on_success

如果我從本地 Git Bash 執行命令,則提交有效並且正在創建分支。 如果我在 localhost 上執行存儲為 shell 腳本的命令,則提交也可以正常工作並且正在創建分支。 但是,如果在腳本部分的 GitLab 管道中執行相同的命令(如上面的代碼塊所示),則 cURL 命令成功並顯示 [0 bytes data] 但既沒有創建提交也沒有創建分支(輸出顯示在下圖)。 此故障似乎僅在 JSON 內容作為變量添加到 cURL 命令(包含變量的 cURL)中時發生。 如果 JSON 內容是 static 字符串,則一切正常。

此錯誤似乎發生在不同的 linux 分布中(在 GitLab 管道中使用 alpine 3.16 和 rhel 8 docker 圖像進行測試)。

有沒有辦法讓 GitLab 管道接受 cURL 命令中的變量?

GitLab 0字節數據的管道狀態

有趣的問題。

我設置了一個測試存儲庫,通過將 JSON-bash-escaping 替換為 IMO 更易於處理的表單來解決此問題。

旁注:如果我沒記錯的話,curl 命令中的-w需要一個參數,並且可能是問題的一部分。

commit_variable:
  stage: build
  script: - |
        CHANGED_FILES=$(cksum /usr/bin/* | sort)
        echo $CHANGED_FILES
        curl -v --request PUT --header "PRIVATE-TOKEN: ${TOKEN}" \
            -F "branch=main" \
            -F "content=${CHANGED_FILES}" \
            -F "commit_message=changed files as variable" \
            "https://gitlab.com/api/v4/projects/38716506/repository/files/as_variable%2Etxt"

作為附加測試,我將環境變量CHANGED_FILES替換為文件,但結果相同。

commit_file:
  stage: build
  script: - |
        cksum /usr/bin/* | sort > /tmp/changed_files.txt
        cat /tmp/changed_files.txt
        curl -v --request PUT --header "PRIVATE-TOKEN: ${TOKEN}" \
            -F "branch=main" \
            -F "content=</tmp/changed_files.txt" \
            -F "commit_message=changed files as file" \
            "https://gitlab.com/api/v4/projects/38716506/repository/files/as_file%2Etxt"

查看完整的.gitlab-ci.yml文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM