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

Separate pipelines for each service

This commit is contained in:
ebosas
2021-11-06 19:21:13 +02:00
parent 4edbc05098
commit 518f0f8c23
3 changed files with 361 additions and 316 deletions

View File

@@ -0,0 +1,16 @@
version: 0.2
phases:
pre_build:
commands:
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
- TAG=$(echo $GIT_COMMIT_ID | head -c 8)
- IMAGE_URI=$REPO_URI:$TAG
build:
commands:
- docker build -t $IMAGE_URI -f $SERVICE.Dockerfile .
post_build:
commands:
- docker push $IMAGE_URI
- printf '{"ImageUri":"%s"}' $IMAGE_URI > build.json
artifacts:
files: build.json

View File

@@ -10,6 +10,7 @@ Parameters:
Type: String Type: String
EnvironmentName: EnvironmentName:
Type: String Type: String
Default: production
DeploymentType: DeploymentType:
Type: String Type: String
Default: fargate Default: fargate
@@ -19,16 +20,16 @@ Resources:
# Create ECR respositories to hold built docker images # Create ECR respositories to hold built docker images
ServerRepository: ServerRepository:
Type: AWS::ECR::Repository Type: AWS::ECR::Repository
Properties: DeletionPolicy: Retain
RepositoryName: !Sub ${EnvironmentName}-server UpdateReplacePolicy: Retain
CacheRepository: CacheRepository:
Type: AWS::ECR::Repository Type: AWS::ECR::Repository
Properties: DeletionPolicy: Retain
RepositoryName: !Sub ${EnvironmentName}-cache UpdateReplacePolicy: Retain
DatabaseRepository: DatabaseRepository:
Type: AWS::ECR::Repository Type: AWS::ECR::Repository
Properties: DeletionPolicy: Retain
RepositoryName: !Sub ${EnvironmentName}-database UpdateReplacePolicy: Retain
# A role used to give CodeBuild permission to access code, # A role used to give CodeBuild permission to access code,
# build it, and upload the build results to ECR # build it, and upload the build results to ECR
@@ -161,322 +162,357 @@ Resources:
# While the build is in progress we need a place to store artifacts # While the build is in progress we need a place to store artifacts
ArtifactBucket: ArtifactBucket:
Type: AWS::S3::Bucket Type: AWS::S3::Bucket
Properties: DeletionPolicy: Retain
BucketName: !Sub ${EnvironmentName}-${AWS::AccountId} UpdateReplacePolicy: Retain
# Build a service # Build a service
CodeBuildProject: CodeBuildProject:
Type: AWS::CodeBuild::Project Type: AWS::CodeBuild::Project
DependsOn: SourceCreds
Properties: Properties:
Artifacts: Artifacts:
Type: CODEPIPELINE Type: NO_ARTIFACTS
Source: Source:
Type: CODEPIPELINE Type: GITHUB
BuildSpec: | Location: !Sub https://github.com/${GitHubUser}/${GitHubRepo}.git
version: 0.2 BuildSpec: deployments/buildspec/service.yml
phases: # SourceVersion: !Ref GitHubBranch
pre_build:
commands:
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
- TAG=$(echo $GIT_COMMIT_ID | head -c 8)
- IMAGE_URI=$REPO_URI:$TAG
build:
commands:
- docker build -t $IMAGE_URI -f server.Dockerfile .
post_build:
commands:
- docker push $IMAGE_URI
- printf '{"ImageUri":"%s"}' $IMAGE_URI > build.json
artifacts:
files: build.json
Environment: Environment:
ComputeType: BUILD_GENERAL1_SMALL ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:5.0 Image: aws/codebuild/standard:5.0
Type: LINUX_CONTAINER Type: LINUX_CONTAINER
PrivilegedMode: true PrivilegedMode: true
EnvironmentVariables:
- Name: varName
Value: varValue
Triggers:
Webhook: true
FilterGroups:
- - Type: EVENT
Pattern: PUSH
ExcludeMatchedPattern: false
- Type: HEAD_REF
Pattern: !Sub ^refs/heads/${GitHubBranch}$
ExcludeMatchedPattern: false
- Type: FILE_PATH
Pattern: ^cmd/server/.*
ExcludeMatchedPattern: false
- Type: FILE_PATH
Pattern: ^internal/.*
ExcludeMatchedPattern: false
- Type: FILE_PATH
Pattern: ^web/.*
ExcludeMatchedPattern: false
- Type: FILE_PATH
Pattern: ^server.Dockerfile$
ExcludeMatchedPattern: false
ServiceRole: !Ref CodeBuildServiceRole ServiceRole: !Ref CodeBuildServiceRole
TimeoutInMinutes: 10
# Create three pipelines for the three services
# Server pipeline # GitHub source credentials
PipelineServer: SourceCreds:
Type: AWS::CodePipeline::Pipeline Type: AWS::CodeBuild::SourceCredential
Properties: Properties:
RoleArn: !GetAtt CodePipelineServiceRole.Arn Token: !Ref GitHubToken
ArtifactStore: ServerType: GITHUB
Type: S3 AuthType: PERSONAL_ACCESS_TOKEN
Location: !Ref ArtifactBucket
Stages:
# Pull the source code from the Github repository
- Name: Source
Actions:
- Name: Source
Namespace: SourceVariables
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
Configuration:
Owner: !Ref GitHubUser
Repo: !Ref GitHubRepo
Branch: !Ref GitHubBranch
OAuthToken: !Ref GitHubToken
OutputArtifacts:
- Name: Source
RunOrder: 1
# Build a service image
- Name: Build
Actions:
- Name: Build
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CodeBuildProject
EnvironmentVariables: !Sub |
[
{
"name":"AWS_DEFAULT_REGION",
"value":"${AWS::Region}",
"type":"PLAINTEXT"
},
{
"name":"AWS_ACCOUNT_ID",
"value":"${AWS::AccountId}",
"type":"PLAINTEXT"
},
{
"name":"REPO_URI",
"value":"${ServerRepository.RepositoryUri}",
"type":"PLAINTEXT"
},
{
"name":"GIT_COMMIT_ID",
"value":"#{SourceVariables.CommitId}",
"type":"PLAINTEXT"
}
]
InputArtifacts:
- Name: Source
OutputArtifacts:
- Name: BuildOutput
RunOrder: 1
# Deploy the service to the ECS/Fargate cluster
- Name: Deploy
Actions:
- Name: Deploy
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
ActionMode: CREATE_UPDATE
RoleArn: !GetAtt CloudFormationDeployRole.Arn
StackName: !Sub ${EnvironmentName}-ServerService
TemplatePath: !Sub Source::deployments/services-${DeploymentType}/server.yml
Capabilities: CAPABILITY_IAM
ParameterOverrides: !Sub |
{
"EnvironmentName": "${EnvironmentName}",
"ImageUrl": {
"Fn::GetParam" : ["BuildOutput", "build.json", "ImageUri"]
}
}
InputArtifacts:
- Name: Source
- Name: BuildOutput
# The cache pipeline # # Create three pipelines for the three services
PipelineCache: # # Server pipeline
Type: AWS::CodePipeline::Pipeline # PipelineServer:
Properties: # Type: AWS::CodePipeline::Pipeline
RoleArn: !GetAtt CodePipelineServiceRole.Arn # Properties:
ArtifactStore: # RoleArn: !GetAtt CodePipelineServiceRole.Arn
Type: S3 # ArtifactStore:
Location: !Ref ArtifactBucket # Type: S3
Stages: # Location: !Ref ArtifactBucket
# Pull the source code from the Github repository # Stages:
- Name: Source # # Pull the source code from the Github repository
Actions: # - Name: Source
- Name: Source # Actions:
Namespace: SourceVariables # - Name: Source
ActionTypeId: # Namespace: SourceVariables
Category: Source # ActionTypeId:
Owner: ThirdParty # Category: Source
Version: 1 # Owner: ThirdParty
Provider: GitHub # Version: 1
Configuration: # Provider: GitHub
Owner: !Ref GitHubUser # Configuration:
Repo: !Ref GitHubRepo # Owner: !Ref GitHubUser
Branch: !Ref GitHubBranch # Repo: !Ref GitHubRepo
OAuthToken: !Ref GitHubToken # Branch: !Ref GitHubBranch
OutputArtifacts: # OAuthToken: !Ref GitHubToken
- Name: Source # OutputArtifacts:
RunOrder: 1 # - Name: Source
# Build a service image # RunOrder: 1
- Name: Build # # Build a service image
Actions: # - Name: Build
- Name: Build # Actions:
ActionTypeId: # - Name: Build
Category: Build # ActionTypeId:
Owner: AWS # Category: Build
Version: 1 # Owner: AWS
Provider: CodeBuild # Version: 1
Configuration: # Provider: CodeBuild
ProjectName: !Ref CodeBuildProject # Configuration:
EnvironmentVariables: !Sub | # ProjectName: !Ref CodeBuildProject
[ # EnvironmentVariables: !Sub |
{ # [
"name":"AWS_DEFAULT_REGION", # {
"value":"${AWS::Region}", # "name":"AWS_DEFAULT_REGION",
"type":"PLAINTEXT" # "value":"${AWS::Region}",
}, # "type":"PLAINTEXT"
{ # },
"name":"AWS_ACCOUNT_ID", # {
"value":"${AWS::AccountId}", # "name":"AWS_ACCOUNT_ID",
"type":"PLAINTEXT" # "value":"${AWS::AccountId}",
}, # "type":"PLAINTEXT"
{ # },
"name":"REPO_URI", # {
"value":"${CacheRepository.RepositoryUri}", # "name":"SERVICE",
"type":"PLAINTEXT" # "value":"server",
}, # "type":"PLAINTEXT"
{ # },
"name":"GIT_COMMIT_ID", # {
"value":"#{SourceVariables.CommitId}", # "name":"REPO_URI",
"type":"PLAINTEXT" # "value":"${ServerRepository.RepositoryUri}",
} # "type":"PLAINTEXT"
] # },
InputArtifacts: # {
- Name: Source # "name":"GIT_COMMIT_ID",
OutputArtifacts: # "value":"#{SourceVariables.CommitId}",
- Name: BuildOutput # "type":"PLAINTEXT"
RunOrder: 1 # }
# Deploy the service to the ECS/Fargate cluster # ]
- Name: Deploy # InputArtifacts:
Actions: # - Name: Source
- Name: Deploy # OutputArtifacts:
ActionTypeId: # - Name: BuildOutput
Category: Deploy # RunOrder: 1
Owner: AWS # # Deploy the service to the ECS/Fargate cluster
Version: 1 # - Name: Deploy
Provider: CloudFormation # Actions:
Configuration: # - Name: Deploy
ActionMode: CREATE_UPDATE # ActionTypeId:
RoleArn: !GetAtt CloudFormationDeployRole.Arn # Category: Deploy
StackName: !Sub ${EnvironmentName}-CacheService # Owner: AWS
TemplatePath: !Sub Source::deployments/services-${DeploymentType}/cache.yml # Version: 1
Capabilities: CAPABILITY_IAM # Provider: CloudFormation
ParameterOverrides: !Sub | # Configuration:
{ # ActionMode: CREATE_UPDATE
"EnvironmentName": "${EnvironmentName}", # RoleArn: !GetAtt CloudFormationDeployRole.Arn
"ImageUrl": { # StackName: !Sub ${EnvironmentName}-ServerService
"Fn::GetParam" : ["BuildOutput", "build.json", "ImageUri"] # TemplatePath: !Sub Source::deployments/services-${DeploymentType}/server.yml
} # Capabilities: CAPABILITY_IAM
} # ParameterOverrides: !Sub |
InputArtifacts: # {
- Name: Source # "EnvironmentName": "${EnvironmentName}",
- Name: BuildOutput # "ImageUrl": {
# "Fn::GetParam" : ["BuildOutput", "build.json", "ImageUri"]
# }
# }
# InputArtifacts:
# - Name: Source
# - Name: BuildOutput
# The database pipeline # # The cache pipeline
PipelineDatabase: # PipelineCache:
Type: AWS::CodePipeline::Pipeline # Type: AWS::CodePipeline::Pipeline
Properties: # Properties:
RoleArn: !GetAtt CodePipelineServiceRole.Arn # RoleArn: !GetAtt CodePipelineServiceRole.Arn
ArtifactStore: # ArtifactStore:
Type: S3 # Type: S3
Location: !Ref ArtifactBucket # Location: !Ref ArtifactBucket
Stages: # Stages:
# Pull the source code from the Github repository # # Pull the source code from the Github repository
- Name: Source # - Name: Source
Actions: # Actions:
- Name: Source # - Name: Source
Namespace: SourceVariables # Namespace: SourceVariables
ActionTypeId: # ActionTypeId:
Category: Source # Category: Source
Owner: ThirdParty # Owner: ThirdParty
Version: 1 # Version: 1
Provider: GitHub # Provider: GitHub
Configuration: # Configuration:
Owner: !Ref GitHubUser # Owner: !Ref GitHubUser
Repo: !Ref GitHubRepo # Repo: !Ref GitHubRepo
Branch: !Ref GitHubBranch # Branch: !Ref GitHubBranch
OAuthToken: !Ref GitHubToken # OAuthToken: !Ref GitHubToken
OutputArtifacts: # OutputArtifacts:
- Name: Source # - Name: Source
RunOrder: 1 # RunOrder: 1
# Build a service image # # Build a service image
- Name: Build # - Name: Build
Actions: # Actions:
- Name: Build # - Name: Build
ActionTypeId: # ActionTypeId:
Category: Build # Category: Build
Owner: AWS # Owner: AWS
Version: 1 # Version: 1
Provider: CodeBuild # Provider: CodeBuild
Configuration: # Configuration:
ProjectName: !Ref CodeBuildProject # ProjectName: !Ref CodeBuildProject
EnvironmentVariables: !Sub | # EnvironmentVariables: !Sub |
[ # [
{ # {
"name":"AWS_DEFAULT_REGION", # "name":"AWS_DEFAULT_REGION",
"value":"${AWS::Region}", # "value":"${AWS::Region}",
"type":"PLAINTEXT" # "type":"PLAINTEXT"
}, # },
{ # {
"name":"AWS_ACCOUNT_ID", # "name":"AWS_ACCOUNT_ID",
"value":"${AWS::AccountId}", # "value":"${AWS::AccountId}",
"type":"PLAINTEXT" # "type":"PLAINTEXT"
}, # },
{ # {
"name":"REPO_URI", # "name":"SERVICE",
"value":"${DatabaseRepository.RepositoryUri}", # "value":"cache",
"type":"PLAINTEXT" # "type":"PLAINTEXT"
}, # },
{ # {
"name":"GIT_COMMIT_ID", # "name":"REPO_URI",
"value":"#{SourceVariables.CommitId}", # "value":"${CacheRepository.RepositoryUri}",
"type":"PLAINTEXT" # "type":"PLAINTEXT"
} # },
] # {
InputArtifacts: # "name":"GIT_COMMIT_ID",
- Name: Source # "value":"#{SourceVariables.CommitId}",
OutputArtifacts: # "type":"PLAINTEXT"
- Name: BuildOutput # }
RunOrder: 1 # ]
# Deploy the service to the ECS/Fargate cluster # InputArtifacts:
- Name: Deploy # - Name: Source
Actions: # OutputArtifacts:
- Name: Deploy # - Name: BuildOutput
ActionTypeId: # RunOrder: 1
Category: Deploy # # Deploy the service to the ECS/Fargate cluster
Owner: AWS # - Name: Deploy
Version: 1 # Actions:
Provider: CloudFormation # - Name: Deploy
Configuration: # ActionTypeId:
ActionMode: CREATE_UPDATE # Category: Deploy
RoleArn: !GetAtt CloudFormationDeployRole.Arn # Owner: AWS
StackName: !Sub ${EnvironmentName}-DatabaseService # Version: 1
TemplatePath: !Sub Source::deployments/services-${DeploymentType}/database.yml # Provider: CloudFormation
Capabilities: CAPABILITY_IAM # Configuration:
ParameterOverrides: !Sub | # ActionMode: CREATE_UPDATE
{ # RoleArn: !GetAtt CloudFormationDeployRole.Arn
"EnvironmentName": "${EnvironmentName}", # StackName: !Sub ${EnvironmentName}-CacheService
"ImageUrl": { # TemplatePath: !Sub Source::deployments/services-${DeploymentType}/cache.yml
"Fn::GetParam" : ["BuildOutput", "build.json", "ImageUri"] # Capabilities: CAPABILITY_IAM
} # ParameterOverrides: !Sub |
} # {
InputArtifacts: # "EnvironmentName": "${EnvironmentName}",
- Name: Source # "ImageUrl": {
- Name: BuildOutput # "Fn::GetParam" : ["BuildOutput", "build.json", "ImageUri"]
# }
# }
# InputArtifacts:
# - Name: Source
# - Name: BuildOutput
Outputs: # # The database pipeline
PipelineServerUrl: # PipelineDatabase:
Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${PipelineServer} # Type: AWS::CodePipeline::Pipeline
PipelineCacheUrl: # Properties:
Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${PipelineCache} # RoleArn: !GetAtt CodePipelineServiceRole.Arn
PipelineDatabaseUrl: # ArtifactStore:
Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${PipelineDatabase} # Type: S3
# Location: !Ref ArtifactBucket
# Stages:
# # Pull the source code from the Github repository
# - Name: Source
# Actions:
# - Name: Source
# Namespace: SourceVariables
# ActionTypeId:
# Category: Source
# Owner: ThirdParty
# Version: 1
# Provider: GitHub
# Configuration:
# Owner: !Ref GitHubUser
# Repo: !Ref GitHubRepo
# Branch: !Ref GitHubBranch
# OAuthToken: !Ref GitHubToken
# OutputArtifacts:
# - Name: Source
# RunOrder: 1
# # Build a service image
# - Name: Build
# Actions:
# - Name: Build
# ActionTypeId:
# Category: Build
# Owner: AWS
# Version: 1
# Provider: CodeBuild
# Configuration:
# ProjectName: !Ref CodeBuildProject
# EnvironmentVariables: !Sub |
# [
# {
# "name":"AWS_DEFAULT_REGION",
# "value":"${AWS::Region}",
# "type":"PLAINTEXT"
# },
# {
# "name":"AWS_ACCOUNT_ID",
# "value":"${AWS::AccountId}",
# "type":"PLAINTEXT"
# },
# {
# "name":"SERVICE",
# "value":"database",
# "type":"PLAINTEXT"
# },
# {
# "name":"REPO_URI",
# "value":"${DatabaseRepository.RepositoryUri}",
# "type":"PLAINTEXT"
# },
# {
# "name":"GIT_COMMIT_ID",
# "value":"#{SourceVariables.CommitId}",
# "type":"PLAINTEXT"
# }
# ]
# InputArtifacts:
# - Name: Source
# OutputArtifacts:
# - Name: BuildOutput
# RunOrder: 1
# # Deploy the service to the ECS/Fargate cluster
# - Name: Deploy
# Actions:
# - Name: Deploy
# ActionTypeId:
# Category: Deploy
# Owner: AWS
# Version: 1
# Provider: CloudFormation
# Configuration:
# ActionMode: CREATE_UPDATE
# RoleArn: !GetAtt CloudFormationDeployRole.Arn
# StackName: !Sub ${EnvironmentName}-DatabaseService
# TemplatePath: !Sub Source::deployments/services-${DeploymentType}/database.yml
# Capabilities: CAPABILITY_IAM
# ParameterOverrides: !Sub |
# {
# "EnvironmentName": "${EnvironmentName}",
# "ImageUrl": {
# "Fn::GetParam" : ["BuildOutput", "build.json", "ImageUri"]
# }
# }
# InputArtifacts:
# - Name: Source
# - Name: BuildOutput
# Outputs:
# PipelineServerUrl:
# Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${PipelineServer}
# PipelineCacheUrl:
# Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${PipelineCache}
# PipelineDatabaseUrl:
# Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${PipelineDatabase}

View File

@@ -10,6 +10,7 @@ Parameters:
Type: String Type: String
EnvironmentName: EnvironmentName:
Type: String Type: String
Default: production
DeploymentType: DeploymentType:
Type: String Type: String
Default: fargate Default: fargate
@@ -21,20 +22,14 @@ Resources:
Type: AWS::ECR::Repository Type: AWS::ECR::Repository
DeletionPolicy: Retain DeletionPolicy: Retain
UpdateReplacePolicy: Retain UpdateReplacePolicy: Retain
Properties:
RepositoryName: !Sub ${EnvironmentName}-server
CacheRepository: CacheRepository:
Type: AWS::ECR::Repository Type: AWS::ECR::Repository
DeletionPolicy: Retain DeletionPolicy: Retain
UpdateReplacePolicy: Retain UpdateReplacePolicy: Retain
Properties:
RepositoryName: !Sub ${EnvironmentName}-cache
DatabaseRepository: DatabaseRepository:
Type: AWS::ECR::Repository Type: AWS::ECR::Repository
DeletionPolicy: Retain DeletionPolicy: Retain
UpdateReplacePolicy: Retain UpdateReplacePolicy: Retain
Properties:
RepositoryName: !Sub ${EnvironmentName}-database
# A role used to give CodeBuild permission to access code, # A role used to give CodeBuild permission to access code,
# build it, and upload the build results to ECR # build it, and upload the build results to ECR
@@ -169,8 +164,6 @@ Resources:
Type: AWS::S3::Bucket Type: AWS::S3::Bucket
DeletionPolicy: Retain DeletionPolicy: Retain
UpdateReplacePolicy: Retain UpdateReplacePolicy: Retain
Properties:
BucketName: !Sub ${EnvironmentName}-${AWS::AccountId}
# This is the definition of how to build the code in the repository # This is the definition of how to build the code in the repository
CodeBuildProject: CodeBuildProject: