简体   繁体   English

适用于CodeCommit回购同步的AWS Lambda

[英]AWS Lambda for CodeCommit repo sync

I want to mirror my Bitbucket repository to CodeCommit and build from it, without affecting developer's workflow. 我想将我的Bitbucket存储库镜像到CodeCommit并从中构建,而又不影响开发人员的工作流程。 Migration is easy, now the goal is to sync CodeCommit with Bitbucket, ideally, triggered by pushing to Bitbucket, but time-based sync is also acceptable. 迁移很容易,现在的目标是将CodeCommit与Bitbucket同步,理想情况下是通过推送到Bitbucket触发的,但是基于时间的同步也是可以接受的。

I found this post: https://aws.amazon.com/blogs/devops/replicating-and-automating-sync-ups-for-a-repository-with-aws-codecommit/ and unanswered question here Mirror a Git Repo directly to AWS CodeCommit 我发现了这篇文章: https : //aws.amazon.com/blogs/devops/replicating-and-automating-sync-ups-for-a-repository-with-aws-codecommit/ ,这里没有答案, 直接镜像Git存储库到AWS CodeCommit

Please, share you ideas how to write aws lambda function to sync CodeCommit repository with Bitbucket. 请与您分享如何编写aws lambda函数以将CodeCommit存储库与Bitbucket同步的想法。

While I haven't tried this, I suspect the following workflow could work. 虽然我还没有尝试过,但是我怀疑下面的工作流程可以工作。 The general idea would be to use the BitBucket webhooks feature to trigger the lambda function through the use of API gateway. 一般的想法是使用BitBucket Webhooks功能通过使用API​​网关来触发lambda函数。

  1. Write a Lambda function which 'mirror' clones from your bitbucket repository url and pushes to your CodeCommit repository url. 编写一个Lambda函数,该函数“镜像”从您的Bitbucket存储库URL克隆并推送到您的CodeCommit存储库URL。 Your function would likely need to include its own standalone git client library. 您的函数可能需要包括自己的独立git客户端库。

  2. Create an API using API gateway which calls your lambda function. 使用调用您的lambda函数的API网关创建一个API。 Potential challenges here might be verifying that POST requests going to your API are coming from Bitbucket, and not some other source. 这里的潜在挑战可能是验证去往您的API的POST请求是否来自Bitbucket,而不是其他来源。

  3. Create a new webhook for your Bitbucket repository with the URL being the url of your API you created in step 2. A 'Repository push' trigger would be sufficient to trigger a replication event after each push. 为您的Bitbucket存储库创建一个新的Webhook,URL为您在步骤2中创建的API的URL。“存储库推送”触发器足以在每次推送后触发复制事件。

One option is to use CodePipeline. 一种选择是使用CodePipeline。 Unfortunately with CodePipeline there is currently a weird process in making a Lambda function work as the sole process for the pipeline. 不幸的是,使用CodePipeline,使Lambda函数作为管道的唯一过程目前存在一个怪异的过程。 Basically it comes down to: 基本上可以归结为:

  1. Make a CodePipeline with the AWS CodeCommit as your source repo 使用AWS CodeCommit作为您的源存储库创建CodePipeline
  2. Create one of the required Build/Deploy stages with the settings it asks for. 使用所需的设置创建所需的构建/部署阶段之一。 Note that you will remove these later (unless you actually plan to use CodePipeline stages as given) so create a new CodeBuild project or something just to get through the wizard. 请注意,您稍后将删除它们(除非您实际上打算按照给定的方式使用CodePipeline阶段),因此创建一个新的CodeBuild项目或仅通过向导即可完成的工作。
  3. Create a Lambda function that talks to BitBucket to sync your changes. 创建一个与BitBucket对话的Lambda函数以同步您的更改。 The role attached must have permissions to interface with CodePipeline and CodeCommit. 附加的角色必须具有与CodePipeline和CodeCommit进行交互的权限。 The Lambda function must also call either one of PubJobSuccessResult or PutJobFailureResult so that CodePipeline knows the Lambda actually did something and to not sit and wait for the function to complete. Lambda函数还必须调用PubJobSuccessResultPutJobFailureResult中的任何一个,以便CodePipeline知道Lambda实际上已经做了一些事情,并且不必坐下来等待函数完成。
  4. Now go back to the CodePipeline and edit it. 现在回到CodePipeline并对其进行编辑。 For whatever stage was generated remove the existing action. 对于生成的任何阶段,请删除现有操作。 Add a new Invoke action that points to the Lambda function and set the Role which gives access to CodePipeline for setting the job result and CodeCommit for reading the repo. 添加一个新的Invoke动作,该动作指向Lambda函数,并设置Role,该角色可以访问CodePipeline来设置作业结果,并访问CodeCommit来读取仓库。

Another option is to use CloudWatch scheduling to invoke the Lambda at X interval if you're okay with a more delayed sync. 另一个选择是,如果可以延迟更长时间的同步,则可以使用CloudWatch调度以X间隔调用Lambda。 This will probably be easier to setup in the long run, with the possibility of having null Lambda runs if there's nothing to sync, counting against your allocation. 从长远来看,这可能会更容易设置,并且如果没有什么要同步的话,有可能使Lambda空运行,这会算作您的分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM