Azure Pipeline中Maven Release插件执行报错及后续问题求助
问题阶段梳理
1. 初始错误:无法检测当前分支
[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]
2. 添加fetchDepth:0后:Git身份未配置
[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] *** Please tell me who you are.
[ERROR] Run
[ERROR] git config --global user.email "you@example.com"
[ERROR] git config --global user.name "Your Name"
3. 尝试配置全局Git身份时:权限不足
error: could not lock config file /home/jenkins/.gitconfig: Permission denied error: could not lock config file /home/jenkins/.gitconfig: Permission denied
现有Azure Pipeline配置
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'
已更新的Git身份脚本
git config --local user.email "$emailId" git config --local user.name "$(build.RequestedFor)" git checkout feature/$(Build.SourceBranchName)
修复建议
- 分支检测问题修复:确认
Build.SourceBranch为完整分支路径(如refs/heads/feature/xxx),避免分支名拼接错误。当前已设置fetchDepth:0拉取完整仓库信息,需保证分支切换逻辑正确。 - Git身份配置优化:
- 保留
--local级别的Git身份配置,避免全局配置文件权限冲突。检查$(build.RequestedForEmail)变量是否能正常取值,若变量为空,可手动指定固定邮箱(如azure-pipeline@yourdomain.com)。 - 替换未定义的
$emailId变量为$(build.RequestedForEmail),确保身份信息完整。
- 保留
- 分支切换逻辑修正:
feature/$(Build.SourceBranchName)拼接可能出错,因为Build.SourceBranchName本身是分支的最后一段(如分支refs/heads/feature/test的SourceBranchName为test),建议直接使用完整分支路径切换:git checkout $(Build.SourceBranch) - 权限与凭证校验:确保
persistCredentials: true生效,允许Pipeline执行Git提交推送。在checkout步骤后添加git config --add safe.directory $(Build.SourcesDirectory),规避Git安全目录限制。 - Maven SCM配置补充:在项目pom.xml中添加正确的SCM配置,帮助插件识别仓库信息:
<scm> <connection>scm:git:https://dev.azure.com/your-org/your-project/_git/your-repo</connection> <developerConnection>scm:git:https://dev.azure.com/your-org/your-project/_git/your-repo</developerConnection> <url>https://dev.azure.com/your-org/your-project/_git/your-repo</url> <tag>HEAD</tag> </scm> - 提交前冲突预防:在身份配置脚本中添加拉取最新代码的逻辑,避免本地分支与远程分支不一致:
git pull --rebase origin $(Build.SourceBranch)
内容的提问来源于stack exchange,提问作者Dev0001

