首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用现有代码的AWS CDK管道

使用现有代码的AWS CDK管道
EN

Stack Overflow用户
提问于 2021-08-16 09:21:53
回答 3查看 1.8K关注 0票数 3

@aws/管道的文档似乎表明CDK管道可以使用codePipeline支柱:https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_pipelines.CodePipeline.html添加到现有的@aws/ added /管道中。

codePipeline? Pipeline An existing Pipeline to be reused and built upon.

但是,我无法让它工作,并且在cdk synth步骤中遇到多个错误,这取决于我如何尝试设置它。据我所知,目前还没有任何文档可以涵盖这种情况。

本质上,我们正在尝试创建一个运行如下所示的管道:

  • clone
  • lint / typecheck /单元测试
  • cdk部署以测试environment
  • integration测试
  • 部署到preprod
  • smoke测试
  • 手动approval
  • deploy以prod

我想只是不清楚这个代码构建管道和cdk管道之间的区别。另外,阶段的命名约定似乎有点不清楚-引用这个问题:https://github.com/aws/aws-cdk/issues/15945

见:https://github.com/ChrisSargent/cdk-issues/blob/pipelines/lib/cdk-test-stack.ts及以下:

代码语言:javascript
复制
import * as cdk from "@aws-cdk/core";
import * as pipelines from "@aws-cdk/pipelines";
import * as codepipeline from "@aws-cdk/aws-codepipeline";
import * as codepipeline_actions from "@aws-cdk/aws-codepipeline-actions";

export class CdkTestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const cdkInput = pipelines.CodePipelineSource.gitHub(
      "ChrisSargent/cdk-issues",
      "pipelines"
    );

    // Setup the code source action
    const sourceOutput = new codepipeline.Artifact();
    const sourceAction = new codepipeline_actions.GitHubSourceAction({
      owner: "ChrisSargent",
      repo: "cdk-issues",
      branch: "pipelines",
      actionName: "SourceAction",
      output: sourceOutput,
      oauthToken: cdk.SecretValue.secretsManager("git/ChrisSargent"),
    });

    const pipeline = new codepipeline.Pipeline(this, "Pipeline", {
      stages: [
        {
          actions: [sourceAction],
          stageName: "GitSource",
        },
      ],
    });

    const cdkPipeline = new pipelines.CodePipeline(this, "CDKPipeline", {
      codePipeline: pipeline,
      synth: new pipelines.ShellStep("Synth", {
        // Without input, we get: Error: CodeBuild action 'Synth' requires an input (and the pipeline doesn't have a Source to fall back to). Add an input or a pipeline source.
        // With input, we get:Error: Validation failed with the following errors: Source actions may only occur in first stage
        input: cdkInput,
        commands: ["yarn install --frozen-lockfile", "npx cdk synth"],
      }),
    });

    // Produces: Stage 'PreProd' must have at least one action
    // pipeline.addStage(new MyApplication(this, "PreProd"));

    // Produces: The given Stage construct ('CdkTestStack/PreProd') should contain at least one Stack
    cdkPipeline.addStage(new MyApplication(this, "PreProd"));
  }
}

class MyApplication extends cdk.Stage {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StageProps) {
    super(scope, id, props);

    console.log("Nothing to deploy");
  }
}

如有此方面的指导或经验,将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2021-11-17 09:09:55

通过向CDK管道中只添加prepost步骤,我可以实现类似的目标,示例代码如下所示,我正在对原始代码片段进行修改:

代码语言:javascript
复制
import * as cdk from "@aws-cdk/core";
import * as pipelines from "@aws-cdk/pipelines";
import * as codepipeline from "@aws-cdk/aws-codepipeline";
import * as codepipeline_actions from "@aws-cdk/aws-codepipeline-actions";

export class CdkTestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const cdkInput = pipelines.CodePipelineSource.gitHub(
      "ChrisSargent/cdk-issues",
      "pipelines"
    );

    const cdkPipeline = new pipelines.CodePipeline(this, "CDKPipeline", {
      selfMutation: true,
      crossAccountKeys: true, //can be false if you don't need to deploy to a different account.
      pipelineName,
      synth: new pipelines.ShellStep("Synth", {
        // Without input, we get: Error: CodeBuild action 'Synth' requires an input (and the pipeline doesn't have a Source to fall back to). Add an input or a pipeline source.
        // With input, we get:Error: Validation failed with the following errors: Source actions may only occur in first stage
        input: cdkInput,
        commands: ["yarn install --frozen-lockfile", "npx cdk synth"],
        primaryOutputDirectory: 'cdk.out'
      }),
    });

    // add any additional test step here, they will run parallels in waves
    cdkPipeline.addWave('test', {post: [provideUnitTestStep(this, 'unitTest')]});
    // add a manual approve step if needed.
    cdkPipeline.addWave('promotion', {post: [new ManualApprovalStep('PromoteToUat')]});

    // Produces: Stage 'PreProd' must have at least one action
    // pipeline.addStage(new MyApplication(this, "PreProd"));

    // Produces: The given Stage construct ('CdkTestStack/PreProd') should contain at least one Stack
    cdkPipeline.addStage(new MyApplication(this, "PreProd"));
  }
}

class MyApplication extends cdk.Stage {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StageProps) {
    super(scope, id, props);

    console.log("Nothing to deploy");
  }
}

值得注意的是,您可能需要隐藏将Codebuild操作写入新的cdk CodeBuildStep的方式。示例单元测试步骤如下所示:

代码语言:javascript
复制
const provideUnitTestStep = (
    id: string
): cdkpipeline.CodeBuildStep => {
    const props: CodeBuildStepProps = {
        partialBuildSpec: codebuild.BuildSpec.fromObject({
            version: '0.2',
            env: {
                variables: {
                    DEFINE_VARIBLES: 'someVariables'
                }
            },
            phases: {
                install: {
                    commands: [
                        'install some dependencies',
                    ]
                },
                build: {
                    commands: [
                        'run some test!'
                    ]
                }
            }
        }),
        commands: [],
        buildEnvironment: {
            buildImage: codebuild.LinuxBuildImage.STANDARD_5_0
        }
    };
    return new cdkpipeline.CodeBuildStep(`${id}`, props);
};

检索下划线CodeBuild项目Role并不是那么简单(而且足够直接),您需要在CodeBuildStep道具中传递rolePolicyStatements属性,以授予测试所需的额外权限。

票数 1
EN

Stack Overflow用户

发布于 2021-08-16 16:01:46

首先,错误Pipeline must have at least two stages是正确的。您只在单个阶段获得了GitHub签出/克隆命令。在第二阶段,您可以使用CodeBuild项目来编译/lint/单元测试.就像你提到的。

但是,您希望如何处理已编译的工件呢?构建容器来部署它们吗?如果是这样的话,CDK有更好的方法来做到这一点(DockerImageAsset)。这也可以保存您先前存在的管道,您可以直接使用CDK管道。

请您尝试设置常规管道的属性restartExecutionOnUpdate: true,如下面的片段所示。

代码语言:javascript
复制
 const pipeline = new codepipeline.Pipeline(this, "Pipeline", {
      restartExecutionOnUpdate: true,
      stages: [
        {
          actions: [sourceAction],
          stageName: "GitSource",
        },
      ],
    });

这是CDK流水线的自突变能力所必需的.

票数 0
EN

Stack Overflow用户

发布于 2022-09-28 13:54:18

当我在没有特定定义的帐户和区域的堆栈中创建管道时,我就遇到了这种情况。

检查是否有这样的env

代码语言:javascript
复制
new CdkStack(app, 'CdkStack', {
    env: {
        account: awsProdAccount,
        region: defaultRegion,
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68800264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档