4

I want to tag a commit with dynamic tag, for example git tag deploy-$(date +"%Y-%m-%d_%H-%M-%S"), and then trigger codepipeline from cloudwatch. The problem is if I use following cloudwatch event pattern:

{
  "source": [
    "aws.codecommit"
  ],
  "detail-type": [
    "CodeCommit Repository State Change"
  ],
  "resources": [
    "arn:aws:codecommit:region:XXX:someName"
  ],
  "detail": {
    "event": [
      "referenceCreated",
      "referenceUpdated"
    ],
    "repositoryName": [
      "someName"
    ],
    "referenceType": [
      "tag"
    ],
    "referenceName": [
      "deploy"
    ]
  }
}

it will be triggered only on specific tag - "deploy". Is there a way to say any tag that starts with(contains) deploy keyword?

datahack
  • 477
  • 1
  • 11
  • 32

2 Answers2

6

I was looking at the Content-based Filtering with Event Patterns docs and its Prefix Matching. The example given in the docs is:

{
  "time": [ { "prefix": "2017-10-02" } ],
}

Based on the example, the following seems as it could work in your case:

{
  "source": [
    "aws.codecommit"
  ],
  "detail-type": [
    "CodeCommit Repository State Change"
  ],
  "resources": [
    "arn:aws:codecommit:region:XXX:someName"
  ],
  "detail": {
    "event": [
      "referenceCreated",
      "referenceUpdated"
    ],
    "repositoryName": [
      "someName"
    ],
    "referenceType": [
      "tag"
    ],
    "referenceName": [
       { "prefix": "deploy" } ]
    ]
  }
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Thank you for your reply, but unfortunately it's not working. I already tried this way and I get error "Validation error. Details: Event pattern contains invalid element (can only be Strings enclosed in quotes, numbers, and the unquoted keywords true, false, and null)" – datahack Sep 08 '20 at 06:49
  • @datahack Could you check please in the EventBridge Console, not CloudWatch Events. I had no problems there creating such a rule. I didn't check its functionality, but EB does not complain about the rule with `{ "prefix": "deploy" }`. – Marcin Sep 08 '20 at 07:11
  • Ok now i'm really confused. AWS states following: "Amazon EventBridge (formerly CloudWatch Events) provides all functionality from CloudWatch Events...". I did not expect new service console :) I will try and let you know if its working. Thank you! – datahack Sep 08 '20 at 07:50
  • @datahack No problem. Check how it works and let me know it will go please. – Marcin Sep 08 '20 at 07:54
  • 1
    Working! Thanks! – datahack Sep 08 '20 at 08:01
  • @datahack Glad to hear that:-) – Marcin Sep 08 '20 at 08:01
  • @Marcin can we adjust the EventBridge such that it triggers pipeline when a matching string appears on suffix. I am planning to trigger the pipeline when it is tagged in the format v0.0.2-develop V0.0.2-staging v0.0.2-product – sandeep krishna Mar 01 '21 at 07:58
  • @sandeepkrishna Not sure now. I think you could ask new question with relevant details. – Marcin Mar 01 '21 at 08:01
  • 1
    @Marcin There is an extra bracket in line `"deploy" } ]` – Shyam Jos May 15 '21 at 16:03
0

You can use a wildcard in the prefix:

{
  "source": [
    "aws.codecommit"
  ],
  "detail-type": [
    "CodeCommit Repository State Change"
  ],
  "detail": {
    "referenceType": [
      "tag"
    ],
    "referenceName": [
      {
        "prefix": "deploy-*"
      }
    ]
  }
}
shariqmaws
  • 8,152
  • 1
  • 16
  • 35