skip to Main Content

I am using below YAML pipeline to create a PR via Azure CLI script:-

  - task: AzureCLI@2
    inputs:
      azureSubscription: '${{ parameters.serviceConnection }}'
      scriptType: 'ps'
      scriptLocation: 'inlineScript'
      inlineScript: |
        # Set organization, project, and repository details
        $organization="https://dev.azure.com/MyOrg"
        $project="MyProject"
        $repository="MyRepo"
        $targetBranch = "refs/heads/dev"
        $sourceBranch = "refs/heads/feature/myfeature"

        # Set the pull request title and description
        $prTitle="Automated Pull Request"
        $prDescription="Auto-generated pull request for changes."

        # Create a pull request using az repos pr create
        az repos pr create --organization $organization --project $project --repository $repository --source-branch $sourceBranch --target-branch $targetBranch --title "$prTitle" --description "$prDescription"

I am receiving the error below:-

# ["ERROR: TF401444: Please sign-in at least once..." in AzureCLI@2 task](https://stackoverflow.com/questions/77663812/error-tf401444-please-sign-in-at-least-once-in-azurecli2-task)

Expecting to not need to explicitly log in when the service connection is specified in the azureSubscription property on the task. I have verified the service connection exists.

2

Answers


  1. Interactive login will not work in Azure CLI task of DevOps pipeline.

    You need to make use of PAT token with az devops login to create PR in the Azure Repository:-

    My Azure DevOps yaml pipeline:-

    trigger:
    - main
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      organization: https://dev.azure.com/sid24desai0738
      project: AzureDevops
      repository: streamlitapp
      targetBranch: main
      sourceBranch: myfeature
      prTitle: Automated Pull Request
      prDescription: Auto-generated pull request for changes
      AZURE_DEVOPS_EXT_PAT: xep3dv726tvoxilxxxxxxx3h4yjnpb6vnyhbjq
    
    steps:
     - task: AzureCLI@2
       inputs:
         azureSubscription: 'xxx subscription (xxxxxx)'
         scriptType: 'bash'
         scriptLocation: 'inlineScript'
         inlineScript: |
           # Set Azure DevOps organization URL
                 #export AZURE_DEVOPS_EXT_PAT=xxxxxtbjz6wyryhl3h4yjnpb6vnyhbjq
           
                 # Azure CLI commands to interact with Azure DevOps
                 echo $(AZURE_DEVOPS_EXT_PAT) | az devops login --organization https://dev.azure.com/sid24desai0738
                   az repos pr create --org $(organization) --project $(project) --repository $(repository) --source-branch $(sourceBranch) --target-branch $(targetBranch) --title "$(prTitle)" --description "$prDescription"
    

    Output:-

    enter image description here

    enter image description here

    Login or Signup to reply.
  2. I think your sign-in issue would be solved by simply adding the following to your AzureCLI@2 task:

    Using Personal Access Token (PAT)

    env:
     AZURE_DEVOPS_EXT_PAT: $(AZURE_DEVOPS_EXT_PAT) # <-- This is your pipeline variable containing your PAT
    

    Using predefined variable System.AccessToken

    env:
     AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
    

    Example full code

      - task: AzureCLI@2
        env:
          AZURE_DEVOPS_EXT_PAT: $(AZURE_DEVOPS_EXT_PAT)
        inputs:
          azureSubscription: ${{ parameters.serviceConnection }}
          scriptType: ps
          scriptLocation: inlineScript
          inlineScript: |
            # Set organization, project, and repository details
            $organization="https://dev.azure.com/MyOrg"
            $project="MyProject"
            $repository="MyRepo"
            $targetBranch = "refs/heads/dev"
            $sourceBranch = "refs/heads/feature/myfeature"
    
            # Set the pull request title and description
            $prTitle="Automated Pull Request"
            $prDescription="Auto-generated pull request for changes."
    
            # Create a pull request using az repos pr create
            az repos pr create --organization $organization --project $project --repository $repository --source-branch $sourceBranch --target-branch $targetBranch --title "$prTitle" --description "$prDescription"
    

    If not mistaken, once added like this in your AzureCLI@2 it should default to using this environment variable for the authentication, and you should not have to sign-in using such as az devops login command.

    Please let me know how it goes, and good luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search