4

I'm using the OpenShift plugin with Jenkins Pipelines to run builds in OpenShift when Github gets a new commit.

I'd also like to be able to report the status of the build back to github.

However in order to do this, I need to know what the commit was that just got built. I'm using the following pipeline config

node() {

   stage 'build'
   def builder = openshiftBuild(buildConfig: 'my-web', showBuildLogs: 'true')

   stage 'deploy'
   openshiftDeploy(deploymentConfig: 'my-web')
   openshiftScale(deploymentConfig: 'my-web',replicaCount: '3')    

}

However I have zero idea how to get the commit SHA from the openshiftBuild step since this does the git pull.

John Mitchell
  • 9,653
  • 9
  • 57
  • 91
  • Does this help? https://stackoverflow.com/questions/35554983/git-variables-in-jenkins-workflow-plugin – Tarun Lalwani May 10 '18 at 07:55
  • Afraid not :( since the git pull happens within the openshift build the variable doesn't appear to be accessible at this point. – John Mitchell May 10 '18 at 10:20
  • 1
    Can you try `builder.getCommitID()` from the plugin code its seems that should have the commit id? – Tarun Lalwani May 10 '18 at 11:08
  • If the above does not work do you have access to the oc binary? – PhilipGough May 10 '18 at 11:19
  • it doesn't appear to work since as soon as the builder is complete the object is nullified. So I get a null reference. I do have access to OC but since the pipeline is within Openshift itself I'm not sure we can call the OC command from inside. – John Mitchell May 11 '18 at 20:56
  • @JohnMitchell, In groovy an object cannot be just nullified if you have a reference to it, unless it's a week reference (not your case). – Vitalii Vitrenko May 12 '18 at 09:51
  • @VitaliiVitrenko when I echo out the `builder` I just get `null`. It could be that the `openshiftBuild` step just never sets anything or I'm inspecting it incorrectly. – John Mitchell May 14 '18 at 17:38

2 Answers2

2

According to https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables, you get it from the GIT_COMMIT environment variable.

If the checkout happens later, you can get it with the following code:

def gitCommitId = sh(returnStdout: true, script: 'git rev-parse HEAD')
Hendrik M Halkow
  • 2,208
  • 15
  • 24
  • Since the open-shift builder seems to be self contained, git rev-parse doesn't' appear to be in existence.Also tried echoing out all env's and the GIT_COMMIT isn't there (either before or after the builder runs). – John Mitchell May 14 '18 at 17:36
0

It's hard to tell without seeing the rest of your pipeline, but it looks like you are just triggering an OpenShift S2I build, which is not what is recommended for Pipeline builds. You should have your pipeline build the artifact(s) for the application, then use an S2I binary build to have OpenShift put the artifacts into a runtime container. For an example, see HERE.

Deven Phillips
  • 1,129
  • 14
  • 39