What is GitHub Script?

GitHub Script is a workflow action that provides you with access to the GitHub API from within your GitHub Actions. It offers convenient support for any API usage available in octokit/rest.js .

Suppose you maintain a vibrant GitHub repository. Your project has a substantial number of consumers and contributors, and you want to ensure they have a welcoming experience. You also find that the responsibilities of managing the team’s planning and workload can really add up. You need a solution that will allow you to hand off some of the mundane tasks to automation so that you can focus on areas where you really add value. You know that GitHub offers an API that will enable you to automatically reply to new issues and begin the triage workflow for new bug reports. You just haven’t invested the time in figuring it all out. Until now.

In this module, you learn how to interact with the GitHub API from a GitHub Actions workflow by using GitHub Script which deploy on Best web hosting server .

 

Here, we discuss how GitHub Script enables you to automate common GitHub processes using GitHub Actions workflows.

What is GitHub Script?

GitHub Script an action that provides an authenticated octokit client and allows JavaScript to be written directly in a workflow file. It runs in Node.js , so you have the power of that platform available when writing scripts.

best web hosting for small business

What is octokit?

octokit is the official collection of clients for the GitHub API. One of these clients, rest.js, provides JavaScript access to GitHub’s REST interface.

You’ve always been able to automate the GitHub API via octokit/rest.js, although it could be a chore to properly set up and maintain. One of the great advantages to using GitHub Script is that it handles all of this overhead so you can immediately start using the API. You don’t need to worry about dependencies, configuration, or even authentication.

What can octokit/rest.js do?

The short answer is that it can do virtually anything with respect to automating GitHub. Not only do you have access to commits, pull requests, and issues, but you also have access to users, projects, and organizations. You can retrieve lists of commonly used files like popular licenses or .gitignore files. You can even render Markdown.

If you’re building something that integrates GitHub, the odds are good that you will find what you’re looking for in the full octokit/rest.js documentation .

How is using GitHub Script different from octokit/rest.js?

The main difference in usage is that GitHub Script provides you with a pre-authenticated octokit/rest.js client named github.

So instead of:

octokit.issues.createComment({

You would use:

github.issues.createComment({

In addition to the github variable, the following variables are also provided:

 

Building a workflow that uses GitHub Script

GitHub Script actions fit into a workflow like any other action. As a result, you can even mix them in with existing workflows, such as those you may already have set up for CI/CD. To illustrate its convenience, we now construct a complete workflow that uses GitHub Script to automatically post a comment to all newly created issues.

We start off with a name and an on clause that specifies that this workflow runs when issues are opened.

Using actions/[email protected]

The actions/[email protected] action, also known as GitHub Script, does all the heavy lifting for our integration with the GitHub API.

This action requires a github-token that will be provided at runtime so that requests are authenticated. This is automatically done for you, so you can use that code as-is.

The script parameter can be virtually any JavaScript that will use the octokit/rest/js client stored in github. In this case, it’s just one line (split across multiple lines for readability) that creates a hardcoded comment.

After the workflow is run, GitHub Script will log the code it executed for review on the Actions tab.completed workflow

Running from a separate file

Sometimes you may need to use a lot of code to fulfill your GitHub Script scenario. When that happens, you can keep the script in a separate file and reference it from the workflow instead of putting all the script inline.

Here is an example of a simple workflow that does this:

on: push

jobs:
echo-input:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/github-script@v2
with:
script: |
const path = require('path')
const scriptPath = path.resolve('./path/to/script.js')
console.log(require(scriptPath)({context}))

Read More about web hosting services best reddit website hosting 2020