kiegroup / github-action-build-chain

86 stars 24 forks source link

Check if another PR exists to cancel a build #200

Open danielezonca opened 2 years ago

danielezonca commented 2 years ago

In Kogito we are creating multiple jobs for each PR to test in parallel different repos and see if the change breaks one of the downstream components. This is useful but it is not necessary if it is a multi repo PR because the same build will be executed also from that repo so it is a very inefficient way to use the (limited) GitHub action executors that we have.

i.e. https://github.com/kiegroup/kogito-runtimes/pull/1703 image i.e. in this case OptaPlanner job https://github.com/kiegroup/optaplanner/pull/1662 image

It should be useful to have an optional check that for each job:

This check has to be opt-it (like skip-if-duplicated : true)

I was thinking about creating a different action for this but it is strictly related to the github-build-chain because it has to know which is the target project that could be different from the current repo (i.e kogito-apps build in kogito-runtimes repo)

danielezonca commented 2 years ago

@Ginxo What about this? ^

Ginxo commented 2 years ago

I like your proposal!! :love_you_gesture: I will definitely implement it. Thanks a lot @danielezonca !! This is actually a very good idea!!

Ginxo commented 2 years ago

thinking about this double... it's a good thing to have, but don't you think were are breaking the single-responsability principle here :thinking: we are talking about jobs here and how to handle them in a more efficient way which is great and I'm ok with it, but I wouldn't delegate this responsibility to build-chain since its execution is just part of the job itself. I would bet to move the functionality you (we) require to the job but to this tool. wdyt?

Ginxo commented 2 years ago

now how to do it from the job configuration...? I don't know and that's probably the reason why we are discussing to move it here... I don't see any other option but to move it here (or to a different tool, but somehow here) sorry for bothering you @danielezonca I guess I used you as my rubber duck :smile:

Ginxo commented 2 years ago

Once we get the list of repositories to be checked it would be about getting the workflows from those repositories like https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/workflows

{
  "total_count": 4,
  "workflows": [
    {
      "id": 3949243,
      "node_id": "MDg6V29ya2Zsb3czOTQ5MjQz",
      "name": "Build Chain Generate Files",
      "path": ".github/workflows/generate_files.yml",
      "state": "active",
      "created_at": "2020-12-01T11:08:00.000Z",
      "updated_at": "2020-12-01T11:08:00.000Z",
      "url": "https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/workflows/3949243",
      "html_url": "https://github.com/kiegroup/droolsjbpm-build-bootstrap/blob/main/.github/workflows/generate_files.yml",
      "badge_url": "https://github.com/kiegroup/droolsjbpm-build-bootstrap/workflows/Build%20Chain%20Generate%20Files/badge.svg"
    },
...

then getting the jobs for this particular branch and this particular workflow https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/runs?event=pull_request&branch=dependabot/maven/org.jsoup-jsoup-1.14.2&status=in_progress (probably use owner query field here)

Note: the only (best) way to identify similar workflows is by the name, so workflows to be cancelled should share the same name

{
  "total_count": 1,
  "workflow_runs": [
    {
      "id": 1160247910,
      "name": "Build Chain",
      "node_id": "WFR_kwLOABUYTs5FJ_pm",
      "head_branch": "dependabot/maven/org.jsoup-jsoup-1.14.2",
      "head_sha": "2087efcd59f29ae898203686deaad0b35553e25b",
      "run_number": 700,
      "event": "pull_request",
      "status": "completed",
      "conclusion": "success",
      "workflow_id": 2033882,
      "check_suite_id": 3574465069,
      "check_suite_node_id": "MDEwOkNoZWNrU3VpdGUzNTc0NDY1MDY5",
      "url": "https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/runs/1160247910",
      "html_url": "https://github.com/kiegroup/droolsjbpm-build-bootstrap/actions/runs/1160247910",
      "workflow_url": "https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/workflows/2033882",
      "jobs_url": "https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/runs/1160247910/jobs"

and then from jobs_url getting the job information to check whether the build-chain step has already started, if this is the case then we will cancel this job since another job has already started.

{
  "total_count": 2,
  "jobs": [
    {
      "id": 3404916091,
      "run_id": 1160247910,
      "run_url": "https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/runs/1160247910",
      "run_attempt": 1,
      "node_id": "MDg6Q2hlY2tSdW4zNDA0OTE2MDkx",
      "head_sha": "2087efcd59f29ae898203686deaad0b35553e25b",
      "url": "https://api.github.com/repos/kiegroup/droolsjbpm-build-bootstrap/actions/jobs/3404916091",
      "html_url": "https://github.com/kiegroup/droolsjbpm-build-bootstrap/runs/3404916091?check_suite_focus=true",
      "status": "completed",
      "conclusion": "success",
      "started_at": "2021-08-23T21:09:50Z",
      "completed_at": "2021-08-23T21:10:42Z",
      "name": "Maven Build (11)",
      "steps": [
        {
          "name": "Set up job",
          "status": "completed",
          "conclusion": "success",
          "number": 1,
          "started_at": "2021-08-23T21:09:50.000Z",
          "completed_at": "2021-08-23T21:09:54.000Z"
        },
        {
          "name": "Set up JDK",
          "status": "completed",
          "conclusion": "success",
          "number": 2,
          "started_at": "2021-08-23T21:09:54.000Z",
          "completed_at": "2021-08-23T21:10:00.000Z"
        },
        {
          "name": "Cache Maven packages",
          "status": "completed",
          "conclusion": "success",
          "number": 3,
          "started_at": "2021-08-23T21:10:00.000Z",
          "completed_at": "2021-08-23T21:10:01.000Z"
        },
        {
          "name": "Build Chain 11",
          "status": "completed",
          "conclusion": "success",
          "number": 4,
          "started_at": "2021-08-23T21:10:01.000Z",
          "completed_at": "2021-08-23T21:10:37.000Z"
        },

Filtering the steps by name (names should match)

Note: It could be the case where two jobs from different repositories are checking each other at the same time and both of them are cancelled. I don't really know how to solve this scenario, any idea @danielezonca