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
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:-
Output:-
I think your sign-in issue would be solved by simply adding the following to your
AzureCLI@2
task:Using Personal Access Token (PAT)
Using predefined variable
System.AccessToken
Example full code
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 asaz devops login
command.Please let me know how it goes, and good luck!