【问题标题】:Azure DevOps Through curl or get task output value as new variableAzure DevOps 通过 curl 或获取任务输出值作为新变量
【发布时间】:2020-03-10 10:20:28
【问题描述】:

我需要通过 curl restAPI 并执行内容 grep 并剪切某些字符以获得新的令牌值作为变量 但是我不知道如何在变量中进行curl、grep、cut ...操作

在变量中做这些操作的逻辑可行吗?

EX.

- task: Bash@3
  displayName: GetToken
  inputs:
    targetType: 'inline'
    script: 
        token= curl -H $HEADER -D $DATA www.example.com | grep -oEi $pattern | cut -d ':' -f 2 | cut -d '"' -f 2
        echo "##vso[task.setvariable variable=token;]$token

或者我可以获取任务输出的值来设置一个新变量吗? 例如。

- task: Bash@3
  displayName: CreateToken
  inputs:
    targetType: 'inline'
    script: 
       curl --header $HEADER --data "{userKey:$USERKEY,orgToken:$ORGTOKEN,requestType:getAllProducts}" $API |grep -oEI "\"productName\":\"$PRODUCTNAME\",\"productToken\":\"[0-9a-f]*\"" | cut -d ':' -f 3 | cut -d '"' -f 2

Output
========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /home/vsts/work/_temp/a6f13e9c-2c45-4ac7-9674-42de3efe2503.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   147    0     0  100   147      0    158 --:--:-- --:--:-- --:--:--   158
100 13655    0 13508  100   147  12577    136  0:00:01  0:00:01 --:--:-- 12714

33234d4db39844cf8c73c54e398c44c248ab368f319a4af7b9646cb461fa60b9 //I want to get this value as new variable

- task: Bash@3
  displayName: GetCreateToken
  inputs:
    targetType: 'inline'
    script:
        Token= $CreateToken
        echo "##vso[task.setvariable variable=token;]$token

【问题讨论】:

    标签: bash azure-devops yaml azure-pipelines pipeline


    【解决方案1】:

    如果我理解正确,您的问题是关于如何将 shell 脚本的输出传递到变量以供其他任务使用? 如果是这样,您可以将问题标题更改为“Azure DevOps 管道 - 将 shell 脚本输出传递到变量”之类的内容,以帮助其他人找到答案。

    不管怎样,这样试试吧:

    - bash: Bash@3
      displayName: GetToken
      inputs:
        targetType: 'inline'
        script: 
            # Note the $() around the call of curl, grep and cut. If you want to assign the result of a call, then encapsulate it into $()
            token=$(curl -H $HEADER -D $DATA www.example.com | grep -oEi $pattern | cut -d ':' -f 2 | cut -d '"' -f 2)
            echo "##vso[task.setvariable variable=token;]$token"
    
      # Just a side note: This is the short-hand syntax for using the bash task
    - bash: |
        echo "$(token)" 
    

    另请参阅docs,了解如何在脚本任务中设置管道变量。
    如果你想在另一个作业中使用一个变量,那么语法有点不同,documented here:

    - job: A
      steps:
      - bash: |
          token=$(curl -H $HEADER -D $DATA www.example.com | grep -oEi $pattern | cut -d ':' -f 2 | cut -d '"' -f 2)
          echo "##vso[task.setvariable variable=token;isOutput=true]$token"
        displayName: GetToken
        name: gettoken # you have to give the task a name to be able to access it through dependencies object below
    
    - job: B
      dependsOn: A
      variables:
        token: $[ dependencies.A.outputs['gettoken.token'] ]
    

    你也可以看看How do I set a variable to the output of a command in Bash?。

    【讨论】:

    • @eddy chi 只是一个友好的提醒,通过评论或接受anwser来回应。
    • 您能否对script 任务而不是bash 任务执行相同的操作?
    • @Stevy 当然。脚本任务将在 linux 构建代理上作为 Bash 执行,AFAIR 在 Windows 主机上使用 CMD。语法可能有点不同。不记得如何打印变量 CMD 样式。也许这个链接有帮助:docs.microsoft.com/en-us/azure/devops/pipelines/process/…
    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多