Skip to content

Instantly share code, notes, and snippets.

@ScottNeaves
Created November 21, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottNeaves/5cdce294296437043b24f0f3f0a8f1d8 to your computer and use it in GitHub Desktop.
Save ScottNeaves/5cdce294296437043b24f0f3f0a8f1d8 to your computer and use it in GitHub Desktop.
Push to GitHub from Jenkins job
node {
// This step utilizes the Workspace Cleanup Plugin: https://wiki.jenkins.io/display/JENKINS/Workspace+Cleanup+Plugin
step([$class: 'WsCleanup'])
}
node {
sshagent (credentials: ['<< insert credentialsId here >>']) {
sh("git clone << your repo here, should be "git@..." like git@github.com:username/reponame.git >>")
}
}
node {
// Make whatever changes you want to the files in the cloned repo here. For example, update a certain file with a the latest for the purpose of backing it up.
// The curl command below is an example, and it's what I am using for backing up ranger policies into GitHub.
// sh "curl -X GET --header 'text/json' -H 'Content-Type: text/json' -u user:pass 'http://172.111.92.43:6080/service/plugins/policies/exportJson' > reponame/ranger-policies/policies.json"
}
node {
sshagent (credentials: ['<< insert credentialsId again here >>']) {
// "git add", "git commit", and "git push" your changes here. You may have to cd into the repo directory like I did here because the current working directory is the parent directory to the directory that contains the actual repo, created by "git clone" earlier in this Jenkinsfile.
sh("(cd reponame && git add ranger-policies/policies.json)")
sh("(cd reponame && git commit -m 'daily backup of ranger-policies/policies.json')")
sh('(cd reponame && git push git@github.com:username/reponame.git)')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment