从代码管道的上一步设置CodeBuild env var

时间:2019-08-23 19:06:59

标签: amazon-cloudformation aws-codepipeline aws-codebuild

我有以下Codepipeline(cloudformation模板代码段)。

我的BuildDeployWebappTest步骤需要在上一个ExecuteChangeSet步骤中创建的s3存储桶的名称。

如您所见,BuildDeployWebappTest步骤使用WebappCodeBuildProjectTestStage CodeBuild项目配置。 WEBAPP_S3_BUCKET环境变量需要设置为在ExecuteChangeSet步骤中创建的存储桶名称的值。

我该如何实现?

当前,我必须在此管道之外创建S3存储桶,并将S3存储桶名称“硬编码”作为CloudFormation参数(TestStageS3Bucket)到下面的管道。

ExecuteChangeSet产生一个名为WebAppBucket的CloudFormation输出。如果需要,我可以export

我知道CodeBuild支持多个InputArtifacts,但是我不知道如何在CodeBuild项目配置中或CodeBuild buildspec.yml文件本身中引用CloudFormation OutputArtifact

...
  WebappCodeBuildProjectTestStage:
    Type: AWS::CodeBuild::Project
    Properties:
      Artifacts:
        Type: CODEPIPELINE
      Environment:
        ComputeType: BUILD_GENERAL1_SMALL
        PrivilegedMode: false
        Type: LINUX_CONTAINER
        Image: !Ref CodeBuildImage
        EnvironmentVariables:          
          - Name: WEBAPP_S3_BUCKET
            Value: !Ref TestStageS3Bucket
          - Name: APP_STAGE
            Value: test
          - Name: ANGULAR_BUILD
            Value: test            
      ServiceRole: !Ref CodeBuildRole
      Source:
        Type: CODEPIPELINE
        BuildSpec: !Ref WebappBuildspecPath
      TimeoutInMinutes: !Ref BuildTimeout
...
Pipeline:
    Type: AWS::CodePipeline::Pipeline
    Properties:
      ArtifactStore:
        Location: !Ref 'ArtifactStoreBucket'
        Type: S3
      DisableInboundStageTransitions: []
      Name: !Ref AWS::StackName
      RoleArn: !GetAtt PipelineRole.Arn
      Stages:
        - Name: Source
          Actions:
            - Name: Source
              ActionTypeId:
                Category: Source
                Owner: ThirdParty
                Provider: GitHub
                Version: '1'
              OutputArtifacts:
                - Name: MyAppCode
              Configuration:
                Owner: !Ref GithubOrg
                Repo: !Select [ 0, !Split [ '--', !Ref 'AWS::StackName' ] ]
                PollForSourceChanges: false
                Branch: !Select [ 1, !Split [ '--', !Ref 'AWS::StackName' ] ]
                OAuthToken: !Ref GithubOAuthToken
              RunOrder: 1   
        - Name: DeployTestResources
          Actions:
            - Name: CreateChangeSet
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Provider: CloudFormation
                Version: '1'
              InputArtifacts:
                - Name: MyAppCode           
              Configuration:
                ActionMode: CHANGE_SET_REPLACE
                RoleArn: !GetAtt CFNRole.Arn
                Capabilities: CAPABILITY_IAM
                StackName: !Sub "${AWS::StackName}--test--gen"
                ChangeSetName: !Sub "${AWS::StackName}--test--changeset"
                TemplatePath: MyAppCode::aws/cloudformation/template.yml
                TemplateConfiguration: !Sub "MyAppCode::${TestCloudFormationTemplateParameters}"                
              RunOrder: 1
            - Name: ExecuteChangeSet
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Provider: CloudFormation
                Version: '1'
              Configuration:
                ActionMode: CHANGE_SET_EXECUTE
                RoleArn: !GetAtt CFNRole.Arn
                StackName: !Sub "${AWS::StackName}--test--gen"
                ChangeSetName: !Sub "${AWS::StackName}--test--changeset"
                OutputFileName: TestOutput.json
              OutputArtifacts:
                - Name: DeployTestResourcesOutput                     
              RunOrder: 2
          - Name: BuildDeployWebappTest
            Actions:
              - Name: CodeBuild
                InputArtifacts:
                  - Name: MyAppCode
                ActionTypeId:
                  Category: Build
                  Owner: AWS
                  Provider: CodeBuild
                  Version: '1'
                Configuration:
                  ProjectName: !Ref WebappCodeBuildProjectTestStage
                RunOrder: 1    

0 个答案:

没有答案