1

Is it possible to do git commit using jenkins dsl.I tried:

shell() - doesn't recognize git command

@library()- i have working library that does take care of git commit. It is working in jenkins pipeline job but not in dsl(throws error at first line where @libary() or library "libname" is used)

sshAgent(credentials){} - didn't work in dsl

I tried to use git plugins but there is no commit for existing plugins.

jack
  • 49
  • 9
  • 1
    Are you aware that Jenkins JOB DSL and Jenkins Pipeline are two completely different things? Although you can run a JOB DSL script in Pipeline from Pipeline point of view it'll just be a string (or file). Quite the same the other way round: You can create Pipeline jobs using JOB DSL. But again JOB DSL won't know anything about the Pipeline job (script). Please provide more details on what you want to do. Do you want to create a freestyle job using JOB DSL or run the git commit from within a Pipeline job? – Joerg S Aug 14 '18 at 13:45
  • Sorry for confusion. I am trying to create a freestyle job using JOB DSL and run git commit.No pipeline is involved. I mentioned it here to say i tried to use pipeline library where i had library that does git commit with my inputs.If someone knows how to use Global libraries in DSL that also helps me to achieve this. – jack Aug 14 '18 at 13:50
  • related: https://stackoverflow.com/questions/19922435/how-to-push-changes-to-github-after-jenkins-build-completes – Florian Castellane Aug 14 '18 at 14:13
  • Think for job DSL you should be able to write your very own libraries just like for any other groovy project. However reusing a global shared library as prepared for Pipeline will not help as that code will be desinged to be executed during runtime while the JOB DSL code is executed upfront to create a job. – Joerg S Aug 14 '18 at 14:53
  • @FlorianCastellane, Thank you for pointing. I am able to do commit from general jenkins job, Jenkins Pipeline Job. Issue is with DSL jobs. – jack Aug 15 '18 at 00:58
  • @JoergS, When i search for job DSL libraries i am getting only shared libraries links. What you mean by own libraries just like any other groovy project? Could you share if there is any example for "Jenkins DSL using libraries" – jack Aug 15 '18 at 01:13
  • you should be able to use the same things in general "freestyle" jobs and in the DSL. Can you show us in your question how you are doing it in general jobs? – Florian Castellane Aug 15 '18 at 09:16
  • Here https://stackoverflow.com/a/43354276/4279361 I found a link where the author of the answer tells that there will be an example on how to use code from a repository in Job DSL. Please let us know whether it is working. – Joerg S Aug 15 '18 at 14:19

1 Answers1

0

The shell command will work if your build agent contains a Git installation. Either pre-install Git on your agents or use the Global Tool Configuration to setup a Git installation and then use the Tool Environment Plugin to get a pointer to that installation.

If you setup a Git installation called GIT2 in Global Tool Configuration, the following should work depending on your OS and the installation method.

job('example') {
  wrappers {
    toolenv('GIT2')
  }
  steps {
    shell('$GIT2_HOME/bin/git commit -am "test"')
  }
}
daspilker
  • 8,154
  • 1
  • 35
  • 49