簡體   English   中英

運行 Azure CLI 命令的單引號問題

[英]Issue with single quotes running Azure CLI command

我的腳本片段如下:

最終目標是創建一個 Azure DevOps 變量組並將另一個變量組的鍵值注入其中(新創建的 Azure DevOps 變量組)

set -x
echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | az devops login --organization https://dev.azure.com/tempcompenergydev
az devops configure --defaults organization=https://dev.azure.com/tempcompenergydev project=Discovery
export target_backend=automation
export target_backend="tempcomp.Sales.Configuration.Spa ${target_backend}"
export new_env="abc"

values=(addressSearchBaseUrl addressSearchSubscriptionKey cacheUrl calendarApiUrl checkoutBffApiUrl cpCode)

az_create_options=""

for ptr in "${values[@]}"
do
    result=$(
      az pipelines variable-group list --group-name "${target_backend}" | jq '.[0].variables.'${ptr}'.value'
        
    )
    printf "%s\t%s\t%d\n" "$ptr" "$result" $?

    # add the variable and value to the array
    az_create_options="${az_create_options} ${ptr}=${result}"
done

az pipelines variable-group create \
    --name "test ${new_env}" \
  --variables "${az_create_options}"

但是,當執行上述命令時,我得到一個意外的 output 如下:

+ az pipelines variable-group create --name 'test abc' --variables ' addressSearchBaseUrl="https://qtruat-api.platform.tempcomp.com.au/shared" addressSearchSubscriptionKey="xxxxxxxxxxxxxxxxxxx" cacheUrl="https://tempcompdstqtruat.digital.tempcomp.com.au/app/config" calendarApiUrl="https://qtruat-api.platform.tempcomp.com.au/sales/calendar/v1;rev=deadpool/AvailableDates/EnergyMovers/" checkoutBffApiUrl="https://qtruat-api.platform.tempcomp.com.au/sales/checkout-experience/v1;rev=deadpool/" cpCode="1067076"'
 cpCode "1067076"   0
 WARNING: Command group 'pipelines variable-group' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
 {
   "authorized": false,
   "description": null,
   "id": 1572,
   "name": "test abc",
   "providerData": null,
   "type": "Vsts",
   "variables": {
     "addressSearchBaseUrl": {
       "isSecret": null,
       "value": "\"https://qtruat-api.platform.tempcomp.com.au/shared\" addressSearchSubscriptionKey=\"xxxxxxxxxxxxxxxxxxxxxxxxx\" cacheUrl=\"https://tempcompdstqtruat.digital.tempcomp.com.au/app/config\" calendarApiUrl=\"https://qtruat-api.platform.tempcomp.com.au/sales/calendar/v1;rev=deadpool/AvailableDates/EnergyMovers/\" checkoutBffApiUrl=\"https://qtruat-api.platform.tempcomp.com.au/sales/checkout-experience/v1;rev=deadpool/\" cpCode=\"1067076\""
     }
   }
 }
 ##[section]Finishing: Bash Script

附帶說明一下,如果我手動運行,我會得到正確的響應。 下面的例子:

az pipelines variable-group create --name "test abc" --variables addressSearchBaseUr="https://qtruat-api.platform.tempcomp.com.au/shared"  addressSearchSubscriptionKey="xxxxxxxxxxxxxxxxxxxxxxxxx"  cacheUrl="https://tempcompdstqtruat.digital.tempcomp.com.au/app/config"  calendarApiUrl="https://qtruat-api.platform.tempcomp.com.au/sales/calendar/v1;rev=deadpool/AvailableDates/EnergyMovers/"  checkoutBffApiUrl="https://qtruat-api.platform.tempcomp.com.au/sales/checkout-experience/v1;rev=deadpool/"  cpCode="1067076"

Output:

 + az pipelines variable-group create --name 'test abc' --variables addressSearchBaseUr=https://qtruat-api.platform.tempcomp.com.au/shared addressSearchSubscriptionKey=xxxxxxxxxxxxxxxxxxx cacheUrl=https://tempcompdstqtruat.digital.tempcomp.com.au/app/config 'calendarApiUrl=https://qtruat-api.platform.tempcomp.com.au/sales/calendar/v1;rev=deadpool/AvailableDates/EnergyMovers/' 'checkoutBffApiUrl=https://qtruat-api.platform.tempcomp.com.au/sales/checkout-experience/v1;rev=deadpool/' cpCode=1067076
 WARNING: Command group 'pipelines variable-group' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
 {
   "authorized": false,
   "description": null,
   "id": 1573,
   "name": "test abc",
   "providerData": null,
   "type": "Vsts",
   "variables": {
     "addressSearchBaseUr": {
       "isSecret": null,
       "value": "https://qtruat-api.platform.tempcomp.com.au/shared"
     },
     "addressSearchSubscriptionKey": {
       "isSecret": null,
       "value": "xxxxxxxxxx"
     },
     "cacheUrl": {
       "isSecret": null,
       "value": "https://tempcompdstqtruat.digital.tempcomp.com.au/app/config"
     },
     "calendarApiUrl": {
       "isSecret": null,
       "value": "https://qtruat-api.platform.tempcomp.com.au/sales/calendar/v1;rev=deadpool/AvailableDates/EnergyMovers/"
     },
     "checkoutBffApiUrl": {
       "isSecret": null,
       "value": "https://qtruat-api.platform.tempcomp.com.au/sales/checkout-experience/v1;rev=deadpool/"
     },
     "cpCode": {
       "isSecret": null,
       "value": "1067076"
     }
   }
 }
 ##[section]Finishing: Bash Script

解決問題

我不是 bash 專業人士,但我找到了適合您的解決方案。 我認為問題的核心在於,當您在那里打印出數組時,它會將 --variables arguments 的集合視為一個長字符串。 所以你得到這個...

az pipelines variable-group create --name "test ${new_env}" --variables ' addressSearchBaseUrl="val" addressSearchSubscriptionKey="val" ...'

而不是這個...

az pipelines variable-group create --name "test ${new_env}" --variables addressSearchBaseUrl="val" addressSearchSubscriptionKey="val" ...

不好:我認為您可以使用eval而不是打印出 arguments 來解決這個問題,但建議不要在幾乎任何情況下使用eval

好:相反,我認為這個問題可以用不同的方式解決。 該腳本不會一次創建所有變量的批處理,而是一次將所需的變量復制到一個新組:

set -x
echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | az devops login --organization https://dev.azure.com/tempcompenergydev
az devops configure --defaults organization=https://dev.azure.com/tempcompenergydev project=Discovery
export target_backend=automation
export target_backend="tempcomp.Sales.Configuration.Spa ${target_backend}"
export new_env="abc"

values=(addressSearchBaseUrl addressSearchSubscriptionKey cacheUrl calendarApiUrl checkoutBffApiUrl cpCode)

az_create_options=()

for ptr in "${values[@]}"
do
   result=$( az pipelines variable-group list --group-name "${target_backend}" | jq '.[0].variables.'${ptr}'.value' )
   echo "$result"
   # Result from this az command always wraps the response in double quotes. They should be removed.
   stripQuote=$( echo $result | sed 's/^.//;s/.$//' )
   printf "%s\t%s\t%d\n" "$ptr" "$stripQuote" $?
  
   # add the variable and value to the array
   # When adding this way you ensure each key|value pair gets added as a separate element
   az_create_options=("${az_create_options[@]}" "${ptr}|${stripQuote}")
   # ^ Note on this: The | is used because there needs to be a way to separate
   #   the key from the value. What you're really trying to do here is an
   #   Associative Array, but not all versions of bash support that out of the
   #   box. This is a bit of a hack to get the same result. CAUTION if your
   #   variable value contains a | character in it, it will cause problems
   #   during the copy.
done
 
# Create the new group, save the group id (would be best to add a check to see if the group by this name exists or not first)
groupId=$( az pipelines variable-group create  --name "test ${new_env}"  --variables bootstrap="start" | jq '.id' )
 
 
for var in "${az_create_options[@]}"
do
   # Split the key|value pair at the | character and use the set to create the new variable in the new group
   # Some check could also be added here to see if the variable already exists in the new group or not if you want to use this create or update an existing group
   arrVar=(${var//|/ })
   echo "Parsing variable ${arrVar[0]} with val of ${arrVar[1]}"
   az pipelines variable-group variable create \
       --id "${groupId}" \
       --name "${arrVar[0]}" \
       --value "${arrVar[1]}"
done
 
# This is needed cause the az command is dumb and won't let you create a group in the first place without a --variables argument. So I had it create a dummy var and then delete it
az pipelines variable-group variable delete --id "${groupId}" --name "bootstrap" --yes

呼出注意事項

  1. 如果可能,我建議使用關聯數組而不是我在此處列出的數組。 但是,關聯 Arrays 僅在 bash v4 或更高版本中受支持。 使用bash --version查看您是否可以使用它們。 如果可以的話,請參閱本指南作為您可以使用的一些方法的示例。

  2. 如果使用我的方法,請注意任何| 正在復制的變量值中的字符將導致腳本失敗。 您可能需要選擇不同的分隔符進行拆分。

  3. az pipelines variable-group list命令的 output 將給出包裝在"中的變量的值。如果您嘗試轉身並在變量創建命令中拋出從列表中獲得的確切結果,您將得到一堆您組中的變量,其值如下

    { "variable": { "isSecret": null, "value": "\”value\”" } }

    代替

    { "variable": { "isSecret": null, "value": "value" } }
  4. az 命令是愚蠢的,並且不會讓您在沒有 --variables 參數的情況下首先創建新的變量組。 所以我讓它創建一個虛擬 var bootstrap="start"然后刪除它。

  5. 正如我所提到的,可能有更好的方法來打印出我缺少的數組。 如果您知道更好的方法,鼓勵對我的帖子發表評論。

這可能會有所幫助....

關於 Azure 托管代理如何使用 az-pipelines 命令向 ADO 進行身份驗證,存在一個未解決的問題。

感覺這是相關的,但如果不能隨意回復,我會刪除答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM