Override ENV Variable for CodePipeline

Viewed 485

I have a lambda that use for kicking off CodeBuild and I set overrides for some environment variables (download paths mostly).

Now I'm migrating over to CodePipeline and I'm trying to figure out how to do the same thing.

For CodeBuild I would do environmentVariablesOverride and then specify the ENV variables I want. I can't find any examples that show I can do the same thing with CodePipeline. Was hoping someone could give me some direction on how I might accomplish this.

Appreciate the help!

1 Answers

The basic use case of passing variables in a pipeline in Cloudformation:

  1. I can have a parameter MyVar1:
AWSTemplateFormatVersion: "2010-09-09"
Description: CodePipeline
Parameters:
  MyVar1:
   Type: String
   Default: "Whatever
  1. Within the pipeline definition I can have an action in a stage that passes that var to a codebuild project:
- Name: Testing
    Actions:
    - Name: Testing
        ActionTypeId:
        Category: Build
        Owner: AWS
        Provider: CodeBuild
        Version: "1"
        Configuration:
        ProjectName: DeployProject
        EnvironmentVariables: !Sub |
            [
            {"name":MY_VAR_1","value":"${MYVAR1}","type":"PLAINTEXT"}
            ]
        InputArtifacts:
        - Name: SourceArtifact
        Namespace: DeployVars
        RunOrder: 1

Note Namespace 'Deployvars'

  1. In my codebuild project I can now reference ${MY_VAR_1}
  DeployProject:
    Type: AWS::CodeBuild::Project
    Properties:
      Name: DeployProject
      Description: Deploy Project

      Artifacts:
        Type: CODEPIPELINE

      Environment:

        ComputeType: BUILD_GENERAL1_MEDIUM
        Image: aws/codebuild/standard:4.0
        PrivilegedMode: true
        Type: LINUX_CONTAINER
        EnvironmentVariables: []

      ServiceRole: !GetAtt MyRole.Arn
      Source:
        BuildSpec: |
          version: 0.2
          env:
            exported-variables:
                - MY_VAR_2
          phases:
            install:
              commands:
                - env
                - ls -al
                - MY_VAR_2='this is a test'

            build:
              commands:
                - echo ${MY_VAR_1}
        Type: CODEPIPELINE

Note I have set and exported ${MY_VAR_2}

  1. I can pass ${MY_VAR_2} around in the pipeline definition by referencing it's namespace.
- Name: Staging
    Actions:
    - Name: Staging
        ActionTypeId:
        Category: Build
        Owner: AWS
        Provider: CodeBuild
        Version: "1"
        Configuration:
        ProjectName: MyOtherProject
        EnvironmentVariables: !Sub |
            [
            {"name":MY_OTHER_VAR","value":"${DeployVars.MY_VAR_2}","type":"PLAINTEXT"}
            ]
        InputArtifacts:
        - Name: SourceArtifact
        RunOrder: 1

Related