在 AWS CodePipeline 中访问 AWS CodeBuild 变量
Access AWS CodeBuild Variables in AWS CodePipeline
我正在尝试使用 AWS CodeBuild Id 作为我的 docker 图像的标签。此 docker 图像是在 CodeBuild 的构建阶段构建的。我想获取这个 Coudebuild Id,它是我在 AWS Code Pipeline 阶段的 docker 标签。我如何在 aws codepipeline 中访问这些代码构建环境变量?
代码构建阶段:
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Location: !Ref ArtifactBucket
Type: "S3"
Source:
Location: !Sub ${ArtifactBucket}/source.zip
Type: "S3"
BuildSpec: |
version: 0.1
phases:
pre_build:
commands:
- $(aws ecr get-login --region $AWS_DEFAULT_REGION)
- sudo apt-get update
- echo Pulling maven image...
- docker pull maven:3.3-jdk-8
- echo done with the pre build phase
build:
commands:
- echo Build started on `date`
- printf "%s" $REPOSITORY_URI
- docker run -i --rm -w /opt/maven -v $PWD:/opt/maven -v $HOME/.m2:/root/.m2 maven:3.3-jdk-8 mvn clean install
- docker build --file Dockerfile --tag $REPOSITORY_URI:$CODEBUILD_BUILD_ID .
post_build:
commands:
- echo post build
- docker push $REPOSITORY_URI:$CODEBUILD_BUILD_ID
discard-paths: yes
Environment:
ComputeType: "BUILD_GENERAL1_LARGE"
Image: "aws/codebuild/docker:1.12.1"
Type: "LINUX_CONTAINER"
EnvironmentVariables:
- Name: AWS_DEFAULT_REGION
Value: !Ref AWS::Region
- Name: REPOSITORY_URI
Value: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}
- Name: PipelineName
Value: !Ref PipelineName
Name: !Ref AWS::StackName
ServiceRole: !Ref CodeBuildServiceRole
这里我的 docker 图像现在是我的存储库 Url 和我的代码构建 ID 的组合。我想在aws codepipeline的部署阶段使用这个codebuild id,如何获取?
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: !Ref PipelineName
RoleArn: !GetAtt CodePipelineServiceRole.Arn
ArtifactStore:
Type: S3
Location: !Ref ArtifactBucket
Stages:
- Name: Source
Actions:
- Name: GitHubRepoSource
ActionTypeId:
Category: Source
Owner: ThirdParty
Provider: GitHub
Version: 1
Configuration:
Owner: !Ref GitHubUser
Repo: !Ref GitHubRepo
Branch: !Ref GitHubBranch
OAuthToken: !Ref GitHubToken
OutputArtifacts:
- Name: GitHubRepoSource
RunOrder: 1
- Name: Build
Actions:
- Name: Build
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CodeBuildProject
InputArtifacts:
- Name: GitHubRepoSource
OutputArtifacts:
- Name: BuildOutput
RunOrder: 1
- Name: Deploy
Actions:
- Name: Deploy
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
ChangeSetName: Deploy
ActionMode: CREATE_UPDATE
StackName: !Sub "${AWS::StackName}-Service"
Capabilities: CAPABILITY_NAMED_IAM
TemplatePath: https://s3.amazonaws.com/cicdoveraws-visa/service.yaml
RoleArn: !GetAtt CloudFormationExecutionRole.Arn
ParameterOverrides: !Sub |
{
"Tag" : "${}",
"DesiredCount": "2",
"Cluster": "${ECSCluster}",
"TargetGroup": "${ECSTG}",
"ImageName": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}:<Tag Name>,
"ContainerName": "${ContainerName}",
"Cpu": "${Cpu}",
"Memory": "${Memory}",
"ContainerPort": "${ContainerPort}"
}
InputArtifacts:
- Name: BuildOutput
RunOrder: 1
您可以在代码构建的 post_build 阶段编写带有标签信息的 build.json 文件
post_build:
commands:
- echo post build
- docker push $REPOSITORY_URI:$CODEBUILD_BUILD_ID
- printf '{"Tag":"%s"}' "$REPOSITORY_URI:$CODEBUILD_BUILD_ID" > /tmp/build.json
artifacts:
files: /tmp/build.json
discard-paths: yes
在您的管道中,您现在可以按如下方式简单地读取您的代码:
ParameterOverrides: !Sub |
{
"Tag" : { "Fn::GetParam" : [ "BuildOutput", "build.json", "Tag" ] },
.........
}
我正在尝试使用 AWS CodeBuild Id 作为我的 docker 图像的标签。此 docker 图像是在 CodeBuild 的构建阶段构建的。我想获取这个 Coudebuild Id,它是我在 AWS Code Pipeline 阶段的 docker 标签。我如何在 aws codepipeline 中访问这些代码构建环境变量?
代码构建阶段:
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Location: !Ref ArtifactBucket
Type: "S3"
Source:
Location: !Sub ${ArtifactBucket}/source.zip
Type: "S3"
BuildSpec: |
version: 0.1
phases:
pre_build:
commands:
- $(aws ecr get-login --region $AWS_DEFAULT_REGION)
- sudo apt-get update
- echo Pulling maven image...
- docker pull maven:3.3-jdk-8
- echo done with the pre build phase
build:
commands:
- echo Build started on `date`
- printf "%s" $REPOSITORY_URI
- docker run -i --rm -w /opt/maven -v $PWD:/opt/maven -v $HOME/.m2:/root/.m2 maven:3.3-jdk-8 mvn clean install
- docker build --file Dockerfile --tag $REPOSITORY_URI:$CODEBUILD_BUILD_ID .
post_build:
commands:
- echo post build
- docker push $REPOSITORY_URI:$CODEBUILD_BUILD_ID
discard-paths: yes
Environment:
ComputeType: "BUILD_GENERAL1_LARGE"
Image: "aws/codebuild/docker:1.12.1"
Type: "LINUX_CONTAINER"
EnvironmentVariables:
- Name: AWS_DEFAULT_REGION
Value: !Ref AWS::Region
- Name: REPOSITORY_URI
Value: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}
- Name: PipelineName
Value: !Ref PipelineName
Name: !Ref AWS::StackName
ServiceRole: !Ref CodeBuildServiceRole
这里我的 docker 图像现在是我的存储库 Url 和我的代码构建 ID 的组合。我想在aws codepipeline的部署阶段使用这个codebuild id,如何获取?
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: !Ref PipelineName
RoleArn: !GetAtt CodePipelineServiceRole.Arn
ArtifactStore:
Type: S3
Location: !Ref ArtifactBucket
Stages:
- Name: Source
Actions:
- Name: GitHubRepoSource
ActionTypeId:
Category: Source
Owner: ThirdParty
Provider: GitHub
Version: 1
Configuration:
Owner: !Ref GitHubUser
Repo: !Ref GitHubRepo
Branch: !Ref GitHubBranch
OAuthToken: !Ref GitHubToken
OutputArtifacts:
- Name: GitHubRepoSource
RunOrder: 1
- Name: Build
Actions:
- Name: Build
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
Configuration:
ProjectName: !Ref CodeBuildProject
InputArtifacts:
- Name: GitHubRepoSource
OutputArtifacts:
- Name: BuildOutput
RunOrder: 1
- Name: Deploy
Actions:
- Name: Deploy
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
ChangeSetName: Deploy
ActionMode: CREATE_UPDATE
StackName: !Sub "${AWS::StackName}-Service"
Capabilities: CAPABILITY_NAMED_IAM
TemplatePath: https://s3.amazonaws.com/cicdoveraws-visa/service.yaml
RoleArn: !GetAtt CloudFormationExecutionRole.Arn
ParameterOverrides: !Sub |
{
"Tag" : "${}",
"DesiredCount": "2",
"Cluster": "${ECSCluster}",
"TargetGroup": "${ECSTG}",
"ImageName": "${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}:<Tag Name>,
"ContainerName": "${ContainerName}",
"Cpu": "${Cpu}",
"Memory": "${Memory}",
"ContainerPort": "${ContainerPort}"
}
InputArtifacts:
- Name: BuildOutput
RunOrder: 1
您可以在代码构建的 post_build 阶段编写带有标签信息的 build.json 文件
post_build:
commands:
- echo post build
- docker push $REPOSITORY_URI:$CODEBUILD_BUILD_ID
- printf '{"Tag":"%s"}' "$REPOSITORY_URI:$CODEBUILD_BUILD_ID" > /tmp/build.json
artifacts:
files: /tmp/build.json
discard-paths: yes
在您的管道中,您现在可以按如下方式简单地读取您的代码:
ParameterOverrides: !Sub |
{
"Tag" : { "Fn::GetParam" : [ "BuildOutput", "build.json", "Tag" ] },
.........
}