ajoberstar / reckon

Infer a project's version from your Git repository.
Apache License 2.0
187 stars 28 forks source link

Build insignificant version when suppliyng scope, for Jenkinsfile declarative pipeline? #60

Closed ieugen closed 6 years ago

ieugen commented 6 years ago

Hello,

How can I make reckon generate insignificant versions when supplying an (empty) stage ?

I'm using reckon to build continuous integration with Jenkins Pipeline via Jenkinsfile and I (believe I) need it to generate insignificant versions when not supplying a scope.

I have configured reckon like this:

reckon {
    normal = scopeFromProp()
    preRelease = stageFromProp('dev', 'rc', 'final')
}

And in my Jenkinsfile I have something like this:

pipeline {
  agent any
  environment { }
  parameters {
    string(name: 'releaseScope', defaultValue: 'minor', description: 'Reckon (gradle plugin) release scope.')
    string(name: 'releaseStage', defaultValue: 'dev', description: 'Reckon (gradle plugin) release stage.')
  }

  stages {
    stage('Checkout') {     steps {    checkout scm    }    }

    stage('Build'){
      steps {
        echo "Building... ${env.BRANCH_NAME}"
        sh "./gradlew clean build -Preckon.scope=${params.releaseScope} -Preckon.stage=${params.releaseStage} --stacktrace"
      }
    }

The issue that I'm having is that with default build triggering, the stage is set to 'dev' and that creates a significant version with a tag and everything, which is not something I wish to have when building non-final versions.

I run it the build with the above configuration, this is executed:

./gradlew clean build -Preckon.scope=minor -Preckon.stage=dev --stacktrace

It gives me a version like this 1.8.0-dev.1 .

I have tried not to supply the stage, or supply an empty string but it does not work:

./gradlew clean build -Preckon.scope=minor -Preckon.stage= --stacktrace
.....
Stage "" is not one of: [rc, dev, final]

Thank you,

p.s. You're awesome for making reckon and all the other stuff.

ieugen commented 6 years ago

I've managed to figgure out a workaround by doing some groovy string interpolation:

${params.releaseScope ? '' : '-Preckon.stage=' + params.releaseStage }

I think it would be nice to treat empty string as non-specified and I believe the code responsible for this is here:

  public PreReleaseStrategy stageFromProp(String... stages) {
    Set<String> stageSet = Arrays.stream(stages).collect(Collectors.toSet());
    BiFunction<VcsInventory, Version, Optional<String>> supplier =
        (inventory, targetNormal) ->
            Optional.ofNullable(project.findProperty(STAGE_PROP)).map(Object::toString);
    return new StagePreReleaseStrategy(stageSet, supplier);
}

It makes Optional.ofNullable() from an empty string. It would be nice to filter empty strings props, don't you think?

https://github.com/ajoberstar/reckon/blob/master/reckon-gradle/src/main/java/org/ajoberstar/reckon/gradle/ReckonExtension.java

ajoberstar commented 6 years ago

Thanks for providing all of the feedback and issues so far. Glad to have someone interested in using the plugin.

I agree about changing it to treat an empty stage (probably the same for scope) as an empty Optional. I'll mark this as an enhancement to pull in in the next release. If you have time, you're certainly welcome to submit a PR. Otherwise, I'll tackle it when I do my next round of fixes to reckon.

cduque89 commented 6 years ago

Hi @ajoberstar.

About this issue as well, how can we only run the reckon plugin on the jenkinspipeline?

Basically on our local development environment we don't want to generate .jar files with the reckon version. Only on the jenkins pipeline prior to do a release we want to use the reckon plugin.

Do you have any suggestion for this behaviour?

Thanks for the help.

ieugen commented 6 years ago

Hi @cduque89 , how you build the jar name is not reckon part. It's gradle part . Search the web. [1] https://stackoverflow.com/questions/31405818/want-to-specify-jar-name-and-version-both-in-build-gradle

ieugen commented 6 years ago

@ajoberstar : I tried to fix the bug but did not manage. It's not fixable from that place. I did not have time to look deeper into this. Sorry.

ajoberstar commented 6 years ago

No problem @ieugen.

ajoberstar commented 6 years ago

@cduque89 Is the JAR name (as @ieugen mentions) what you were referring to for your local development? If not, what do you want the version to look like locally?