CodePipeline 生成器
项目描述
管道根
CodePipeline/CodeBuild CI/CD Pipeline 创建者 + 部署者。
安装
使用 pip/pypi 安装:
pip install pipegen
用法
使用 pipegen 部署管道:
pipegen deploy --config CONFIG_FILE --stack-name NAME_OF_STACK [--var KEY=VALUE [--var KEY=VALUE]]
输出编译配置:
pipegen dump config --config CONFIG_FILE [--var KEY=VALUE [--var KEY=VALUE]]
要输出已编译的 CloudFormation 模板:
pipegen dump template --config CONFIG_FILE [--var KEY=VALUE [--var KEY=VALUE]]
配置模式
架构分为几个部分:
- 跨许多资源使用的基本/共享配置
- 源配置
- 管道配置
基本配置
基本配置用于使用 部署的多个资源pipegen
,或者可用于覆盖某些默认值。
下面记录了完整的模式,用 指定了必需的元素(R)
,并指示了默认值。
config:
s3_bucket: (R) the name of a S3 bucket to store artifacts in (source copies,
codebuild artifacts, etc)
kms_key_arn: (R) the ARN of a KMS key to encrypt all resources with
codepipeline:
restart_execution_on_update: whether or not to restart an in-progress
CodePipeline execution if the pipeline is
updated (default: false)
codebuild:
compute_type: the default compute type to use (default: BUILD_GENERAL1_SMALL)
image: the default docker image to use
(default: aws/codebuild/amazonlinux2-x86_64-standard:3.0)
log_group:
enabled: whether or not to enable CloudWatch logs for CodeBuild
projects (default: true)
create: whether or not to create the CloudWatch log group (default: true).
Disable if you have created the log group externally.
name: the name of the CloudWatch log group for the pipeline
(default: random generated name based on the stack name)
retention: A number in days to retain logs (default: null, logs are retained
indefinitely)
iam: a list of IAM statements to add to the CodeBuild role (default: null).
Use if your CodeBuild projects need to manipulate AWS resources
IAM 示例
默认情况下,pipegen
使用最少的权限配置 CodeBuild,以便运行、从 KMS 解密您的工件、从 ECR 拉取映像(如果已配置)、将日志写入 CloudWatch 日志(如果已配置)。如果您需要额外的 IAM 权限,您可以使用以下语法指定它们:
config:
iam:
- standard IAM statement
例如,在构建中添加将文件上传到 S3 的权限:
config:
iam:
- Effect: Allow
Action:
- s3:PutObject
Resource:
- my-bucket-name/*
源配置
源配置定义了项目代码的来源。
目前支持两个顶级选项:
- CodeCommit,AWS 的托管 git 服务
- CodeStar Connections,使用 Github.com、GitHub Enterprise (GHE) 或 BitBucket
管道可以使用来自任一提供商的多个源存储库。
代码提交
下面介绍使用 CodeCommit 的源配置。
下面记录了完整的模式,用 指定了必需的元素(R)
,并指示了默认值。
sources:
- name: (R) a unique name for this source entry
from: (R) the provider to use. use `CodeCommit` for CodeCommit
repository: (R) the name of the repository
branch: (R) the branch to build from
poll_for_source_changes: whether CodePipeline should poll for updates (default: false)
event_for_source_changes: whether to use CloudWatch events/EventBridge to
detect updates (default: true)
注意:如果您希望管道在将存储库推送到时触发构建,poll_for_source_changes
或者event_for_source_changes
应该设置为true
(event_for_source_changes
首选)。
CodeStar 连接
下面介绍使用 CodeStar Connections 从 Github.com、GHE 或 BitBucket 获取源的源配置。
为了使用 CodeStar 连接,您必须在部署管道堆栈之前单独配置连接。
下面记录了完整的模式,用 指定了必需的元素(R)
,并指示了默认值。
sources:
- name: (R) a unique name for this source entry
from: (R) the provider to use. use `CodeCommit` for CodeCommit
repository: (R) the name of the repository
branch: (R) the branch to build from
connection_arn: (R) the codestar connection ARN to use to pull changes through
管道配置
下面描述管道的构建配置。
下面记录了完整的模式,用 指定了必需的元素(R)
,并指示了默认值。
stages:
- name: (R) a unique name for this deploy stage, e.g. "Build", "Deploy", etc
enabled: whether or not this stage is enabled (default: true)
actions:
- name: (R) a unique name for this action, e.g. "Build", "DeployToStaging",
"DeleteProduction", etc
provider: the provider to use to perform the action,
allowed values: CodeBuild (default: CodeBuild).
Note: list may be extended in future
buildspec: a path to a CodeBuild buildspec YAML file from your first
repository's root directory
commands: a list of commands to run as part of your buildspec
artifacts: a list of file path artifacts to store after your build
compute_type: the compute type to use (default: config.codebuild.compute_type's value)
image: the docker image to use (default: config.codebuild.image's value)
environment: a hash of "key: value" variables to provide to the build
input_artifacts: a list of other build actions `Name` fields, who's artifacts
to bring in to your build
BuildSpec / 命令 / 工件
使用 CodeBuild 时,您可以:
- 不指定
buildspec
,commands
, 或artifacts
字段,默认buildspec.yml
在主源的根目录中使用构建指令, - 提供一个
buildspec
字段,以指定存储库中包含构建指令的文件, - 为 pipegen提供
commands
(如果需要)为您内联生成构建规范。artifacts
可在此处找到 CodeBuild 构建规范的模板参考:https ://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html 。如果您指定commands
,pipegen 使用 buildspec v0.2 生成内联的 buildspec。
环境变量
默认情况下,pipegen 为您的构建项目提供两个环境变量:
AWS_DEFAULT_REGION
和AWS_REGION
,两者都设置为您部署到的 AWS 区域(CloudFormation 等效于AWS::Region
)。通过使用替代值指定这些键,可以覆盖其中任何一个。
您可以使用以下语法根据需要提供其他环境变量:
environment:
KEY: VALUE
MY_VARIABLE: my value
输入工件
pipegen 配置的每个 CodeBuild 项目都会将所有配置的源添加为输入,这意味着每个构建都可以访问每个存储库。
如果您使用“构建然后部署”模式进行构建,您可能希望将构建工件传递到部署阶段。您可以使用类似于以下的语法来实现此目的:
stages:
- name: Build
actions:
- name: MyBuildStep
- name: Deploy
actions:
- name: Deploy
commmands:
- bin/deploy
input_artifacts:
- MyBuildStep # << Note that this matches the action name above
变量和导入
pipegen
支持大多数配置条目的两种特殊语法。
变量
任何--var key=name
传递给 CLI 的内容都可以在您的配置文件中使用,以在运行时使用类似{{vars.KEY}}
. 这通常用于改变所使用的分支等内容。例如:
config.yml
:
sources:
- name: Source
from: CodeCommit
repository: my-repo
branch: {{vars.BranchName}}
pipegen cli
:
pipegen deploy --var BranchName=main ...
此外,由于 pipegen 的配置是使用 jinja2 评估的,因此您可以使用变量来确定其他值。通常,这可用于基于分支启用/禁用阶段(例如,仅为分支运行生产部署main
)。
stages:
- name: Production
enabled: {{vars.BranchName == "main"}}
进口
pipegen
支持使用类似的语法从CloudFormation 导出中导入值import:ImportName
。
例子:
config:
s3_bucket: import:S3BucketName
kms_key_arn: import:KmsKeyArn
示例配置
从 CodeCommit 构建
---
配置:
s3_bucket :我的s3-bucket
kms_key_arn : arn:aws:kms:REGION:ACCOUNT:key/KEY-ID
sources:
- name: Source
from: CodeCommit
repository: my-repo
branch: {{vars.BranchName}}
阶段:
-名称:构建
行动:
-名称:构建
提供者:代码构建
构建规范:构建规范/build.yml
-名称:DevDeploy
行动:
-名称:部署
提供者:代码构建
构建规范:构建规范/deploy.yml
环境:
TARGET_ENVIRONMENT :开发
输入工件:
-构建
-名称:ProdDeploy
启用:{{ vars.BranchName == "main" }}
行动:
-名称:ProdDeploy
提供者:代码构建
构建规范:构建规范/deploy.yml
环境:
TARGET_ENVIRONMENT :产品
输入工件:
-构建
从 CodeStar/Github.com 构建
---
config:
s3_bucket: import:S3BucketName
kms_key_arn: import:KMSKeyArn
codebuild:
log_group:
retention: 7 # (days)
sources:
- name: codebuild-test
from: CodeStarConnection
repository: samjarrett/codebuild-test
branch: main
connection_arn: arn:aws:codestar-connections:REGION:ACCOUNT:connection/CONNECTION-ID
stages:
- name: Build
actions:
- name: Build
provider: CodeBuild
commands:
- make build
artifacts:
- build/*
- name: Deploy
actions:
- name: Deploy
provider: CodeBuild
commands:
- make deploy
input_artifacts:
- Build
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。