0
votes

I'm trying to get Jenkins CSRF crumb to call the job from Rest Api in Azure DevOps pipeline, but I can't assign the output of curl to a variable to pass to the next task.

So first thing I tried was to create a variable in a variable group and assign the output to it, but seems bash task doesn't get this - it gave exit code 127.

jenkins.crumb is a variable from the group.

$(jenkins.crumb)=$(curl -u $(jenkins.user):$(jenkins.token) -s '$(jenkins.url)/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')

Second thing I tried is setvariable in inline command like below, but it doesn't give an error, but no value is assigned. (temp, crumb are all empty in the log.)

variables:
- group: Myvariables-group

  pool:
    vmImage: 'ubuntu'

  steps:
  - task: Bash@3
    name: GetCSRFcrumb
    inputs:
      targetType: 'inline'
      script: |
        temp=$(curl -u $JENKINS_USER:$JENKINS_TOKEN -s '$JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
        echo "##vso[task.setvariable variable=crumb]$temp"
        echo "$temp"
        echo $temp

  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        echo "$(crumb)"
        echo "$CRUMB"

I'd like to avoid creating shell script file or curl output as a file. (maybe last resort if there is no other way.)

1
If $temp is also empty so the issue is in the curl, did you check that your get an output? - Shayki Abramczyk
@ShaykiAbramczyk Thank. I just tried and found it seems variable issue. My variables are set to jenkins.user, jenkins.token and jenkins.url, so I used $JENKINS_USER as the document suggests as bash env, but there was no error at all, but empty result in that line. Now I just tried $(jenkins.user) which is a direct reference, then it looks working as it is. - kevmando
More precisely, It seems secret variable (jenkins.token) doesn't look like converted to bash env. That's the only thing I needed to change to a direct reference. - kevmando

1 Answers

1
votes

So the root cause was the token which was a secret variable. Once I changed it to map directly, then curl returns the result.

Unlike a normal variable, they are not automatically decrypted into environment variables for scripts. You can explicitly map them in, though.

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables