aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.5k stars 3.85k forks source link

[aws-codepipeline-actions] Enable setting filters on github source action #10265

Open fongie opened 4 years ago

fongie commented 4 years ago

I would like to be able to set the webhook filters on the source action in my code pipeline

Use Case

I want to trigger the pipeline only when certain files are changed (aka the purpose of the filters)

Proposed Solution

Codebuild module seems to support setting webhook filters, but I cannot find a way to do this on the GitHubSourceAction that I use as the source for my code pipelines. Looking at the code for codepipeline-actions module, it seems like the webhook is set here. Maybe you could re-use some of the Codebuild classes to implement this for Codepipeline source actions too?

Other


This is a :rocket: Feature Request

fongie commented 4 years ago

maybe this is harder than I thought.. codepipeline webhook filters seem to work differently from codebuild ones?

edit: I actually wanted to trigger based on file paths modified, but it appears that this is not supported at all in codepipeline since "matchEquals" doesnt support regex? Then I assume the reason you don't support this feature is because it doesn't really allow you to do much else than what setting "branch" does.. Am I right?

skinny85 commented 4 years ago

Hey @fongie ,

does the GitHub webhook API actually contain the modified files when it does the call?

Thanks, Adam

fongie commented 4 years ago

Hi!

Yes, I'm pretty sure it does. By looking at my last webhook invocation from a CDK-created pipeline, I think you should be able to get the needed information from the "commits" key, under the subkeys "added", "removed", and "modified".

This is the structure from my latest webhook invocation:

{
  "ref": "..",
  "before": "sha..",
  "after": "sha..",
  "repository": {
    "stuff": "..lots of stuff"
  },
  "pusher": {
    "name": "fongie",
    "email": ".."
  },
  "organization": {
     "stuff": "..lots of stuff"
  },
  "sender": {
     "stuff": "..lots of stuff"
  },
  "created": false,
  "deleted": false,
  "forced": false,
  "base_ref": null,
  "compare": "...",
  "commits": [
    {
      "id": "sha",
      "tree_id": "id",
      "distinct": true,
      "message": "readd tsconfig",
      "timestamp": "2020-09-09T19:03:23+02:00",
      "url": "url",
      "author": {
        "name": "fongie",
        "email": "..",
        "username": "fongie"
      },
      "committer": {
        "name": "fongie",
        "email": "..",
        "username": ".."
      },
      "added": [
        "tsconfig.json"
      ],
      "removed": [

      ],
      "modified": [
        ".gitignore"
      ]
    }
  ],
  "head_commit": {
    "id": "id",
    "tree_id": "id",
    "distinct": true,
    "message": "readd tsconfig",
    "timestamp": "2020-09-09T19:03:23+02:00",
    "url": "url",
    "author": {
      "name": "fongie",
      "email": "..",
      "username": "fongie"
    },
    "committer": {
      "name": "fongie",
      "email": "..",
      "username": "fongie"
    },
    "added": [
      "tsconfig.json"
    ],
    "removed": [

    ],
    "modified": [
      ".gitignore"
    ]
  }
}

Might not be the best example, but the full file path shows up (it could be src/lib/my-thing.ts for example)

In fact, I created a jsonpath expression that could select the "added", "removed", and "modified" files without much difficulty as a long list (.. I may have gotten rid of it when I found out it wouldnt work though) as described here, and then what remains would be simply to match each with a regex. But then I understood that "matchEquals" doesnt support regex or glob type expressions, only exact matches (?) which makes me think that this is not something we could solve "just" using CDK?

It would be really nice if you could though!

fongie commented 4 years ago

I have worked around this by creating my own construct which creates a pipeline and a codebuild project "in front" of the pipeline with the filters I wanted

  1. source updates
  2. codebuild triggers if changed files match (as documented here)
  3. the codebuild buildspec simply contains an aws cli command to start the pipeline

but it would of course be nice if you could specify the filters right away on the pipeline instead =)

edit: I have open sourced a construct that does this here available on npm, or you can see some quick code at this gist if you want to write your own :)

TharaldBye commented 3 years ago

It would be very good if the webhook filter for aws cdk codepipeline supported 'pattern' and 'excludeMatchedPattern' like codebuild webhook does. https://docs.aws.amazon.com/codebuild/latest/APIReference/API_WebhookFilter.html

Currently only jsonPath and matchEquals properties are supported with aws cdk codepipeline. MatchEquals takes a simple string property which makes it impossible to match paths or file patterns from jsonPath output.

adriantaut commented 3 years ago

hey, are there any updates or plans on this?

skinny85 commented 3 years ago

Hey @adriantaut,

unfortunately, this first needs to be done in CodePipeline first (this is a missing capability currently).

If you have Premium Support, I believe you can open a Feature Request to the CodePipeline service.

Thanks, Adam

adriantaut commented 3 years ago

@skinny85 Thank you for your suggestion, Adam! Will try to get in touch with the AWS team. Cheers! 👍

LeoLapworthKT commented 3 years ago

https://blog.cuckoo.org/aws-pipelines-with-github-release-trigger-in-cdk - which shows setting of the webhook filters - this also relates to https://github.com/aws/aws-cdk/issues/8111

coderbyheart commented 3 years ago

We also need a way to match booleans, because in the GitHub release event, the properties draft and prerelease are boolean values which indicate the type of the release. In my case I am only interested to trigger a pipeline if the release is not a draft or pre-release.

github-actions[bot] commented 3 months ago

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.