eddy chi
eddy chi

Reputation: 53

Azure DevOps Through curl or get task output value as new variable

I need to go through the curl restAPI and do content grep and cut certain characters to get a new token value as a variable But I don't know how to make curl, grep, cut ... operations in the variables

Is the logic of doing these operations in variables feasible?

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

Or can I get the value of task output to set a new variable? ex.

- 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

Upvotes: 3

Views: 4748

Answers (1)

Michaelvsk
Michaelvsk

Reputation: 334

If I understand you correctly your question is regarding on how to pass the output of a shell script into a variable to be consumed by other tasks? If so you may change the question title into some like " Azure DevOps pipeline - passing shell script output into variables" to be more help other to find an anwser.

Anyway, try it this way:

- 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)" 

See also the docs on how to set pipeline variable in script tasks.
If you want to use a variable in another job, then the syntax is a bit different and 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'] ]

You may also have a look at How do I set a variable to the output of a command in Bash?.

Upvotes: 3

Related Questions