Search code examples
azure-devopsyamldatabricksdatabricks-connectdatabricks-cli

How to store Databricks token created from CLI in Yaml Bash step


I have the following Yaml script. I am looking for how to grab the token created and store into a variable:

    - bash: |
        echo {} > ~/.databricks-connect#
        source py37-venv/bin/activate
        pip3 install wheel
        pip3 install databricks-cli
      displayName: Install Databricks CLI
    - bash: |
        source py37-venv/bin/activate
        databricks configure --token <<EOF
        ${DATABRICKS_HOST}
        ${DATABRICKS_AAD_TOKEN}
        EOF
        databricks tokens create --lifetime-seconds 129600 --comment "My comment."

The response that the above command returns is this json:

{
  "token_value": "dapi1a23b45678901cd2e3fa4bcde56f7890",
  "token_info": {
    "token_id": "1ab23cd45678e90123f4567abc8d9e012345fa67890123b45678cde90fa123b4",
    "creation_time": 1621287738473,
    "expiry_time": 1621417338473,
    "comment": "My comment."
  }
}

I want to store the value of token_value above so I can use it in another task below.


Solution

  • You can use jq to parse the response json to get token value, for example:

    token=$(databricks tokens create --lifetime-seconds 129600 --comment "My comment." | jq .token_value --raw-output)
    

    Set $token as variable with logging command(you can set it as secret or not,click the link to check the usage), then use it in next job($(setvar.databrickstoken)).

    echo "##vso[task.setvariable variable=databrickstoken;issecret=true;isoutput=true]$token"