簡體   English   中英

使用 CDK 的 AWS CodeBuild“錯誤:.git/HEAD 不存在”

[英]AWS CodeBuild With the CDK "Error: .git/HEAD does not exist"

我的願望是使用來自 CDK 庫aws-cdk-lib/pipelines的 AWS Code Pipeline 構建 CDK Cloud Formation 堆棧。 在 CLI 中運行cdk ls時,一切都按預期進行。 我也可以使用cdk deploy成功部署管道。

錯誤信息:

[Container] 2022/12/30 09:18:36 Running command npx cdk synth
Error: .git/HEAD does not exist
    at gitHeadPath (/codebuild/output/src224694107/src/backend/node_modules/git-branch/index.js:36:11)
    at branch (/codebuild/output/src224694107/src/backend/node_modules/git-branch/index.js:14:28)
    at /codebuild/output/src224694107/src/backend/src/context/getContext.ts:11:41
    at new Promise (<anonymous>)
    at Object.exports.getContext (/codebuild/output/src224694107/src/backend/src/context/getContext.ts:9:12)
    at createStack (/codebuild/output/src224694107/src/backend/bin/template.ts:9:25)
    at Object.<anonymous> (/codebuild/output/src224694107/src/backend/bin/template.ts:18:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module.m._compile (/codebuild/output/src224694107/src/backend/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
(node:179) UnhandledPromiseRejectionWarning: undefined
(Use `node --trace-warnings ...` to show where the warning was created)
(node:179) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:179) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

CDK 流水線代碼:

this.codePipeline = new CodePipeline(this, `${environment}-pipeline-${appName}`, {
  pipelineName: `${environment}-pipeline-${appName}`,
  selfMutation: true,
  crossAccountKeys: false,
  role: this.codePipelineRole,
  synth: new ShellStep("Deployment", {
    input: CodePipelineSource.codeCommit(this.codeRepository, environment),
    installCommands: ["npm uninstall -g aws-cdk", "npm i -g npm@latest", "npm install -g aws-cdk"],
    commands: ["cd backend", "npm ci", "npm run build", "npx cdk synth"],
    primaryOutputDirectory: "backend/cdk.out",
  }),
});

獲取上下文 Function:

export const getContext = (app: App): Promise<CDKContext> => {
    return new Promise(async (resolve, reject) => {
        try {
            const currentBranch = await gitBranch();
            const environment = app.node.tryGetContext("environments").find((e: any) => e.branchName === currentBranch);
            const globals = app.node.tryGetContext("globals");
            return resolve({...globals, ...environment});
        }
        catch (error) {
            console.error("error", error);
            return reject();
        }
    })
}

Package.json 依賴項:

  "dependencies": {
    "@aws-cdk/aws-appsync-alpha": "^2.55.1-alpha.0",
    "aws-cdk-lib": "^2.58.0",
    "aws-sdk": "^2.1278.0",
    "constructs": "^10.1.204",
    "git-branch": "^2.0.1",
    "source-map-support": "^0.5.21"
  }

Code Build 有兩個克隆存儲庫的選項:

  1. CodePipeline Default - “AWS CodePipeline 使用管道中工件的默認 zip 格式。不包括關於存儲庫的 git 元數據”
  2. 完整克隆- “AWS CodePipeline 傳遞有關存儲庫的元數據,允許后續操作執行完整的 git 克隆。僅支持 AWS CodeBuild 操作。”

從控制台獲取的報價。

因此,管道定義需要添加一個 Code Commit source prop 來告訴 CDK 進行完整克隆。 CDK Docs 的選項在這里。

更新input

    input: CodePipelineSource.codeCommit(this.codeRepository, environment, {
      codeBuildCloneOutput: true
    })

codeBuildCloneOutput - “如果已設置,下一個 CodeBuild 作業將克隆存儲庫(而不是 CodePipeline 下載文件)。” 這允許完整克隆存儲庫,並將消除錯誤。

CDK 權限更新:

此圖像顯示現在 CodeBuild 可以執行GitPull

CDK 權限變更

下載的 CodeBuild 源代碼沒有.git文件夾。 無論您的gitBranch()方法做什么,它都不起作用。

由於CodePipelineSource.codeCommit()已經要求您傳遞分支,您只需將其作為環境變量傳遞給 CodeBuild 項目即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM