Search code examples
azure-devopsazure-cli

Azure Cli task in Azure Devops, cant output SAS token to pass it as variable in another task


Im trying to perform an azcopy command to copy everything from one storage account to another, however, as they are in different subscriptions, my guess was to generate a sas token for each one and then perform the action.

All of this is working fine, except the sas token is not passing to the other azure cli tasks. I even tried to use the output command to explicitly pass them but in the next taskt they are empty. Can someone help me to resolve this? I never experienced anything before.

Here´s the code:

- task: AzureCLI@2
  displayName: 'Obtain Development SAS'
  inputs:
    azureSubscription: $(developmentAzureServiceConnection)
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      set -e

      end=`date -u -d "5 minutes" '+%Y-%m-%dT%H:%M:00Z'`
      developmentConnectionString=$(az storage account show-connection-string --name $(developmentStorageAccountName) --resource-group "$(developmentResourceGroup)" --output tsv)
      echo "[DEBUG]: az storage container generate-sas --account-name $(developmentStorageAccountName) --name '$web' --permissions acdmrw --expiry $end --connection-string $developmentConnectionString"
      developmentSas=$(az storage container generate-sas --account-name $(developmentStorageAccountName) --name '$web' --permissions acdmrw --expiry $end --connection-string $developmentConnectionString)
      echo "[DEBUG]: developmentSas: $developmentSas"
      echo "##vso[task.setvariable variable=developmentSas]$developmentSas"

- task: AzureCLI@2
  displayName: 'Obtain Shared SAS'
  inputs:
    azureSubscription: $(sharedAzureServiceConnection)
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      set -e

      end=`date -u -d "5 minutes" '+%Y-%m-%dT%H:%M:00Z'`

      echo "[DEBUG]: developmentSas: $developmentSas"
      sharedConnectionString=$(az storage account show-connection-string --name $(sharedStorageAccountName) --resource-group "$(sharedResourceGroup)" --output tsv)
      echo "[DEBUG]: az storage container generate-sas --account-name $(sharedStorageAccountName) --name '$web' --permissions acdmrw --expiry $end --connection-string $sharedConnectionString"
      sharedSas=$(az storage container generate-sas --account-name $(sharedStorageAccountName) --name '$web' --permissions acdmrw --expiry $end --connection-string $sharedConnectionString)
      echo "[DEBUG]: sharedSas: $sharedSas"
      echo "##vso[task.setvariable variable=sharedSas;isOutput=false]$sharedSas"

- task: AzureCLI@2
  displayName: 'Copy $(developmentStorageAccountName) contents into $(sharedStorageAccountName)'
  inputs:
    azureSubscription: $(developmentAzureServiceConnection)
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      set -e

      echo "[DEBUG]: developmentSas: $developmentSas"
      echo "[DEBUG]: sharedSas: $sharedSas"

      # Upload to blob storage
      echo "azcopy copy https://$(developmentStorageAccountName).blob.core.windows.net/?$developmentSas https://$(sharedStorageAccountName).blob.core.windows.net/?$sharedSas --recursive"
      azcopy copy "https://$(developmentStorageAccountName).blob.core.windows.net/?$developmentSas" "https://$(sharedStorageAccountName).blob.core.windows.net/?$sharedSas" --recursive

this returns with the echos of the sas tokens empty for the azcopy task

[DEBUG]: developmentSas: 
[DEBUG]: sharedSas: 

I tried with and without echo "##vso[task.setvariable variable=developmentSas]$developmentSas" but it is not passing between tasks.


Solution

  • You need to reference the environment variable or pipeline variable in your next step, you are not doing either. Sample YAML code:

    steps:
    - bash: |
        sharedSas=my-shared-sas
        echo "##vso[task.setvariable variable=sharedSas]$sharedSas"
    
    - bash: |
        # An environment variable called 'SHAREDSAS' can be used in the downstream steps
        echo "Environment variable: $SHAREDSAS"
        # pipeline variables needs to be referenced using $()
        echo "Pipeline variable: $(sharedSas)"
        echo "I won't be evaluated: $sharedSas"
    

    Demo run:

    enter image description here

    See the docs for details