You need to enable JavaScript to run this app. AWS CodeBuild无法向CodeCommit提交推送变更的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

AWS CodeBuild无法向CodeCommit提交推送变更的问题求助

问题描述

我在CodePipeline中配置了一条流水线,当CodeCommit仓库的main分支有推送操作时会触发该流水线。

该流水线包含一个Build步骤,执行的命令会修改当前构建流水线上下文内的部分文件。修改完成后,我需要将变更提交、推送到CodeCommit仓库并创建PR。

我的buildspec.yml文件内容如下:

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 16.x
    commands:
      # 安装全局NPM包

  build:
    commands:
      # 执行修改仓库文件的命令

  post_build:
    commands:
      - git status

      # 设置凭证助手
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true
      - git checkout -b l10n
      - git add .
      - git commit -m "New Changes"
      - git push origin l10n --force
      - aws codecommit create-pull-request --title "New PR" \
        --description "Pull request description" \
        --targets repositoryName=test,sourceReference=main,destinationReference=l10n

但该配置无法正常运行,我遇到了以下错误:

[Container] 2023/03/29 13:57:35 Phase complete: BUILD State: SUCCEEDED
[Container] 2023/03/29 13:57:35 Phase context status code:  Message: 
[Container] 2023/03/29 13:57:35 Entering phase POST_BUILD
[Container] 2023/03/29 13:57:35 Running command git status
HEAD detached at 839b707
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    locales/UK.json
    locales/de.json
    locales/es.json
    locales/et.json
    locales/fr.json

nothing added to commit but untracked files present (use "git add" to track)

[Container] 2023/03/29 13:57:35 Running command git config --global credential.helper '!aws codecommit credential-helper $@'

[Container] 2023/03/29 13:57:35 Running command git config --global credential.UseHttpPath true

[Container] 2023/03/29 13:57:35 Running command git checkout -b l10n
Switched to a new branch 'l10n'

[Container] 2023/03/29 13:57:35 Running command git add .

[Container] 2023/03/29 13:57:35 Running command git commit -m "New Changes"
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@06f7081cc58f.(none)')

[Container] 2023/03/29 13:57:35 Command did not exit successfully git commit -m "New Changes" exit status 128
[Container] 2023/03/29 13:57:35 Phase complete: POST_BUILD State: FAILED
[Container] 2023/03/29 13:57:35 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: git commit -m "New Changes". Reason: exit status 128

我的CodeBuild IAM角色已附加AWSCodeCommitFullAccess策略。请问是否可以通过配置Git和流水线,实现将构建步骤中产生的变更提交并推送的需求?我也曾尝试执行git config --global user.email和git config --global user.name命令,但未解决问题。


解决方案

可以实现这个需求,问题出在Git身份配置时机、仓库状态处理上,具体修正步骤如下:

1. 正确配置Git提交身份

在post_build阶段最开始添加Git用户信息配置,且不要使用--global(避免全局配置冲突),直接配置当前仓库的身份:

- git config user.name "CodeBuild Bot"
- git config user.email "codebuild-bot@example.com"

2. 修复分离HEAD状态

CodeBuild拉取代码后默认处于分离HEAD状态,直接创建分支提交可能存在问题。先切换回main分支,再基于它创建新分支:

- git checkout main
- git checkout -b l10n

3. 精准追踪变更文件

避免用git add .提交无关文件,明确指定需要提交的路径:

- git add locales/*.json

4. 修正后的完整buildspec.yml

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: 16.x
    commands:
      # 安装全局NPM包示例:npm install -g some-package

  build:
    commands:
      # 修改文件的命令示例:npm run generate-locales

  post_build:
    commands:
      - git status
      
      # 配置当前仓库的Git提交身份
      - git config user.name "CodeBuild Bot"
      - git config user.email "codebuild-bot@example.com"
      
      # 修复分离HEAD状态,基于main分支创建新分支
      - git checkout main
      - git checkout -b l10n
      
      # 添加需要提交的变更文件
      - git add locales/*.json
      
      # 提交变更
      - git commit -m "Auto-generated locale files"
      
      # 配置CodeCommit凭证助手
      - git config credential.helper '!aws codecommit credential-helper $@'
      - git config credential.UseHttpPath true
      
      # 推送分支到CodeCommit
      - git push origin l10n --force
      
      # 创建Pull Request(注意source和destination分支顺序)
      - aws codecommit create-pull-request --title "Auto-generated locale update" \
        --description "Pull request for auto-updated locale files from CodeBuild pipeline" \
        --targets repositoryName=test,sourceReference=l10n,destinationReference=main

额外注意事项

  • 避免循环触发:PR合并回main会再次触发流水线,可通过提交信息加入[skip-codepipeline]标记,或修改CodePipeline触发条件忽略CodeBuild Bot的提交。
  • 分支覆盖风险:--force会覆盖远程l10n分支历史,若需保留历史,建议每次创建带时间戳的新分支(如l10n-20240520)。

内容的提问来源于stack exchange,提问作者Andrii Bodnar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 07:45:03