3
votes

I am using AWS codepipeline as CI/CD pipeline tool. There are many stages in my yml file. The cloudformation reference for codepipeline is: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codepipeline-pipeline.html

...
Stages:
  - Name: ...
  Actions: ...

...

There are a list of stages in the pipeline but not all of them are required to run for every branch. How can I skip some of them based on branch name?

I tried below configuration which is passed by parameters via aws cloudformation deploy --parameter-overrides ... command. It works but if I change the parameter in the deploy command, it shows No changes to deploy.. It seems that the template doesn't update the parameter change.

Parameters:
    ShouldRunTest:
       Type: String
       Default: false

Conditions:
    ShouldRunTest: !Equals [!Ref ShouldRunTest, "true"]

...


        - !If
          - ShouldRunTest
          - Name: test
            Actions:
              - Name: Deploy
                ActionTypeId:
                  Category: Deploy
                  Owner: AWS
                  Provider: CloudFormation
                  Version: 1
                InputArtifacts:
                  - Name: SourceOutput
                Configuration:
                  Capabilities: CAPABILITY_NAMED_IAM
                 
                  ActionMode: REPLACE_ON_FAILURE
                  RoleArn: !GetAtt CodePipelineServiceRole.Arn
                  StackName: codeBuild
                RunOrder: 1
          - !Ref "AWS::NoValue"
1
Is it cloudformation yaml?Marcin
yes it is cloudformation yaml.Joey Yi Zhao

1 Answers

4
votes

You can skip entire CP actions or stages using conditions in your CFN template.

For example:

Conditions:

  HaveCodeDeployStage:
    !Not [ !Equals [!Ref CodeDeployAppName, ''] ]  

Resources:

  MyCodePipepline:
    Type: AWS::CodePipeline::Pipeline
    Properties:
      #... 
      Stages: 
        - !If
          - HaveCodeDeployStage
          - Name: DeployStage
            Actions: 
              - ActionTypeId: 
                  Category: Deploy
                  Owner: AWS
                  Provider: CodeDeployToECS
                  Version: 1
                  #....
          - !Ref "AWS::NoValue"
        # ...