4

When running my Jenkins build i need to update the contents of a file with a version number in this case. I have come across a plugin called text-file-operations but rather than write a whole new file I thought it would be better to update.

In this example I have a podspec file located in the root of the project which just needs a version number updated with a variable I have created earlier in the process.

spec.version               = '13.4.0'

I just need to convert that to

spec.version               = "${VERSION_NUMBER}"

Is there a way to do this ?

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Richlewis
  • 15,070
  • 37
  • 122
  • 283
  • So the file exists already with the content you specified last? Or is that what you want to write to the file, with the value from the environment variable? – MaTePe Dec 06 '16 at 09:50
  • the file already exists and i want to write to that file with the value of the environment variable – Richlewis Dec 06 '16 at 09:51

1 Answers1

4

Is this what you want then?

Groovy + how to append text in file ( new line )

f = new File('<filename>')
f.append("spec.version               = ${VERSION_NUMBER}\n")
Community
  • 1
  • 1
MaTePe
  • 936
  • 1
  • 6
  • 11
  • 1
    it's almost what i require, seem to be having issues finding the file and permissions to access it.. would this replace the text or just append to the end of the file ? – Richlewis Dec 06 '16 at 10:50
  • It would append it. That is another issue, permissions and access, yes, the instance that runs the script (and with that user) has to be able to access the file. – MaTePe Dec 06 '16 at 11:47
  • ok thanks, can i just replace `spec.version = 'original value'` with `spec.version = ${VERSION_NUMBER}\n` – Richlewis Dec 06 '16 at 11:53
  • Yes, unless you are doing something strange in your job. That should be enough. Please test it... – MaTePe Dec 06 '16 at 12:33