1

I am trying to get the email id of the user who committed the changes into bitbucket from Jenkins. I have a user-defined variable named EMAIL_ID which has the default value example1@gmail.com. Using web-hook I trigger the Jenkins job. The Jenkins job has the following PowerShell script for finding the email id of the user who committed into bitbucket:

git log -n 1 > git.log
$var = Select-String -Path .\git.log -Pattern "@gmail.com"
$regex = "[a-zA-Z0-9_.-]+@gmail.com"
$email = [regex]::matches($var, $regex).value
$ENV:EMAIL_ID=$email

When I echo the $email, I get example2@gmail.com. But the scope of this variable is within this script only. Even if I assign $ENV:EMAIL_ID=$email, its not working as expected. I need to access this variable from other build steps in the same job. In other words I need to make this variable as a global one, so I can access this from anywhere within the job. Is there any way to achieve this? Thanks in advance!!

AkshayBadri
  • 504
  • 1
  • 10
  • 18

1 Answers1

0

There are two points here: first, you create the PowerShell script that returns the information that you want (in this case, the email address)

I'd assume you are using pipelines - In that case, what you need is to call the bat step in pipeline using the option returnStdout: true (see doc here) and assign the "printed" information to a variable in the pipeline.

For possible following issues, you can have a look into Jenkinsfile windows cmd output variable reference and How do you handle global variables in a declarative pipeline?

Jose Camacho
  • 281
  • 2
  • 8
  • Thanks! This helped me! But can we achieve the same in free style job? – AkshayBadri Sep 22 '20 at 13:28
  • I have not tested it, but you can call Groovy scripts in free style jobs. But if that does not work, you can write the email address into a file. Read that file from the next steps should work (this is the solution I used in freestyle jobs) – Jose Camacho Sep 22 '20 at 14:01