0

Usually, when using SCM like the Git Plugin, there are a bunch of environment variables that you can use (e.g. see these)

But neither the Git Step nor the Generic SCM seem to do that.

Is there a way to get these variables into the groovy env.* so that they can be used?

Something like this would be useful:

def commitMessage = sh 'git log --max-count=1 --oneline --no-merges | cut -b9-'

I can think of writing the results to a file and read them via the readFile() mehtod -- but is there an easier way to achieve this?

Community
  • 1
  • 1
mana
  • 6,347
  • 6
  • 50
  • 70

2 Answers2

3

For the Record: I have the following code to get the branch-name:

stage 'preparation'
node {
 // checkout branch
  git branch: 'origin/master', url: 'git@example.net:project.git'

  // write current branch-name to file
  sh 'git branch -a --contains `git rev-parse HEAD` | grep origin | sed \'s!\\s*remotes/origin/\\(.*\\)!\\1!\' > git-branch.txt'

  // read data from file into environment-variable
  env.gitBranch = readFile('git-branch.txt').trim()

  // let people know what's up
  echo "testing branch ${env.gitBranch}"
}

The remainder of the flow-script is comprised of serveral parametrized jobs which get the env.gitBranch passed as parameter (among others, if needed).

Be sure to allow concurrent builds for the workflow to catch every updated branch.

kronn
  • 925
  • 11
  • 31
2

See JENKINS-24141; these variables are not yet available from Workflow.

In the meantime, you are on the right track: run a git command to record any information you need, and use readFile to load it (see also JENKINS-26133).

Jesse Glick
  • 24,539
  • 10
  • 90
  • 112