如何使用现代版本的 CodePipeline 进行 S3 部署

问题描述 投票:0回答:1

我正在尝试使用最新版本的适用于 typescript 的 AWS CDK (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 对象,并且显然构建步骤旨在使用嵌套输出,如上面的示例所示。每个输出文件都保存在一个文件名丑陋的存储桶中,因此也不能直接访问。

我看到了一些黑客方法,用 CodeBuildStep 替换 ShellStep 并在 buildspec.yml 文件中用作构建后命令,如下所示:

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

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

amazon-web-services amazon-s3 aws-cdk aws-codepipeline
1个回答
3
投票

您可以扩展

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 函数,但使用起来更简单。

© www.soinside.com 2019 - 2024. All rights reserved.