Reputation: 15384
As part of my Jenkins pipeline build I checkout my repo (which copies to my workspace I can see). I then modify a file in my workspace, which I then would like to push back up to my Github repo. I am just updating a version number in a podspec file.
node {
stage 'Update File'
env.WORKSPACE = pwd()
File file = new File("${env.WORKSPACE}/ios.podspec");
fileText = file.text;
regex = "(spec.version\\s.*\$)";
fileText = fileText.replaceAll(regex, "spec.version = '${VERSION}'\n".trim());
file.write(fileText);
}
How can I take that file and push it back up to my Git repo?
Upvotes: 0
Views: 1838
Reputation: 6321
sh "git checkout $branch"
sh "git add <your file>"
sh "git commit -m '...'"
sh "git push $url $branch"
The tricky part is to set the url with the relevant credentials I am using this method -
def getRemoteUrlWithCredentials(credentialsId) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
def scmUrl = scm.getUserRemoteConfigs()[0].getUrl()
scmUrl = scmUrl.substring(scmUrl.indexOf("github.com"))
return "https://${GIT_USERNAME}:${GIT_PASSWORD}@${scmUrl}"
}
}
where credentialId is your git credentialsId. You will need to add scm.getUserRemoteConfigs
to the approve list in Manage Jenkins -> In Process Script Approval.
And last part - I am not sure if it's necessary but maybe you'd need to set the config user.email and user.name ->
def setupConfig(email, userName) {
sh "git config user.email $email"
sh "git config user.name $userName"
}
Upvotes: 2