bash - 如何将curl输出保存在gitlab变量中?

标签 bash shell gitlab yaml gitlab-ci

对于一个项目,我们希望调用一个 API 并将此curl 的结果保存在一个变量中。

管道的构建如下:

stages:
  - download

scan:
  stage: download
  image: ubuntu
  variables:
    TOKEN: 
  
  script:

    - apk add curl
    - apk add jq
    
    - TOKEN=$('curl -H "Content-Type: application/json" -d "{\"username\":\"$USER", \"password\":\"$PWD"}" https://example.org/api2/authenticate | jq .token ')
    #- echo $TOKEN

我收到此错误:

此 GitLab CI 配置无效:jobs:scan:script 配置应该是字符串或最多 10 层深度的嵌套字符串数组。

curl 命令(从 $() 中删除,但保留单引号来包裹双引号)正常工作并返回带有 token 的字符串。唯一的问题是将结果封装在变量中。可以做什么?

谢谢。

最佳答案

尝试代替这个

- TOKEN=$('curl -H "Content-Type: application/json" -d "{\"username\":\"$USER", \"password\":\"$PWD"}" https://example.org/api2/authenticate | jq .token ')

以下内容

- |
  TOKEN=$(curl -H "Content-Type: application/json" -d "{\"username\":\"$USER\", \"password\":\"$PWD\"}" https://example.org/api2/authenticate | jq .token)

附注 我建议先运行

- |
  curl -H "Content-Type: application/json" -d "{\"username\":\"$USER\", \"password\":\"$PWD\"}" https://example.org/api2/authenticate 

为了调试curl命令的输出,在运行jq之前

关于bash - 如何将curl输出保存在gitlab变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71145924/

相关文章:

BASH - 在系统内部使用 awk 列

linux - !# :3? 是什么意思

linux - 并行运行两个 shell 脚本,然后在完成后运行另一个

git - 克隆或推送时询问密码 - docker gitlab

GitLab.com 下载计数器

linux - 如何获取大目录中特定文件的行数?

linux - 将 CSV 值导入 Bash

linux - 我想在字符串中嵌入单引号

linux - UNIX,更改所有 shell 脚本中的解释器行

postgresql - 在 Gitlab CI 上设置 postgis