1
0
mirror of https://github.com/ebosas/microservices.git synced 2025-08-24 20:08:55 +02:00

Create codebuild trigger

This commit is contained in:
ebosas
2021-11-12 19:25:01 +02:00
parent 7e5dbc5f75
commit 1449143807
2 changed files with 109 additions and 97 deletions

View File

@@ -60,7 +60,7 @@ Resources:
DependsOn: DependsOn:
- SSMArtifactBucket - SSMArtifactBucket
- SSMCodePipelineServiceRoleArn - SSMCodePipelineServiceRoleArn
- SSMCloudFormationDeployRole # - SSMCloudFormationDeployRole
Properties: Properties:
RoleArn: !GetAtt CodePipelineServiceRole.Arn RoleArn: !GetAtt CodePipelineServiceRole.Arn
ArtifactStore: ArtifactStore:
@@ -179,8 +179,8 @@ Resources:
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# Services # Services
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# - Name: Services - Name: Services
# Actions: Actions:
# - Name: Server # - Name: Server
# ActionTypeId: # ActionTypeId:
@@ -196,8 +196,8 @@ Resources:
# Capabilities: CAPABILITY_IAM # Capabilities: CAPABILITY_IAM
# ParameterOverrides: !Sub | # ParameterOverrides: !Sub |
# { # {
# "EnvironmentName": "${EnvironmentName}",
# "ServiceName": "server", # "ServiceName": "server",
# "EnvironmentName": "${EnvironmentName}",
# "ImageUrl": "amazon/amazon-ecs-sample" # "ImageUrl": "amazon/amazon-ecs-sample"
# } # }
# InputArtifacts: # InputArtifacts:
@@ -217,33 +217,35 @@ Resources:
# Capabilities: CAPABILITY_IAM # Capabilities: CAPABILITY_IAM
# ParameterOverrides: !Sub | # ParameterOverrides: !Sub |
# { # {
# "EnvironmentName": "${EnvironmentName}",
# "ServiceName": "cache", # "ServiceName": "cache",
# "EnvironmentName": "${EnvironmentName}",
# "ImageUrl": "amazon/amazon-ecs-sample" # "ImageUrl": "amazon/amazon-ecs-sample"
# } # }
# InputArtifacts: # InputArtifacts:
# - Name: Source # - Name: Source
# - Name: Database - Name: Database
# ActionTypeId: ActionTypeId:
# Category: Deploy Category: Deploy
# Owner: AWS Owner: AWS
# Version: 1 Version: 1
# Provider: CloudFormation Provider: CloudFormation
# Configuration: Configuration:
# ActionMode: CREATE_UPDATE ActionMode: CREATE_UPDATE
# RoleArn: !GetAtt CloudFormationDeployRole.Arn RoleArn: !GetAtt CloudFormationDeployRole.Arn
# StackName: !Sub ${EnvironmentName}-Database-Service StackName: !Sub ${EnvironmentName}-Database-Service
# TemplatePath: !Sub Source::deployments/services-${DeploymentType}/database.yml TemplatePath: !Sub Source::deployments/services-${DeploymentType}/database.yml
# Capabilities: CAPABILITY_IAM Capabilities: CAPABILITY_IAM
# ParameterOverrides: !Sub | # Don't have an image at this point, using a sample image
# { ParameterOverrides: !Sub |
# "EnvironmentName": "${EnvironmentName}", {
# "ServiceName": "database", "ServiceName": "database",
# "ImageUrl": "amazon/amazon-ecs-sample" "EnvironmentName": "${EnvironmentName}",
# } "DeploymentType": "${DeploymentType}",
# InputArtifacts: "ImageUrl": "amazon/amazon-ecs-sample"
# - Name: Source }
InputArtifacts:
- Name: Source
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
@@ -266,8 +268,8 @@ Resources:
# Capabilities: CAPABILITY_IAM # Capabilities: CAPABILITY_IAM
# ParameterOverrides: !Sub | # ParameterOverrides: !Sub |
# { # {
# "ServiceName": "server",
# "EnvironmentName": "${EnvironmentName}", # "EnvironmentName": "${EnvironmentName}",
# "ServiceName": "server",
# "DeploymentType": "${DeploymentType}", # "DeploymentType": "${DeploymentType}",
# "TriggerMessagePattern": "[(BuildServer|BuildAll)]", # "TriggerMessagePattern": "[(BuildServer|BuildAll)]",
# "GitHubRepo": "${GitHubRepo}", # "GitHubRepo": "${GitHubRepo}",
@@ -292,8 +294,8 @@ Resources:
# Capabilities: CAPABILITY_IAM # Capabilities: CAPABILITY_IAM
# ParameterOverrides: !Sub | # ParameterOverrides: !Sub |
# { # {
# "ServiceName": "cache",
# "EnvironmentName": "${EnvironmentName}", # "EnvironmentName": "${EnvironmentName}",
# "ServiceName": "cache",
# "DeploymentType": "${DeploymentType}", # "DeploymentType": "${DeploymentType}",
# "TriggerMessagePattern": "[(BuildCache|BuildAll)]", # "TriggerMessagePattern": "[(BuildCache|BuildAll)]",
# "GitHubRepo": "${GitHubRepo}", # "GitHubRepo": "${GitHubRepo}",
@@ -318,8 +320,8 @@ Resources:
Capabilities: CAPABILITY_IAM Capabilities: CAPABILITY_IAM
ParameterOverrides: !Sub | ParameterOverrides: !Sub |
{ {
"ServiceName": "database",
"EnvironmentName": "${EnvironmentName}", "EnvironmentName": "${EnvironmentName}",
"ServiceName": "database",
"DeploymentType": "${DeploymentType}", "DeploymentType": "${DeploymentType}",
"TriggerMessagePattern": "[(BuildDatabase|BuildAll)]", "TriggerMessagePattern": "[(BuildDatabase|BuildAll)]",
"GitHubRepo": "${GitHubRepo}", "GitHubRepo": "${GitHubRepo}",
@@ -462,12 +464,12 @@ Resources:
Name: /microservices/codepipeline_service_role_arn Name: /microservices/codepipeline_service_role_arn
Type: String Type: String
Value: !GetAtt CodePipelineServiceRole.Arn Value: !GetAtt CodePipelineServiceRole.Arn
SSMCloudFormationDeployRole: # SSMCloudFormationDeployRole:
Type: AWS::SSM::Parameter # Type: AWS::SSM::Parameter
Properties: # Properties:
Name: /microservices/cloudformation_deploy_role # Name: /microservices/cloudformation_deploy_role
Type: String # Type: String
Value: !Ref CloudFormationDeployRole # Value: !Ref CloudFormationDeployRole
Outputs: Outputs:
PipelineUrl: PipelineUrl:

View File

@@ -29,9 +29,9 @@ Parameters:
CodePipelineServiceRoleArn: CodePipelineServiceRoleArn:
Type: AWS::SSM::Parameter::Value<String> Type: AWS::SSM::Parameter::Value<String>
Default: /microservices/codepipeline_service_role_arn Default: /microservices/codepipeline_service_role_arn
CloudFormationDeployRole: # CloudFormationDeployRole:
Type: AWS::SSM::Parameter::Value<String> # Type: AWS::SSM::Parameter::Value<String>
Default: /microservices/cloudformation_deploy_role # Default: /microservices/cloudformation_deploy_role
Resources: Resources:
@@ -106,17 +106,32 @@ Resources:
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# CodePipeline # CodePipeline
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# CloudWatchEventRule:
# Type: AWS::Events::Rule
# Properties:
# EventPattern:
# detail:
# action-type: [PUSH]
# image-tag: [latest]
# repository-name: [!Ref Repository]
# result: [SUCCESS]
# detail-type: [ECR Image Action]
# source: [aws.ecr]
# Targets:
# - Arn: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${Pipeline}
# RoleArn: !GetAtt CloudWatchEventRole.Arn
# Id: server-pipeline
# https://docs.aws.amazon.com/codebuild/latest/userguide/sample-build-notifications.html#sample-build-notifications-ref
CloudWatchEventRule: CloudWatchEventRule:
Type: AWS::Events::Rule Type: AWS::Events::Rule
Properties: Properties:
EventPattern: EventPattern:
detail-type: [CodeBuild Build State Change]
source: [aws.codebuild]
detail: detail:
action-type: [PUSH] build-status: [SUCCEEDED]
image-tag: [latest] project-name: [!Ref CodeBuildProject]
repository-name: [!Ref Repository]
result: [SUCCESS]
detail-type: [ECR Image Action]
source: [aws.ecr]
Targets: Targets:
- Arn: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${Pipeline} - Arn: !Sub arn:aws:codepipeline:${AWS::Region}:${AWS::AccountId}:${Pipeline}
RoleArn: !GetAtt CloudWatchEventRole.Arn RoleArn: !GetAtt CloudWatchEventRole.Arn
@@ -151,34 +166,34 @@ Resources:
- Name: Source - Name: Source
Actions: Actions:
- Name: Image
Namespace: ImageVariables
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: ECR
Configuration:
RepositoryName: !Ref Repository
ImageTag: latest
OutputArtifacts:
- Name: Image
RunOrder: 1
# - Name: Image # - Name: Image
# Namespace: ImageVariables
# ActionTypeId: # ActionTypeId:
# Category: Source # Category: Source
# Owner: AWS # Owner: AWS
# Version: 1 # Version: 1
# Provider: S3 # Provider: ECR
# Configuration: # Configuration:
# S3Bucket: !Ref ArtifactBucket # RepositoryName: !Ref Repository
# S3ObjectKey: !Sub imagedefinitions/${ServiceName}.zip # ImageTag: latest
# PollForSourceChanges: false
# OutputArtifacts: # OutputArtifacts:
# - Name: Image # - Name: Image
# RunOrder: 1 # RunOrder: 1
- Name: Image
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: S3
Configuration:
S3Bucket: !Ref ArtifactBucket
S3ObjectKey: !Sub imagedefinitions/${ServiceName}.zip
PollForSourceChanges: false
OutputArtifacts:
- Name: Image
RunOrder: 1
- Name: Code - Name: Code
ActionTypeId: ActionTypeId:
Category: Source Category: Source
@@ -193,31 +208,10 @@ Resources:
PollForSourceChanges: false PollForSourceChanges: false
OutputArtifacts: OutputArtifacts:
- Name: Code - Name: Code
RunOrder: 1
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# Deploy # Deploy
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# - Name: Deploy
# Actions:
# - Name: Deploy
# ActionTypeId:
# Category: Deploy
# Owner: AWS
# Version: 1
# Provider: ECS
# Configuration:
# ClusterName:
# Fn::ImportValue: !Sub ${EnvironmentName}:ClusterName
# ServiceName: !Ref ServiceName
# # Needs imagedefinitions.json but ECR produces imageDetail.json.
# # Need a build stage to build images or transform imageDetail.json
# # as described here: https://stackoverflow.com/a/57015190
# # Or use blue/green deployment
# FileName: imagedefinitions.json
# InputArtifacts:
# - Name: Image
- Name: Deploy - Name: Deploy
Actions: Actions:
- Name: Deploy - Name: Deploy
@@ -225,22 +219,38 @@ Resources:
Category: Deploy Category: Deploy
Owner: AWS Owner: AWS
Version: 1 Version: 1
Provider: CloudFormation Provider: ECS
Configuration: Configuration:
ActionMode: CREATE_UPDATE ClusterName:
RoleArn: !Ref CloudFormationDeployRole Fn::ImportValue: !Sub ${EnvironmentName}:ClusterName
StackName: !Sub ${EnvironmentName}-Server-Service ServiceName: !Ref ServiceName
TemplatePath: !Sub Code::deployments/services-${DeploymentType}/server.yml FileName: imagedefinitions.json
Capabilities: CAPABILITY_IAM
ParameterOverrides: !Sub |
{
"EnvironmentName": "${EnvironmentName}",
"ServiceName": "${ServiceName}",
"ImageUrl": "${Repository.RepositoryUri}@#{ImageVariables.ImageDigest}"
}
InputArtifacts: InputArtifacts:
- Name: Image - Name: Image
- Name: Code
# - Name: Deploy
# Actions:
# - Name: Deploy
# ActionTypeId:
# Category: Deploy
# Owner: AWS
# Version: 1
# Provider: CloudFormation
# Configuration:
# ActionMode: CREATE_UPDATE
# RoleArn: !Ref CloudFormationDeployRole
# StackName: !Sub ${EnvironmentName}-Server-Service
# TemplatePath: !Sub Code::deployments/services-${DeploymentType}/server.yml
# Capabilities: CAPABILITY_IAM
# ParameterOverrides: !Sub |
# {
# "EnvironmentName": "${EnvironmentName}",
# "ServiceName": "${ServiceName}",
# "ImageUrl": "${Repository.RepositoryUri}@#{ImageVariables.ImageDigest}"
# }
# InputArtifacts:
# - Name: Image
# - Name: Code
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# Role for CodeBuild service # Role for CodeBuild service
@@ -268,13 +278,13 @@ Resources:
- logs:CreateLogStream - logs:CreateLogStream
- logs:PutLogEvents - logs:PutLogEvents
- ecr:GetAuthorizationToken - ecr:GetAuthorizationToken
- Resource: !Sub arn:aws:s3:::${ArtifactBucket}/* - Resource: !Sub arn:aws:s3:::${ArtifactBucket}*
Effect: Allow Effect: Allow
Action: Action:
- s3:GetObject - s3:GetObject
- s3:PutObject - s3:PutObject
- s3:GetObjectVersion - s3:GetObjectVersion
- Resource: "*" # !GetAtt Repository.Arn - Resource: !GetAtt Repository.Arn
Effect: Allow Effect: Allow
Action: Action:
- ecr:GetDownloadUrlForLayer - ecr:GetDownloadUrlForLayer