jenkinsci / stashnotifier-plugin

A Jenkins Plugin to notify Atlassian Stash|Bitbucket of build results
https://plugins.jenkins.io/stashNotifier/
Other
163 stars 136 forks source link

Different multibranch pipelines use the same key #175

Open Wilfred opened 6 years ago

Wilfred commented 6 years ago

Suppose I have two separate multibranch pipeline jobs, foo-unit and foo-integration. When I push a branch to master, the following notifications are sent to Stash:

{
    "state": "SUCCESSFUL",
    "key": "master-http:\\/\\/ci.dev.example\\/",
    "name": "foo-unit / master #11",
    "description": "built by Jenkins @ http://ci.dev.example/",
    "url": "http://ci.dev.example/job/foo-unit/job/master/11/"
}
{
    "state": "SUCCESSFUL",
    "key": "master-http:\\/\\/ci.dev.example\\/",
    "name": "foo-integration / master #11",
    "description": "built by Jenkins @ http://ci.dev.example/",
    "url": "http://ci.dev.example/job/foo-integration/job/master/11/"
}

These both have the same key. As a result, Stash uses the last build and ignores the first.

I can't use includeBuildNumberInKey because if build 1 of foo-integration is red, but build 2 of foo-integration is green, Stash thinks they are distinct jobs. This gives a key of the form "master-2-http:\\/\\/ci.dev.example\\/".

If I use step([$class: 'StashNotifier', prependParentProjectKey: true]) I get a key that's unique to the current build:

{
    "state": "SUCCESSFUL",
    "key": "foo-integration-master-http:\\/\\/ci.dev.example\\/",
    "name": "foo-integration / master #11",
    "description": "built by Jenkins @ http://ci.dev.example/",
    "url": "http://ci.dev.example/job/foo-integration/job/master/11/"
}

However, this is subtle gotcha. It would be nice if this plugin used the build name, not the branch name, for multibranch pipelines.

I think the correct solution would be to change StashNotifier#getDefaultBuildKey to use something like if build instanceOf MultiBranchProject, but this would introduce a dependency on that plugin.

What do you think is a good solution here?

scaytrase commented 6 years ago

Hi, @Wilfred

Can you try override projectKey value for the notifier manually for your needs for both jobs?

this one https://github.com/jenkinsci/stashnotifier-plugin/blob/release/1.x/src/main/java/org/jenkinsci/plugins/stashNotifier/StashNotifier.java#L151

Wilfred commented 6 years ago

Yep, setting the projectKey for just one job of my two jobs works as a solution.

However, this requires every user with multiple jobs that are multibranch pipelines to do the same thing. Ideally stashnotifier-plugin wouldn't require every user to set projectKey or prependParentProjectKey.

scaytrase commented 6 years ago

Initially, prepend parent job key option was introduced in #60 by me for solving your initial problem - create unique keys for multibranch projects operating the same repo. I'm not sure that configuring single option is a really big problem to make this plugin hardly tied to multibranch plugin. If you have a decision without declaring a dependency - PR is always welcome

Wilfred commented 6 years ago

@scaytrase OK, how about this?

If env.BRANCH_NAME is the same as the project name, then we automatically prepend the parent job key?

scaytrase commented 6 years ago

How do you resolve false positives, i.e classic non-multibranch job, named master building only master branch?

I think it's ok to introduce some optional global-configured project key guesser, but it should be off by default anyway for BC reasons to not get any key renamed unintentionally

Wilfred commented 6 years ago

If there's a classic non-multibranch job named master, prepending the parent job key is harmless because StashNotifier handles the parent being null.

If the classic non-multibranch job is in a folder, then the the parent job key will be non-null, but that seems harmless to me.