ajoberstar / reckon

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

Pass version to other tools ? #77

Closed ieugen closed 6 years ago

ieugen commented 6 years ago

Hello,

We are using Jenkinsfile to automate builds on Jenkins and also deployments to a Kubernetes cluster. How can we pass the project version from reckon to the project?

Right now we are using a gradle task that writes all the build properties to a properties file that we then load in our Jenkins file. While this works ok, it is a bit of a hack and I think it would be awesome if we can get this functionality from reckon.

One solution would be for reckon to add a small utility task that will print the project version. Having this in reckon should avoid some duplication.

Any ideas on how to better handle this?

Regards,

task writeProps {
    doLast {
        Properties myProps = new Properties()
        for (Map.Entry<String, Object> prop : project.properties) {
            if (prop.value != null) {
                String key = String.format("%s", prop.key)
                myProps.put(key, prop.value.toString())
            }
        }
        def buildDir = new File(project.buildDir.toString())
        if (!buildDir.exists()) {
            Files.createDirectory(project.buildDir.toPath())
        }
        myProps.store(new FileWriter("$project.buildDir/build.properties"), "")
    }
}
    stage('Docker build & push') {
      when {
        expression { env.BRANCH_NAME == 'develop' || env.BRANCH_NAME == 'master' }
      }
      steps {
        script {
          def props = readProperties file: 'build/build.properties'
          def versionToDeploy = props['version']
          // docker version tag should not contain +
          safeDockerTag = versionToDeploy.replaceAll('\\+', '_')

          docker.withRegistry('docker-private', 'deployment_credentials') {
            echo "Building image: ${privateImageName}:${safeDockerTag} - Version: ${versionToDeploy}"
            def app = docker.build(privateImageName, '-f distribution/Dockerfile .')

            echo "Publishg image tag: ${privateImageName}:${safeDockerTag} - Version: ${versionToDeploy}"
            app.push("${safeDockerTag}")
          }
        }
      } // steps
    } // stage
ajoberstar commented 6 years ago

I'm not sure this is a good fit for reckon, because if you need to pass info from Gradle to Jenkins, you may need more than just the version.

Gradle has a built in WriteProperties task type that would clean up the Gradle piece that you're currently writing. I've used that as an approach to pass things to Jenkins before.

ieugen commented 6 years ago

Thanks for the suggestion, I think it is going to be enough. If it's a common scenario, maybe a few words in the docs make sens.