Methods to work with GitHub.
def github = new GitHub(this) // init github configurations github.init( repository : 'zowe/explorer-jes', branch : 'master', usernamePasswordCredential : 'my-github-credential', email : 'my-github-user@gmail.com' ) // do some modifications // ... // commit changes github.commit('My work is done') // push to remote github.push() // check if the branch is synced if (!github.isSynced()) { echo "Branch is not synced with remote." }
Modifiers | Name | Description |
---|---|---|
static java.lang.String |
DEFAULT_BRANCH |
Default branch name. |
static java.lang.String |
DEFAULT_REMOTE |
Default remote name. |
static java.lang.String |
GITHUB_API_DOMAIN |
GitHub domain name for api calls. |
static java.lang.String |
GITHUB_DOMAIN |
GitHub domain name. |
static java.lang.String |
GITHUB_DOWNLOAD_DOMAIN |
GitHub domain name for downloading user content. |
Type | Name and description |
---|---|
java.lang.String |
branch Github branch to checkout |
java.lang.String |
email github user email |
java.lang.String |
folder Folder where the repository is cloned to. |
java.lang.String |
remote Remote name |
java.lang.String |
repository Github repository in format of owner/repo . |
java.lang.Object |
steps Reference to the groovy pipeline variable. |
java.lang.String |
username github user name |
java.lang.String |
usernamePasswordCredential Jenkins credential ID for github username/password |
Constructor and description |
---|
GitHub
(java.lang.Object steps) Constructs the class. |
Type Params | Return Type | Name and description |
---|---|---|
|
void |
checkout(java.util.Map args = [:]) Checkout a new branch |
|
void |
checkout(java.lang.String branch, java.lang.Boolean isNew = false) Checkout a new branch |
|
void |
cloneRepository(java.util.Map args = [:]) Clone a repository. |
|
java.lang.Boolean |
closePullRequest(java.util.Map args = [:]) Close pull request |
|
java.lang.Boolean |
closePullRequest(java.lang.Integer pr) Return infomation of a pull request |
|
java.lang.String |
command(java.lang.String command) Issue git command and get stdout return. |
|
void |
commit(java.util.Map args = [:]) Commit all the changes. |
|
void |
commit(java.lang.String message) Commit all the changes. |
|
void |
config() Setup git config of username/email based on properties. |
|
java.lang.Integer |
createPullRequest(java.util.Map args = [:]) Create pull request based on current branch |
|
java.lang.Integer |
createPullRequest(java.lang.String base, java.lang.String title, java.lang.String body = '') Create pull request based on current branch |
|
void |
deleteRemoteBranch(java.util.Map args = [:]) Delete remote branch. |
|
java.util.Map |
getLastCommit(java.util.List fields = ['hash']) Get last commit information. |
|
java.lang.String |
getLastCommitHash(java.lang.Boolean returnLong = true) Get last commit hash. |
|
java.util.Map |
getPullRequest(java.util.Map args = [:]) Return infomation of a pull request |
|
java.util.Map |
getPullRequest(java.lang.Integer pr) Return infomation of a pull request |
|
void |
init(java.util.Map args = [:]) Initialize github properties |
|
void |
initFromFolder(java.lang.String folder = '') We can guess repository/branch information from an existing git folder. |
|
java.lang.Boolean |
isClean(java.util.Map args = [:]) Check if current working tree is clean or not. |
|
java.lang.Boolean |
isSynced(java.util.Map args = [:]) Check if current branch is synced with remote. |
|
void |
push(java.util.Map args = [:]) Push all local commits to remote. |
|
void |
reset(java.util.Map args = [:]) Reset current branch to remote. |
|
void |
tag(java.util.Map args = [:]) Tag the branch and push to remote. |
|
void |
tag(java.lang.String tag) Tag the branch and push to remote. |
|
java.lang.Boolean |
tagExistsLocal(java.lang.String tag) Validate if a tag exists in local. |
|
java.lang.Boolean |
tagExistsRemote(java.lang.String tag) Validate if a tag exists in remote. |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Default branch name.
"master"
Default remote name.
"origin"
GitHub domain name for api calls.
"api.github.com"
GitHub domain name.
"github.com"
GitHub domain name for downloading user content.
"raw.githubusercontent.com"
Github branch to checkout
github user email
Folder where the repository is cloned to.
Remote name
Github repository in format of owner/repo
. The owner can be organization or GitHub user.
Reference to the groovy pipeline variable.
github user name
Jenkins credential ID for github username/password
Constructs the class.
When invoking from a Jenkins pipeline script, the Pipeline must be passed the current environment of the Jenkinsfile to have access to the steps.
def github = new GitHub(this)
steps
- The workflow steps object provided by the Jenkins pipelineCheckout a new branch
github.checkout('feature/test')
branch
- branch nameCheckout a new branch
Clone a repository.
clone
, which is conflicted with java.lang.Object#clone()
.github.cloneRepository( branch : 'my-branch', folder : '.tmp-my-branch' )
branch
- branch to checkoutfolder
- which folder to save the cloned filesClose pull request
pr
- pull request idReturn infomation of a pull request
Issue git command and get stdout return.
String result = github.command('git rev-parse HEAD') // result should be a string with latest commit hash
command
- git commandCommit all the changes.
github.commit('my changes to handle some errors')
message
- git commit messageCommit all the changes.
Setup git config of username/email based on properties.
Create pull request based on current branch
title
- pull request title to be createdbase
- base branchbody
- pull request body messagedraft
- boolean, if this is a draft pull requestCreate pull request based on current branch
Delete remote branch.
Get last commit information.
def info = github.getLastCommit(['hash', 'subject']) echo "Current commit hash : ${info['hash']}" echo "Current commit subject : ${info['subject']}"
fields
- List of what information of the commit should be returned. Available fields are:Get last commit hash.
def commitHash = github.getLastCommitHash()
returnLong
- return long or short hashReturn infomation of a pull request
pr
- pull request idReturn infomation of a pull request
Initialize github properties
args
Map.repository
- repository namebranch
- branch name to work with (like clone, push, etc). Optional,
default value is DEFAULT_BRANCH.folder
- target folderusernamePasswordCredential
- github username/password credentialusername
- github user.name
config. Optional, can be extracted from credentialemail
- github user.email
configWe can guess repository/branch information from an existing git folder.
#repository
and/or #branch
fields.folder
- which folder to testCheck if current working tree is clean or not.
if (!github.isClean()) { echo "There are changes not committed." }
Check if current branch is synced with remote.
if (!github.isSynced()) { echo "Branch is not synced with remote." }
Push all local commits to remote.
Reset current branch to remote. This is get rid of all local modifications.
Tag the branch and push to remote.
tag
- tag name to be createdTag the branch and push to remote.
tag
- tag name to be createdValidate if a tag exists in local.
if (github.tagExistsLocal('v1.2.3')) { echo "Tag v1.2.3 already exists in local." }
tag
- tag name to checkValidate if a tag exists in remote.
if (github.tagExistsRemote('v1.2.3')) { echo "Tag v1.2.3 already exists in remote." }
tag
- tag name to checkGroovy Documentation