0

I am trying to set up builds on Jenkins. When the build is successful, I push a tag of the build using the Post build Action Git Publisher.

Until Now, these have been done manually, with the tag being given the version number, A.B.C.D (which comes from a text file). Now we are using Jenkins, D comes from the $BUILD_NUMBER Jenkins variable, but A.B.C are stored in a text file within the workspace. Is there a way I can pass A.B.C to the git publisher? or will have to do all of the git commands in a script?

Using Jenkins v1.624. Updating not currently possible (before people suggest this)

Tricky
  • 3,791
  • 3
  • 10
  • 23
  • which kind of jenkins jobs you are using (freestyle or pipeline) ? – BigGinDaHouse May 23 '18 at 09:04
  • Its Jenkins v1.624, so freestyle I think. – Tricky May 23 '18 at 09:23
  • In freestyle jobs Im not sure you will be able to achieve your task easily, if it would be a Jenkins 2.0+ then inside pipeline via groovy you could easily read the contents of file and pass it to the publisher. For freestyle, may be the easiest way is to pass A.B.C instead of txt file as build param – BigGinDaHouse May 23 '18 at 09:25
  • Unfortunately the text file is needed for offline development jobs not done on jenkins, and will get updated for each release. So if it was passed in as a Jenkins parameter we would have to constantly update the Jenkins job with the new version number (not really preferable). We do plan to move to pipeline jobs, so may just have to wait for that. – Tricky May 23 '18 at 09:31
  • if you do plan to move to pipelines, better wait a bit and have nice and shine implementation of your needs in groovy ;) – BigGinDaHouse May 23 '18 at 09:32
  • In that version of Jenkins, can you use 'prepare an environment for the run'? and how do the values A.B.C get written to the text file - are the parameters passed into the job? – elworthy May 23 '18 at 10:30
  • A.B.C come from a text file in the Git repo. They are changed by the devs when we are working towards a release. A= variant, B/C = major/minor. Currently there are 6 variants. They are needed in a text file because devs can compile the code outside of jenkins (for development and debug). – Tricky May 23 '18 at 13:38
  • Best to include the @username when you respond to a comment so the person is notified (only the question owner gets notified automatically). Does your freestyle job have the option to 'prepare an environment for the run' from the https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin ? – elworthy May 24 '18 at 08:07

1 Answers1

0

Not entirely sure if this will work the Post build Action Git Publisher (as I don't use it), and I can only test this on:

Jenkins ver. 2.32.3
EnvInject+Plugin 1.93.1
Groovy+plugin 1.30

Based on suggestion by 'Joerg S' in this post:

Creating a Jenkins environment variable using Groovy

Add a 'Execute Groovy Script' build step to read in the workspace file (tmpFile) containing A.B.C and convert it to a java based properties file - name:value) :

def custom_tag = new File('tmpfile').text.trim()
File propFile = new File('properties.text')
propFile.write "CUSTOM_TAG:"+custom_tag

Then add a 'Inject Environment Variables' build step to read in the new file, so Properties File Path is properties.text

You should then be able to use the ${CUSTOM_TAG} in your post build git publish as the TAG, as now it's an environment variable.

If this doesn't work check out the groovy code in the link above, it might offer something else.

elworthy
  • 454
  • 4
  • 12