繁体   English   中英

与现有代码管道一起使用的 AWS CDK 管道

[英]AWS CDK Pipelines using with an existing codepipeline

@aws-cdk/pipelines 的文档似乎表明可以使用codePipeline道具将 CDK 管道添加到现有的@aws-cdk/aws-codepipeline/Pipeline: https://docs.aws.amazon.com/ cdk/api/latest/docs/@aws-cdk_pipelines.CodePipeline.html

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

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

本质上,我们正在尝试创建一个运行类似以下内容的管道:

  • 克隆
  • lint / 类型检查 / 单元测试
  • cdk部署到测试环境
  • 集成测试
  • 部署到预生产
  • 烟雾测试
  • 人工审批
  • 部署到产品

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

请参阅: https://github.com/ChrisSargent/cdk-issues/blob/pipelines/lib/cdk-test-stack.ts及以下内容:

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");
  }
}

任何与此有关的指导或经验将不胜感激。

首先,错误Pipeline must have at least two stages是正确的。 您只将 GitHub checkout/clone 命令作为单个阶段。 对于第二阶段,您可以使用 CodeBuild 项目来编译/lint/单元测试......正如您所提到的。

但是,那么您想对编译后的工件做什么? 构建容器以稍后部署它们? 如果是这样,CDK 有更好的方法来执行此操作 ( DockerImageAsset )。 这也可以节省您预先存在的管道,您可以直接使用 CDK 管道。

你能不能尝试设置属性restartExecutionOnUpdate: true ,你的常规管道,就像我下面的代码片段一样?

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

这是 CDK 管道的自突变能力所必需的。

我能够通过只增加波/阶段来实现类似的东西prepost步入CDK管道,示例代码被列为下面,我修改您的原始代码片段:

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 示例单元测试步骤可能如下所示:

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属性以授予测试所需的额外权限。

当我在没有特别定义的帐户和区域的堆栈中创建管道时,这发生在我身上。

检查你是否有env的环境:


new CdkStack(app, 'CdkStack', {
    env: {
        account: awsProdAccount,
        region: defaultRegion,
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM