我想将Bitbucket的代码集成到AWS Code Pipeline中。我无法找到相同的例子。我的源代码是.Net。有人可以指导我。谢谢。
您可以使用调用AWS API Gateway的webhook来集成Bitbucket和AWS CodePipeline,后者调用Lambda函数(调用CodePipeline)。有一个AWS博客引导您通过这个:Integrating Git with AWS CodePipeline
BitBucket有一个名为PipeLines
的服务,可以将代码部署到AWS服务。使用Pipelines打包并将更新从主分支推送到连接到CodePipeline
的S3存储桶
注意:
PipeLines
bitbucket-pipelines.yml
的文件,该文件必须放在项目中下面是一个示例bitbucket-pipelines.yml
,它将名为DynamoDb的目录的内容复制到S3存储桶
pipelines:
branches:
master:
- step:
script:
- apt-get update # required to install zip
- apt-get install -y zip # required if you want to zip repository objects
- zip -r DynamoDb.zip .
- apt-get install -y python-pip
- pip install boto3==1.3.0 # required for s3_upload.py
# the first argument is the name of the existing S3 bucket to upload the artefact to
# the second argument is the artefact to be uploaded
# the third argument is the the bucket key
- python s3_upload.py LandingBucketName DynamoDb.zip DynamoDb.zip # run the deployment script
下面是一个Python上传脚本的工作示例,该脚本应与项目中的bitbucket-pipelines.yml
文件一起部署。上面我命名了我的Python脚本s3_upload.py
:
from __future__ import print_function
import os
import sys
import argparse
import boto3
from botocore.exceptions import ClientError
def upload_to_s3(bucket, artefact, bucket_key):
"""
Uploads an artefact to Amazon S3
"""
try:
client = boto3.client('s3')
except ClientError as err:
print("Failed to create boto3 client.\n" + str(err))
return False
try:
client.put_object(
Body=open(artefact, 'rb'),
Bucket=bucket,
Key=bucket_key
)
except ClientError as err:
print("Failed to upload artefact to S3.\n" + str(err))
return False
except IOError as err:
print("Failed to access artefact in this directory.\n" + str(err))
return False
return True
def main():
parser = argparse.ArgumentParser()
parser.add_argument("bucket", help="Name of the existing S3 bucket")
parser.add_argument("artefact", help="Name of the artefact to be uploaded to S3")
parser.add_argument("bucket_key", help="Name of the S3 Bucket key")
args = parser.parse_args()
if not upload_to_s3(args.bucket, args.artefact, args.bucket_key):
sys.exit(1)
if __name__ == "__main__":
main()
这是一个只有一个Source
阶段的示例CodePipeline(您可能想要添加更多):
Pipeline:
Type: "AWS::CodePipeline::Pipeline"
Properties:
ArtifactStore:
# Where codepipeline copies and unpacks the uploaded artifact
# Must be versioned
Location: !Ref "StagingBucket"
Type: "S3"
DisableInboundStageTransitions: []
RoleArn:
!GetAtt "CodePipelineRole.Arn"
Stages:
- Name: "Source"
Actions:
- Name: "SourceTemplate"
ActionTypeId:
Category: "Source"
Owner: "AWS"
Provider: "S3"
Version: "1"
Configuration:
# Where PipeLines uploads the artifact
# Must be versioned
S3Bucket: !Ref "LandingBucket"
S3ObjectKey: "DynamoDb.zip" # Zip file that is uploaded
OutputArtifacts:
- Name: "DynamoDbArtifactSource"
RunOrder: "1"
LandingBucket:
Type: "AWS::S3::Bucket"
Properties:
AccessControl: "Private"
VersioningConfiguration:
Status: "Enabled"
StagingBucket:
Type: "AWS::S3::Bucket"
Properties:
AccessControl: "Private"
VersioningConfiguration:
Status: "Enabled"
可以在此处找到对此Python代码以及其他示例的引用:https://bitbucket.org/account/user/awslabs/projects/BP
跟进任何人现在发现这个:
AWS CodeBuild现在支持Atlassian Bitbucket Cloud作为源类型,使其成为现有支持源的第四个:AWS CodeCommit,Amazon S3和GitHub。
这意味着您不再需要像@ Kirkaiya与Bitbucket集成的链接中所建议的那样实现lambda函数 - 它仍然是一个有效的解决方案,具体取决于您的用例或者您正在与非云版本的Bitbucket集成。
发表在AWS博客2017年8月10日 - https://aws.amazon.com/about-aws/whats-new/2017/08/aws-codebuild-now-supports-atlassian-bitbucket-cloud-as-a-source-type/
AWS CodeBuild Now Supports Building Bitbucket Pull Requests,我们可以利用它来获得更好的解决方案,而无需使用webhooks / API Gateway / Lambda
您可以使用CodeBuild将代码压缩到s3,并将其用作CodePipeline中的源代码
替换@binary的答案,并澄清@ OllyTheNinja的答案:
简而言之:让CodeBuild听Bitbucket的Webhook并写入S3对象。在管道中听取后者的更新事件。
在AWS codesuite中
对我来说,将Bitbucket与任何AWS服务集成的最佳方法是使用Pipelines将任何提交镜像到(镜像)AWS CodeCommit存储库。从那里,您可以完美地集成到AWS上的任何服务。你可以找到一个很好的方法:here:
如果您正在寻找一种使用AWS CodePipeline自动化构建部署过程的方法,其中source as bitbucket而不使用lambdas,请执行以下步骤。
注意-1。为了创建webhook,您需要具有bitbucket管理员访问权限因此从提交到部署的过程是完全自动化的。 2.截至目前(1919年4月),CodeBuild不支持关于Pull请求合并的webhook。如果你想要,你可以创建触发器,每天都会触发代码构建。
您还可以创建触发器以定期构建代码https://docs.aws.amazon.com/codebuild/latest/userguide/trigger-create.html
更新 - (June'19) - CodeBuild现在支持PR_Merge的Pull Request构建。参考:https://docs.aws.amazon.com/codebuild/latest/userguide/sample-bitbucket-pull-request.html#sample-bitbucket-pull-request-filter-webhook-events。