Search code examples
gitazure-pipelines

Lock an Azure devops git branch via pipeline


I am trying to lock a branch in one of my azure devops repository via azure devops pipeline but i get error below. Any idea about that error?

"ERROR: The controller for path '/obisvt/_apis' was not found or does not implement IController. Operation returned a 404 status code."

trigger: none

appendCommitMessageToRunName: false



pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.x'  # Required to use Azure CLI

- task: UseDotNet@2  # Ensures .NET is available if needed
  inputs:
    packageType: 'sdk'
    version: '6.0.302'  

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

- script: 
      az repos ref lock --repository "test" --org "https://dev.azure.com/test/project1" --project "OBISVT"  --name "main_test"

Solution

  • I can reproduce the error with your yaml. This is due to that the parameter of az repos ref lock is not correct.

    For --org, you should only use organization url, like https://dev.azure.com/orgname, remove extra project1.

    In addition, for --name, should be format like heads/my_branch, with heads/ before.

    Sample below:

    az repos ref lock --repository "reponame" --org "https://dev.azure.com/orgname" --project "projectname"  --name "heads/main_test"
    

    Please check the doc az repos ref lock for the command detials.

    Also, you are using $(system.accesstoken) to login devops, the identity points to build service identity, which could not have proper permission to lock target branch, it's recommended to create a Personal access token from user account.

    The complete yaml as below, please fix the value to yours:

    trigger: none
    appendCommitMessageToRunName: false
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.x'  # Required to use Azure CLI
    
    - task: UseDotNet@2  # Ensures .NET is available if needed
      inputs:
        packageType: 'sdk'
        version: '6.0.302'  
    
    # use $AZURE_DEVOPS_CLI_PAT for echo, and add --organization for login if it's not current organization.
    - bash: echo $AZURE_DEVOPS_CLI_PAT | az devops login
      env:
        AZURE_DEVOPS_CLI_PAT: $(pat)
      displayName: 'Login Azure DevOps Extension'
    
    - script: 
        az repos ref lock --repository "test" --org "https://dev.azure.com/orgname" --project "OBISVT"  --name "heads/main_test"
    

    enter image description here

    You can also remove login task, directly append the pat token for the script, it works too.

    - script: 
        az repos ref lock --repository "test" --org "https://dev.azure.com/orgname" --project "OBISVT"  --name "heads/main_test"
      env:
        AZURE_DEVOPS_EXT_PAT: $(pat)