ServiceNow – GitHub
Integration
Centralize and streamline your code management with GitHub.
• One-Click Code Push to GitHub
Easily push your ServiceNow application code to a GitHub repository with a
single click, ensuring fast and reliable code transfers.
• Create or Commit to Repositories Seamlessly
Effortlessly create new repositories or commit changes to existing ones, directly
from within the ServiceNow environment.
• Simplified Version Control
Manage your code versions efficiently through GitHub, ensuring consistency and
traceability across your development lifecycle.
• Streamlined Team Collaboration
Collaborate effectively across teams by leveraging GitHub features like pull
requests, code reviews, and commit histories—enhancing transparency,
accountability, and coordination.
• Upload the code in a new repository:
Create a new repository by specifying its name, provide the file name to store
your code, and commit—a repository with the specified file name will be created
within GitHub containing all your code.
• Upload the code in an existing repository:
Already have an existing repository in GitHub and want to commit your commit in
it, simply provide the name of your existing repository and the name of your new
file and hit commit. A new file will be crated in your existing repository with your
code in it.
• Update the code in an existing repository:
To update existing code in your repository, simply provide the repository name
and the filename you wish to update. On committing, the code in the specified
file will be replaced with the new code.
Once the code is successfully pushed from ServiceNow to the GitHub repository
and a confirmation is received from GitHub, a success message is displayed within
ServiceNow.
A new repository is created in GitHub using the name provided by the user by
invoking the POST /user/repos GitHub API endpoint. Once the repository is
successfully created, a file is added to it using the specified filename. This is
accomplished by calling the PUT /repos/{owner}/{repo}/contents/{path}
endpoint, where the code pushed from ServiceNow is committed as the content of
the file. The request includes a commit message, author information, and the
Base64-encoded code payload. This process ensures the code is securely version-
controlled in the newly created GitHub repository from the initial commit.
Implementation Details
A UI Page that captures all required details from the user.
The GitHub UI Action triggers the UI Page using the GlideModal class for a modal
display.
When the GitHub UI Action is clicked, the client script on the UI Page invokes the
Script Include and passes all the necessary parameters.
The function within the Script Include receives all parameters passed from the
client script, including the stringified version of the code. The code is then parsed
into a JSON object and encoded as a Base64 string.
Based on the operation selected by the user—such as creating a new file or
updating an existing one—the corresponding REST Message is invoked to commit
the code changes to the GitHub repository.
Action = Create:
Action = Upload:
Action = Update:
When updating an existing file in a GitHub repository, GitHub requires the SHA hash of
the file’s current version to ensure accurate and safe replacement. To obtain this, a GET
request is first made to the GitHub API using the endpoint
/repos/{owner}/{repo}/contents/{path}. The response contains the file's
metadata, including the required SHA value. This SHA must then be included in the
subsequent PUT request to the same endpoint when committing the updated content.
Once the confirmation is received from GitHub of successful code commit, the client
script of the UI Page invokes the confirmation UI Page.