Joel Barenco
Joel Barenco

Reputation: 323

Pass AWS CodePipeline variable to actions

I'm trying to pass the CodePipeline variable #{codepipeline PipelineExecutionId} to both the codeBuild action and then a deploy action.

I understand this variable is readily available to all actions as explained in AWS docs.

I'm however having a difficulty with the syntax as the parameters is not passed thru into the actions.

I've been using the following code:

For the build actions:

        - Name: "Build-Docker-Container"
          Actions:
            - Name: "Build-Docker-Container"
              ActionTypeId:
                Category: Build
                Owner: AWS
                Provider: CodeBuild
                Version: "1"
              Configuration:
                ProjectName: !Sub ${ProjectName}-build-${BranchName}
                EnvironmentVariables:
                  - Name: IMAGE_TAG
                    Type: PLAINTEXT
                    Value: "#{codepipeline.PipelineExecutionId}"
              InputArtifacts:
                - Name: !Ref ProjectName
              RunOrder: 3

and for the deploy action:

        - Name: "Deploy-Services"
          Actions:
            - Name: "Deploy-Services"
              ActionTypeId:
                Category: Deploy
                Owner: AWS
                Provider: CloudFormation
                Version: "1"
              Configuration:
                ActionMode: CREATE_UPDATE
                StackName: !Sub "${ProjectName}-services-${BranchName}"
                TemplatePath: !Sub "${ProjectName}::aws/03-services.yml"
                Capabilities: "CAPABILITY_NAMED_IAM"
                RoleArn: !GetAtt DeployRole.Arn
                ParameterOverrides: !Sub |
                  {
                    "ProjectName": "${ProjectName}",
                    "ExecutionId": "#{codepipeline.PipelineExecutionId}"
                  }
              InputArtifacts:
                - Name: !Ref ProjectName
                - Name: InfrastructureOutput
              RunOrder: 4

UPDATE The code was actually good; I just needed to update the CloudFormation pipeline stack to apply it (I thought the github webhook would trigger this, but it only update the actions inside the pipeline)

Upvotes: 3

Views: 1711

Answers (1)

Marcin
Marcin

Reputation: 238199

I can confirm that the syntax you've used in the deploy action is correct:

                ParameterOverrides: !Sub |
                  {
                    "ProjectName": "${ProjectName}",
                    "ExecutionId": "#{codepipeline.PipelineExecutionId}"
                  }

I verified that on my Pipeline with CloudFormation provider. I can also confirm that this works as expected.

You can check in console if you edit the CFN action's in question, that the parameters are correctly set:

enter image description here

For now I can't verify build action, but by the look of it, it also seems fine.

Upvotes: 2

Related Questions