amazon-web-services - 如何使用现代版 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/
但它是在构建阶段解决的,而不是在理想存在的部署阶段。
我在文档中没有看到任何有见地的东西,所以欢迎提出任何建议。谢谢!
解决方案
您可以扩展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 函数,但它更易于使用。
推荐阅读
- c# - 从字符串动态创建 CLR 对象
- php - 在循环中仅显示一次父类别标题
- excel - VBA 函数:从字符串中提取日期 - 类型不匹配
- c++ - GCC for c++11 和 USACO 编译器之间的编译器区别
- sql - 显示另一个表中是否存在值的 SQL 指令
- javascript - 如果 HTML“产品 A”存在,则使用 Javascript 将其更改为“价格 A”
- c# - 等待 FileSystemWatcher 结果
- python - Python 对象的 __iter__ 没有被调用
- reactjs - 在 TypeScript 项目中使用 props 设置样式化组件 CSS 规则
- mysql - Mysql:如何将数据添加到存储在变量中的现有数据中?