Search code examples
azure-devopsazure-pipelinescicdazure-pipelines-yaml

Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref in mvn release - Azure Pipeline


I get the following error output while running the Maven release plugin prepare step i.e. mvn release:clean release:prepare release:perform . Any suggestions on how to fix this issue?

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.3:prepare (default-cli) on project TEST_C: An error is occurred in the checkin process: Exception while executing SCM command. Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.3:prepare (default-cli) on project TEST_C: An error is occurred in the checkin process: Exception while executing SCM command.
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
        at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
        at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
        at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
        at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
        at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
        at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
        at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke (Method.java:568)

Updates:

After adding fetchDepth:0

I am getting another error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.3:prepare (default-cli) on project measures_connect: Unable to commit files
[ERROR] Provider message:
[ERROR] The git-commit command failed.
[ERROR] Command output:
[ERROR] Author identity unknown
[ERROR] 
[ERROR] *** Please tell me who you are.
[ERROR] 
[ERROR] Run
[ERROR] 
[ERROR]   git config --global user.email "[email protected]"
[ERROR]   git config --global user.name "Your Name"
[ERROR] 
[ERROR] to set your account's default identity.
[ERROR] Omit --global to set the identity only in this repository.
[ERROR] 
[ERROR] fatal: unable to auto-detect email address (got 'athulya_azpcontainer@3a075dbc4a82.(none)')
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.5.3:prepare (default-cli) on project measures_connect: Unable to commit files
Provider message:
The git-commit command failed.
Command output:
Author identity unknown

I tried to add git identity but getting the below error for the commands

      git config --global user.email $(build.RequestedForEmail)
      git config --global user.name $(build.RequestedFor)
      git pull origin $(Build.SourceBranch)

error: could not lock config file /home/jenkins/.gitconfig: Permission denied error: could not lock config file /home/jenkins/.gitconfig: Permission denied

Updates on 11/07/2024:

Please find the script:

parameters:
- name: 'skipRelease'
  type: boolean
  default: false

stages:
  - stage: ValidateGitCheckin
    jobs:
      - job: Validate
        steps:
          - script: |
              git config --add safe.directory $(Build.SourcesDirectory)
              
              if [ "${{ parameters.skipRelease }}" = "true" ]; then
                echo "SKIP_RELEASE: Inherit parameter"
                echo "##vso[task.setvariable variable=skipRelease;isoutput=true]true"
              fi
            displayName: "Validate git checkin"

  - stage: SetupMaven
    jobs:
      - job: DownloadMaven
        steps:
          - task: DownloadSecureFile@1 
            name: settingsxml
            inputs:
              secureFile: "settings.xml"
          - script: |
              mkdir -p $(System.DefaultWorkingDirectory)/.m2
              cp $(settingsxml.secureFilePath) $(System.DefaultWorkingDirectory)/.m2/settings.xml
            displayName: "Setup Maven settings.xml"
            
          - task: PublishPipelineArtifact@1
            inputs:
              path: $(System.DefaultWorkingDirectory)/.m2
              artifact: m2
            displayName: "Download and store Maven settings.xml to artifact"


  - stage: Release
    jobs:
      - job: ReleaseJob
        container: dockerContainer
        steps:
          - checkout: self
            fetchDepth: 0
            persistCredentials: true
          - script: |

              git config --local user.email "$(build.RequestedForEmail)"
              git config --local user.name "$(build.RequestedFor)"
              git status
              if ! git diff-index --quiet HEAD --; then
                echo "There are uncommitted changes"
                exit 1
              fi
            displayName: 'Added git identity'
          - task: DownloadPipelineArtifact@2
            inputs:
              artifact: m2
              path: $(System.DefaultWorkingDirectory)/.m2
          - task: Maven@3
            displayName: 'Release'
            inputs:
              mavenPomFile: '$(System.DefaultWorkingDirectory)/pom.xml'
              options: '-B -X -s $(System.DefaultWorkingDirectory)/.m2/settings.xml'    
              goals: 'release:clean release:prepare release:perform'

Updated the git identity script as below:

git config --local user.email "$emailId"
git config --local user.name "$(build.RequestedFor)"
git checkout feature/$(Build.SourceBranchName)

Solution

  • failed: fatal: ref HEAD is not a symbolic ref

    From the error message, it seems that Pipeline does not checkout the entire branch.

    By default, the Shallow fetch of the pipeline repo is 1 by default.

    In this case, the ref will target to a single commit.

    For example:

    enter image description here

    You can try to set the fetchDepth to 0 in YAML Pipeline.

    For example:

    YAML Pipeline

    steps:
        - checkout: self
          fetchDepth: 0
    

    Or you can navigate to YAML Pipeline -> ... -> Triggers -> YAML -> Get sources -> Shallow fetch. You can unselect the option.

    enter image description here

    enter image description here

    Classic Pipeline:

    enter image description here

    For more detailed info, you can refer to this doc: Shallow fetch

    Update:

    fatal: unable to auto-detect email address

    To solve this issue, you need to add the following script before the maven command:

       git config --global user.email "youremail"
       git config --global user.name "yourname"
    

    And you can set persistCredentials: true in the checkout step to make sure that the git credential is correct.

    YAML sample:

    steps:
    - checkout: self
      fetchDepth: 0
      persistCredentials: true
    
    - script: |
       git config --global user.email "youremail"
       git config --global user.name "yourname"
       mvn release:prepare
    

    Update2:

    how can i generalize the git identity script to all type of branches like bugfix/feature/enhancement.

    There is no out-of-box variable can directly get the branch name with the format: prefix/branchname.

    To meet your requirement, you need to custom a variable based on the Build.SourceBranch varaible.

    Here is an example:

    variables:
      branchname: $[replace(variables['Build.SourceBranch'], 'refs/heads/', '')]
       
    steps:
    
    - checkout: self
      fetchDepth: 0
      persistCredentials: true
    - powershell: |
         git config --local user.email "$emailId"
         git config --local user.name "$(build.RequestedFor)"
         git checkout $(branchname)