0

Jenkins ver. 2.73.3

I have a sample build task that is triggered by a commit to a Github repository. This is how the build information looks:

enter image description here

We need to write this username to a separate file and store it in a particular location. How can I achieve it?

**********Edit-1********** Added a build step that executes a shell command to write the variable GIT_COMMITTER_NAME to a file. This fails(empty file) but if I write, say JENKINS_URL, it is written to the file:

enter image description here

Kaliyug Antagonist
  • 3,512
  • 9
  • 51
  • 103
  • Some more info required - what OS / file system are you using? What type of job is this - is this a pipeline job? have you looked at groovy scripting, or plugins to add a build step to allow bash/batch (OS dependent) scripting? What have you tried? – Jay Dec 06 '17 at 14:44
  • 1. Jenkins is running on RHEL7 2. This is a 'free-style'(NOT pipeline) job The information about the (Github)user that triggered the build is already reflected in the build info., I was wondering if there is a way to just extract it. Is a plugin required for this? – Kaliyug Antagonist Dec 06 '17 at 14:48
  • if you add an Execute Shell build step can you access the environment variable GIT_AUTHOR_NAME ? i.e. `echo $GIT_AUTHOR_NAME` - Does this give you the value you are after? – Jay Dec 06 '17 at 15:07
  • Can you check the 'Edit-1' to the original question? I guess these variables are not set by the plug-in, thus, the resultant file is blank. Or am I missing something? – Kaliyug Antagonist Dec 06 '17 at 15:40
  • Sorry, I haven't got a rig to test this on anymore - I was doing stuff like this last year, and have since moved on to pipelines. I also used to use [envinject](https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin#EnvInjectPlugin-Someusecases) to do stuff with SCM variables after checkout, which may work for you. Good luck, I hope someone else can help you! – Jay Dec 06 '17 at 16:16

2 Answers2

0

I guess the github plugin doesn't set, by default, the variables like GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL etc. Taking a cue from this answer, I proceeded with using the placeholders of the 'pretty option' of git show command. I added the following command in the 'Execute Shell' build step of Jenkins job:

git show -s --pretty='GIT_AUTHOR_NAME='%aN%n'GIT_AUTHOR_EMAIL='%aE%n'GIT_COMMITTER_NAME='%cN%n'GIT_COMMITTER_EMAIL='%cE >> github.properties

The output:

GIT_AUTHOR_NAME=LastName FirstName
GIT_AUTHOR_EMAIL=FirstName.LastName@company.com
GIT_COMMITTER_NAME=GitHub Enterprise
GIT_COMMITTER_EMAIL=noreply@github.company.com
Kaliyug Antagonist
  • 3,512
  • 9
  • 51
  • 103
0

Instead of echo $variable name execute env in shell, it will give you all environment variables at the time of execution and then you can pick the correct variable. (From Gitlab to Jenkins its $gitlabUserName)

Dhananjay
  • 354
  • 1
  • 2
  • 15
  • You mean something like env echo "GIT_AUTHOR_NAME=\"$GIT_AUTHOR_NAME\"" > github.properties Getting empty name – Kaliyug Antagonist Dec 06 '17 at 18:31
  • 1
    no, in your execute shell box just keep `env` and push the code in Git, when Job will get triggered, go to the console and see what all variables are available. – Dhananjay Dec 06 '17 at 18:35
  • That was a good suggestion, I see several env. variables in the console output but the ones needed e.g: GIT_AUTHOR_NAME, GIT_COMMITTER_EMAIL etc. are missing(I guess the plugin is not setting them, at all) – Kaliyug Antagonist Dec 07 '17 at 07:27