2
votes

I want to execute AZ cli commands from my Azure DevOps Pipeline. In my YAML file I have this:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.x'
    architecture: 'x64'

# Updating pip to latest
- script: python -m pip install --upgrade pip
  displayName: 'Upgrade pip'

# Updating to latest Azure CLI version.
- script: pip install --pre azure-cli --extra-index-url https://azurecliprod.blob.core.windows.net/edge
  displayName: 'upgrade azure cli'

- script: az --version
  displayName: 'Show Azure CLI version'

- script: az extension add -n azure-devops
  displayName: 'Install Azure DevOps Extension'

- script: echo ${AZURE_DEVOPS_CLI_PAT} | az devops login
  env:
    AZURE_DEVOPS_CLI_PAT: $(System.AccessToken)
  displayName: 'Login Azure DevOps Extension'

- script: az aks show --name census-k8s  --resource-group Census
  displayName: 'Show AKS'

The echo ${AZURE_DEVOPS_CLI_PAT} | az devops login step is completed (with success apparently) with a warning message

Failed to store PAT using keyring; falling back to file storage.
You can clear the stored credential by running az devops logout.
Refer https://aka.ms/azure-devops-cli-auth to know more on sign in with PAT.

The az aks show step fails:

Please run 'az login' to setup account.

I am a little bit lost. The az devops login command should enable me to use the az cli, right? If not, Am I supposed to use az login instead of az devops login? And if I am supposed to use az login, how can I pass my credentials in a secure way?

3

3 Answers

8
votes

No, you don't need az devops login. What you need is Azure CLI Task:

- task: AzureCLI@2
  displayName: Azure CLI
  inputs:
    azureSubscription: <Name of the Azure Resource Manager service connection>
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      az --version
      az account show

but then you don't have to do any login. Please call there your az aks show --name census-k8s --resource-group Census

0
votes

Just to Add to Krzysztof's answer (and jeromerg question in the comment): in Azure CLI step you can also use other tools then az, which require being logged in with AzureCLI:

- task: AzureCLI@2
  displayName: Publish Function
  inputs:
    azureSubscription: <Name of the Azure Resource Manager service connection>
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      func azure publish <function-name>
0
votes

If your scriptLocation is a scriptPath use the following example

  - task: AzureCLI@2
    displayName: 'update function appsettings'
    inputs:
    azureSubscription: 'MY-AzureSubscriptionName'
    scriptType: ps
    scriptLocation: 'scriptPath'
    scriptPath: '$(System.DefaultWorkingDirectory)/Scripts/updateSettings.ps1'
    arguments:
        -ResourceGroupName 'MY-ResourceGroupName' `
        -FunctionAppName 'MY-FunctionAppName'

updateSettings.ps1

param (
    [string]$ResourceGroupName,
    [string]$FunctionAppName)
)
.
. script body here
.