1

您好,如果我在使用 AWS 云开发工具包 (CDK) 部署代码管道时收到此错误的可能来源,我将不胜感激

我有一个 Typecript CDK 项目(cdk init --language typescript)。这使用 GitHub 挂钩在我的 GitHub 存储库中的分支的每次更新上进行部署。这一步工作正常。但是,构建后步骤发生失败并出现错误

“阶段上下文状态代码:CLIENT_ERROR 消息:找不到与 cdk.out 匹配的基本目录路径”

从下面的代码中,您可以看到我使用了 @aws-cdk/pipelines 、 SimpleSynthAction.standardNpmSynth,我认为这不会生成 cdk.out 文件 (?)。任何有助于我解决此问题的评论将不胜感激

import { Construct, SecretValue, Stack, StackProps } from '@aws-cdk/core';
import * as cp from '@aws-cdk/aws-codepipeline';
import * as cpa from '@aws-cdk/aws-codepipeline-actions';
import * as pipelines from '@aws-cdk/pipelines';
import { WebServiceStage } from './webservice_stage';


    export class MyPipelineStack extends Stack {
      constructor(scope: Construct, id: string, props?: StackProps) {
        super(scope, id, props);
    
        const sourceArtifact = new cp.Artifact();
        const cloudAssemblyArtifact = new cp.Artifact();
        const sourceAction = new cpa.GitHubSourceAction({
            actionName: 'GitHub',
            output: sourceArtifact,
            oauthToken: SecretValue.secretsManager('github-token'),
            owner: 'owner',
            repo: 'repo-name',
            branch:'branch-name'
        });
    
        const synthAction = pipelines.SimpleSynthAction.standardNpmSynth({
            sourceArtifact,
            cloudAssemblyArtifact,
            buildCommand: 'npm install && npm run build && npm test',
            synthCommand: 'npm run synth'
        });
    
        const pipeline = new pipelines.CdkPipeline(this, 'Pipeline', {
            cloudAssemblyArtifact,
            sourceAction,
            synthAction
        });
        // Dev stage
        const development = new WebServiceStage(this, 'Development');
        pipeline.addApplicationStage(development);
    
      }
    }

npm 'npm run syth' 只是调用 'cdk synth'

我尝试在我的 /bin/my_pipeline.ts 文件中运行 app.synth() ,但这并不能解决错误。

再一次,我已经用谷歌搜索了这个地狱并且无法解决它,所以非常感谢任何帮助。如果它在这里有任何用途是我在构建日志中输出的 buildSpec 部分

BuildSpec: >-
          {
            "version": "0.2",
            "phases": {
              "pre_build": {
                "commands": [
                  "npm ci"
                ]
              },
              "build": {
                "commands": [
                  "cd partnerportal_cicd_pipeline && npm install && npm run build && npm test",
                  "npm run synth"
                ]
              }
            },
            "artifacts": {
              "base-directory": "cdk.out",
              "files": "**/*"
            }
          }
4

3 回答 3

2

我在 CDK v2 上遇到了同样的错误。

就我而言,我必须通过选项指定 CodePipeline 可以期望该cdk.out文件的子目录primaryOutputDirectory

默认情况下,CodePipeline 期望此目录位于项目的根目录中,这就是它失败的原因。

带有子目录的示例配置

const pipeline = new pipelines.CodePipeline(this, 'Pipeline', {
  synth: new pipelines.ShellStep('Synth', {
    input: source,
    commands: [
      'cd mysubdir',
      'npm ci',
      'npm run build',
      'npx cdk synth',
    ],
    primaryOutputDirectory: 'mysubdir/cdk.out',
  }),
});

相关文档

于 2021-12-10T18:00:01.367 回答
2

对我来说,问题是 cdk.out 必须在存储库的根文件夹中,所以我添加了一个新命令: mv cdk.out ../ 。

于 2021-10-16T13:11:47.037 回答
1

我让它工作了,我必须 cdk 销毁部署的堆栈,然后运行 ​​'cdk bootstrap --cloudformation-execution-policies'arn:aws:iam::aws:policy/AdministratorAccess',然后确保所有内容都推送到 repo最后再次运行“cdk deploy”。我认为因为我没有这样做,所以我的“synthCommand”没有生效。

于 2021-01-20T16:57:45.527 回答