如何使用代码管道持续部署到 CloudFormation 堆栈
How to use Code Pipeline to continuously deploy to a CloudFormation Stack
我正在尝试构建代码管道以:
- 取一个githubJava源项目
- 编译生成jar文件
- 使用 sam-template.yml 部署 jar
我可以构建 jar,即我有第 2 阶段工作。我假设第 3 阶段将涉及调用 sam-template 来进行部署。该模板是同一 github 存储库的一部分。
我的问题是:我看不到如何将 jar 和模板文件提供给第三阶段进行部署。
我在下面附上三个文件以供参考:
- 构建规范有效,但我找不到生成的工件。
到目前为止 - A json 管道,由
aws codepipeline get-pipeline
生成
- 将在第 3 阶段使用的 sam-template.yml 副本
1. buildspec.yml
version: 0.2
phases:
install:
commands:
- echo Entered the install phase...
- apt-get update -y
- apt-get install -y maven
build:
commands:
- echo Entered the build phase...
- mvn package
post_build:
commands:
- echo Entered the post_build phase...
artifacts:
files:
- server/harvest/target/harvest-1.0-SNAPSHOT.jar
discard-paths: yes
secondary-artifacts:
cf-config:
files:
- server/aws/sam-app/sam-template.yml
discard-paths: yes
jar-file:
files:
- server/harvest/target/harvest-1.0-SNAPSHOT.jar
discard-paths: yes
2。 codepipeline.json
{
"pipeline": {
"name": "<<Name>>",
"roleArn": "arn:aws:iam::xxxxxxxx",
"artifactStore": {
"type": "S3",
"location": "codepipeline-eu-west-1-xxxxxxx"
},
"stages": [
{
"name": "Source",
"actions": [
{
"name": "Source",
"actionTypeId": {
"category": "Source",
"owner": "ThirdParty",
"provider": "GitHub",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "master",
"OAuthToken": "****",
"Owner": "<<username>>",
"PollForSourceChanges": "false",
"Repo": "repo-name"
},
"outputArtifacts": [
{
"name": "SourceArtifact"
}
],
"inputArtifacts": []
}
]
},
{
"name": "Build",
"actions": [
{
"name": "Build",
"actionTypeId": {
"category": "Build",
"owner": "AWS",
"provider": "CodeBuild",
"version": "1"
},
"runOrder": 1,
"configuration": {
"ProjectName": "Harvest"
},
"outputArtifacts": [
{
"name": "BuildArtifact"
}
],
"inputArtifacts": [
{
"name": "SourceArtifact"
}
]
}
]
}
],
"version": 3
},
"metadata": {
"pipelineArn": "arn:aws:codepipeline:eu-west-1:xxxxxxxxx",
"created": 1546780342.845,
"updated": 1547288970.709
}
}
3。山姆-template.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS Serverless Spring Boot API - uk.co.pack::harvest
Globals:
Api:
EndpointConfiguration: REGIONAL
Outputs:
HarvestApi:
Description: URL for application
Export:
Name: HarvestApi
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/ping'
Parameters:
amazonawsaccessKey:
Type: String
amazonawssecretkey:
Type: String
amazondynamodbendpoint:
Type: String
appid:
Type: String
url:
Type: String
Resources:
HarvestRatingsFunction:
Properties:
CodeUri: build/harvest-1.0-SNAPSHOT.jar
Environment:
Variables:
AMAZON_AWS_ACCESSKEY: !Ref 'amazonawsaccessKey'
AMAZON_AWS_SECRETKEY: !Ref 'amazonawssecretkey'
AMAZON_DYNAMODB_ENDPOINT: !Ref 'amazondynamodbendpoint'
IOS_APP_ID: !Ref 'appid'
IOS_URL: !Ref 'url'
Events:
GetResource:
Properties:
Method: any
Path: /{proxy+}
Type: Api
Handler: uk.co.pack.StreamLambdaHandler::handleRequest
MemorySize: 512
Policies: AWSLambdaBasicExecutionRole
Runtime: java8
Timeout: 60
Type: AWS::Serverless::Function
RatingsDbTable:
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PROVISIONED
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: '1'
WriteCapacityUnits: '1'
TableName: Review
Type: AWS::DynamoDB::Table
Transform: AWS::Serverless-2016-10-31
CodePipeline 中的第三阶段可能如下所示:
{
"Name": "Deploy",
"Actions": [
{
"Name": "Beta",
"ActionTypeId": {
"Category": "Deploy",
"Owner": "AWS",
"Provider": "CloudFormation",
"Version": 1
},
"Configuration": {
"ActionMode": "CREATE_UPDATE",
"Capabilities": "CAPABILITY_IAM",
"RoleArn": "CloudformationRole.Arn",
"StackName": "Harvest",
"TemplatePath": "BuildOutput::sam-template.yml",
"ParameterOverrides": "{\"appid\": \"${app123456}\", \"url\": \"https://apple.com\"}"
},
"InputArtifacts": [
{
"Name": "BuildOutput"
}
],
"RunOrder": 1
}
]
}
jar 和模板在 BuildOutput
工件包中可用,因为您在 buildspec.yml 中指定了它们。只要你有BuildOutput
(或SourceOutput
)作为InputArtifacts
,你就可以像上面看到的那样使用它们。
我正在尝试构建代码管道以:
- 取一个githubJava源项目
- 编译生成jar文件
- 使用 sam-template.yml 部署 jar
我可以构建 jar,即我有第 2 阶段工作。我假设第 3 阶段将涉及调用 sam-template 来进行部署。该模板是同一 github 存储库的一部分。
我的问题是:我看不到如何将 jar 和模板文件提供给第三阶段进行部署。
我在下面附上三个文件以供参考:
- 构建规范有效,但我找不到生成的工件。 到目前为止
- A json 管道,由
aws codepipeline get-pipeline
生成
- 将在第 3 阶段使用的 sam-template.yml 副本
1. buildspec.yml
version: 0.2
phases:
install:
commands:
- echo Entered the install phase...
- apt-get update -y
- apt-get install -y maven
build:
commands:
- echo Entered the build phase...
- mvn package
post_build:
commands:
- echo Entered the post_build phase...
artifacts:
files:
- server/harvest/target/harvest-1.0-SNAPSHOT.jar
discard-paths: yes
secondary-artifacts:
cf-config:
files:
- server/aws/sam-app/sam-template.yml
discard-paths: yes
jar-file:
files:
- server/harvest/target/harvest-1.0-SNAPSHOT.jar
discard-paths: yes
2。 codepipeline.json
{
"pipeline": {
"name": "<<Name>>",
"roleArn": "arn:aws:iam::xxxxxxxx",
"artifactStore": {
"type": "S3",
"location": "codepipeline-eu-west-1-xxxxxxx"
},
"stages": [
{
"name": "Source",
"actions": [
{
"name": "Source",
"actionTypeId": {
"category": "Source",
"owner": "ThirdParty",
"provider": "GitHub",
"version": "1"
},
"runOrder": 1,
"configuration": {
"Branch": "master",
"OAuthToken": "****",
"Owner": "<<username>>",
"PollForSourceChanges": "false",
"Repo": "repo-name"
},
"outputArtifacts": [
{
"name": "SourceArtifact"
}
],
"inputArtifacts": []
}
]
},
{
"name": "Build",
"actions": [
{
"name": "Build",
"actionTypeId": {
"category": "Build",
"owner": "AWS",
"provider": "CodeBuild",
"version": "1"
},
"runOrder": 1,
"configuration": {
"ProjectName": "Harvest"
},
"outputArtifacts": [
{
"name": "BuildArtifact"
}
],
"inputArtifacts": [
{
"name": "SourceArtifact"
}
]
}
]
}
],
"version": 3
},
"metadata": {
"pipelineArn": "arn:aws:codepipeline:eu-west-1:xxxxxxxxx",
"created": 1546780342.845,
"updated": 1547288970.709
}
}
3。山姆-template.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS Serverless Spring Boot API - uk.co.pack::harvest
Globals:
Api:
EndpointConfiguration: REGIONAL
Outputs:
HarvestApi:
Description: URL for application
Export:
Name: HarvestApi
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/ping'
Parameters:
amazonawsaccessKey:
Type: String
amazonawssecretkey:
Type: String
amazondynamodbendpoint:
Type: String
appid:
Type: String
url:
Type: String
Resources:
HarvestRatingsFunction:
Properties:
CodeUri: build/harvest-1.0-SNAPSHOT.jar
Environment:
Variables:
AMAZON_AWS_ACCESSKEY: !Ref 'amazonawsaccessKey'
AMAZON_AWS_SECRETKEY: !Ref 'amazonawssecretkey'
AMAZON_DYNAMODB_ENDPOINT: !Ref 'amazondynamodbendpoint'
IOS_APP_ID: !Ref 'appid'
IOS_URL: !Ref 'url'
Events:
GetResource:
Properties:
Method: any
Path: /{proxy+}
Type: Api
Handler: uk.co.pack.StreamLambdaHandler::handleRequest
MemorySize: 512
Policies: AWSLambdaBasicExecutionRole
Runtime: java8
Timeout: 60
Type: AWS::Serverless::Function
RatingsDbTable:
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PROVISIONED
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: '1'
WriteCapacityUnits: '1'
TableName: Review
Type: AWS::DynamoDB::Table
Transform: AWS::Serverless-2016-10-31
CodePipeline 中的第三阶段可能如下所示:
{
"Name": "Deploy",
"Actions": [
{
"Name": "Beta",
"ActionTypeId": {
"Category": "Deploy",
"Owner": "AWS",
"Provider": "CloudFormation",
"Version": 1
},
"Configuration": {
"ActionMode": "CREATE_UPDATE",
"Capabilities": "CAPABILITY_IAM",
"RoleArn": "CloudformationRole.Arn",
"StackName": "Harvest",
"TemplatePath": "BuildOutput::sam-template.yml",
"ParameterOverrides": "{\"appid\": \"${app123456}\", \"url\": \"https://apple.com\"}"
},
"InputArtifacts": [
{
"Name": "BuildOutput"
}
],
"RunOrder": 1
}
]
}
jar 和模板在 BuildOutput
工件包中可用,因为您在 buildspec.yml 中指定了它们。只要你有BuildOutput
(或SourceOutput
)作为InputArtifacts
,你就可以像上面看到的那样使用它们。