0

I have a jenkins organization pipeline job that executes on all repositories that have "Jenkinsfile" defined. The job clones the repository from github, then runs the powershell script that increments the version number in the file. I'm now trying to publish that updated file back to the original repository on github, so when developer pulls the changes he gets the latest version number.

I tried using the script (inside "jenkinsfile") as suggested in jenkins JIRA (https://issues.jenkins-ci.org/browse/JENKINS-28335), but to no avail. Any suggestions will be appreciated. Basically need to execute "git commit" and "git push" using the same parameters defined for a job.

Just as a reference, here is a previous solution used for free style (not pipeline job): How to push changes to github after jenkins build completes?.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Woland
  • 2,881
  • 2
  • 20
  • 33

1 Answers1

0

Actually found couple solutions, first I modied script from Jenkins like this (some objects changed in workflow pipeline):

import hudson.FilePath
import org.eclipse.jgit.transport.URIish

node {
    env.WORKSPACE = pwd()
    stage 'Checkout'
        checkout scm

        def build = manager.build
        def listener = manager.listener
        def workspace = new FilePath(new File(env.WORKSPACE))
        def environment = build.getEnvironment(listener)
        final def project = build.getParent()
        final def gitScm = project.getTypicalSCM()
        final def gitClient = gitScm.createClient(listener, environment, build, workspace);

        final def gitTagName = "TAG_NAME"
        final def comment = "COMMENT"
        final def remoteURI = new URIish("origin")

        gitClient.tag(gitTagName, comment)
        gitClient.push().tags(true).to(remoteURI).execute()
}

You need to run the script multiple times and then allow code execution in jenkins (manage jenkins->in process script approval).

Another solution, much simpler (using this one for now):

bat "\"${tool 'Git'}\" config user.email \"ci@virtocommerce.com\""
bat "\"${tool 'Git'}\" config user.name \"Virto Jenkins\""
bat "\"${tool 'Git'}\" commit -am \"Updated version number\""
bat "\"${tool 'Git'}\" push origin HEAD:master -f"

You have to have Git tool with a name "Git" configured in Jenkins.

Woland
  • 2,881
  • 2
  • 20
  • 33