Search code examples
c#githubgithub-actionsazure-blob-storageazure-cli

Get connectionString value from az storage account show in github action


I want to get azure storage account blob connectionString via:

  - name: Azure CLI Create Blob Container
    uses: azure/CLI@v1
    with:
      azcliversion: 2.30.0
      inlineScript: |

        groupName=${{ env.AZURE_RESOURCE_GROUP_NAME }}
        blobName=${{ env.AZURE_BLOB_NAME }}
        connectionString=$(az storage account show-connection-string --name $blobName --resource-group $groupName --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }})

The returned result is

*** "connectionString": "DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=name;AccountKey=***" ***

Pay attention on *** in the beginning and the ending of the value (it looks like hidden { and }). How can I get only the value:

DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=name;AccountKey=***

(and ignore connectionString key and *** as })?

I've tried:

connectionStringValue=${connectionString#*:}

which trims *** and connectionString key from the beginning, but doesn't touch *** in the end. Also, I feel like it should be a better approach to get a value from the azure result, but not sure how to do it in github action. Any help would be appreaciated.


Solution

  • By default the output is a JSON object with a single key:

    {
      "connectionString": "DefaultEndpointsProtocol=https;EndpointSuffix"
    }
    

    You can use jq to extract the value of this key:

    connectionString=$(az storage account show-connection-string --name $blobName --resource-group $groupName --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }} | jq -r .connectionString)
    

    or you can request text output instead:

    connectionString=$(az storage account show-connection-string --name $blobName --resource-group $groupName --subscription ${{ secrets.AZURE_SUBSCRIPTION_ID }} --output tsv)