gpt4 book ai didi

amazon-web-services - 仅当 lambda 发生任何更改时才使用 AWS Code Pipeline 执行云形成

转载 作者:行者123 更新时间:2023-12-03 07:19:10 24 4
gpt4 key购买 nike

我正在使用AWS代码管道来执行云形成。我的源代码已提交到 GitHub 存储库中。当我的 github 存储库中发生提交时,AWS Code Pipeline 将开始执行并执行云形成。这些功能运行良好。

在我的项目中,我有多个模块。因此,如果仅在一个模块中修改用户,则整个模块的 lambda 都会更新。有什么方法可以使用 AWS Code Pipeline 来限制这种情况。

我的代码管道有 3 个阶段。

  1. 来源
  2. 构建
  3. 部署

以下是我的代码管道的快照。

enter image description here

最佳答案

这正是我们最近面临的问题,虽然我看到评论提到使用单个存储库无法实现这一目标,但我找到了解决方法!

通常,代码管道由监听 GitHub/Code Commit 存储库的 CloudWatch 事件触发。我没有触发管道,而是让 CloudWatch 事件触发 lambda 函数。在 lambda 中,我们可以编写逻辑来仅对发生更改的模块执行管道。这非常有效,并且提供了对管道执行的大量控制。这样可以从单个存储库创建多个管道,解决问题中提到的问题。

Multiple pipeline from a repository structure

Lambda 逻辑可以是这样的:

import boto3

# Map config files to pipelines
project_pipeline_mapping = {
"CodeQuality_ScoreCard" : "test-pipeline-code-quality",
"ProductQuality_ScoreCard" : "test-product-quality-pipeline"
}

files_to_ignore = [ "readme.md" ]

codecommit_client = boto3.client('codecommit')
codepipeline_client = boto3.client('codepipeline')

def lambda_handler(event, context):
projects_changed = []
# Extract commits
print("\n EVENT::: " , event)
old_commit_id = event["detail"]["oldCommitId"]
new_commit_id = event["detail"]["commitId"]
# Get commit differences
codecommit_response = codecommit_client.get_differences(
repositoryName="ScorecardAPI",
beforeCommitSpecifier=str(old_commit_id),
afterCommitSpecifier=str(new_commit_id)
)
print ("\n Code commit response: ", codecommit_response)

# Search commit differences for files that trigger executions
for difference in codecommit_response["differences"]:
file_name = difference["afterBlob"]["path"]

project_name = file_name.split('/')[0]

print("\nChanged project: ", project_name)

# If project corresponds to pipeline, add it to the pipeline array
if project_name in project_pipeline_mapping:
projects_changed.insert(len(projects_changed),project_name)


projects_changed = list(dict.fromkeys(projects_changed))
print("pipeline(s) to be executed: ", projects_changed)

for project in projects_changed:
codepipeline_response = codepipeline_client.start_pipeline_execution(
name=project_pipeline_mapping[project]
)

查看有关此主题的 AWS 博客:Customizing triggers for AWS CodePipeline with AWS Lambda and Amazon CloudWatch Events

关于amazon-web-services - 仅当 lambda 发生任何更改时才使用 AWS Code Pipeline 执行云形成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59046395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com