首页 > 解决方案 > 如何使用现代版 CodePipeline 进行 S3 部署

问题描述

我正在尝试使用最新版本的 AWS CDK for typescript (1.128) 设置一个全新的管道。

管道的创建非常简单。我添加了源代码和构建阶段,没有任何问题。这里的目标是自动部署静态登录页面。

到目前为止,我有这段代码:

        const landingPageStep = new ShellStep(`${PREFIX}LandingPageCodeBuildStep`, {
            input: CodePipelineSource.connection(`${GIT_ORG}/vicinialandingpage`, GIT_MAIN, {
                connectionArn: GIT_CONNECTION_ARN, // Created using the AWS console
            }),
            installCommands: [
                'npm ci',
            ],
            commands: [
                'npm run build',
            ],
            primaryOutputDirectory: 'out',
        })

        const pipeline = new CodePipeline(this, `${PREFIX}Pipeline`, {
            pipelineName: `${PREFIX}Pipeline`,
            synth: new ShellStep(`${PREFIX}Synth`, {
                input: CodePipelineSource.connection(`${GIT_ORG}/viciniacdk`, GIT_MAIN, {
                    connectionArn: GIT_CONNECTION_ARN, // Created using the AWS console
                    }),
                commands: [
                    'npm ci',
                    'npm run build',
                    'npx cdk synth',
                ],
                additionalInputs: {
                    'landing_page': landingPageStep,
                },
            }),
        });

我不确定如何实现它的步骤是如何使用“landing_page”的输出部署到 S3。在以前版本的 Pipelines 中,大量使用了 Artifacts 对象和 CodePipelineActions,类似于 sourceOutput 是 Artifact 对象的情况:

    const targetBucket = new s3.Bucket(this, 'MyBucket', {});

    const pipeline = new codepipeline.Pipeline(this, 'MyPipeline');
    const deployAction = new codepipeline_actions.S3DeployAction({
        actionName: 'S3Deploy',
        stage: deployStage,
        bucket: targetBucket,
        input: sourceOutput,
    });
    const deployStage = pipeline.addStage({
        stageName: 'Deploy',
        actions: [deployAction],
    });

现在完全不同了,因为您可以访问 FileSet 对象,并且显然构建步骤旨在用于嵌套输出,如上面的示例。每个输出文件都保存在具有难看文件名的存储桶中,因此也不打算直接访问它。

我已经看到一些 hacky 方法用 CodeBuildStep 替换 ShellStep 并在 buildspec.yml 文件中用作 postbuild 命令,如下所示:

aws s3 sync out s3://cicd-codebuild-static-website/

但它是在构建阶段解决的,而不是在理想存在的部署阶段。

我在文档中没有看到任何有见地的东西,所以欢迎提出任何建议。谢谢!

标签: amazon-web-servicesamazon-s3aws-cdkaws-codepipelinestatic-web-apps

解决方案


您可以扩展Step和实现ICodePipelineActionFactory. 它是一个获取codepipeline.IStage和添加您需要添加的任何操作的界面。

一旦你有了工厂步骤,你就可以将它作为方法选项的一个pre或多个post选项传递addStage()

接近以下的东西应该可以工作:

class S3DeployStep extends Step implements ICodePipelineActionFactory {
  constructor(private readonly provider: codepipeline_actions.JenkinsProvider, private readonly input: FileSet) {
  }

  public produceAction(stage: codepipeline.IStage, options: ProduceActionOptions): CodePipelineActionFactoryResult {

    stage.addAction(new codepipeline_actions.S3DeployAction({
        actionName: 'S3Deploy',
        stage: deployStage,
        bucket: targetBucket,
        input: sourceOutput,
        runOrder: options.runOrder,
    }));

    return { runOrdersConsumed: 1 };
  }
}

// ...

pipeline.addStage(stage, {post: [new S3DeployStep()]});

但是一种更简单的方法是使用BucketDeployment它作为堆栈部署的一部分。它创建一个自定义资源,将数据从您的资产或另一个存储桶复制到存储桶。它不会在管道中获得自己的步骤,它会在后台创建一个 Lambda 函数,但它更易于使用。


推荐阅读