Github script
Last updated
Last updated
You might also want to interact with the Github API during your workflows. Some of the common use cases we've noticed include:
Fetching information about the repository/pull request/user
Creating issues
Creating issue/pull request comments
Updating issues/pull requests
Retrieving information about another repository
Automatically running jobs and creating commit
You can use the Github API via Github script, an action that allows you to write Javascript scripts using the Github API — actions/github-script@v7
.
The README.md
of the action contains a lot of examples of use cases with the Github script. However, we will just cover a very simple script to illustrate a few points:
In the above example, we are using Github script to create a new comment on a newly created issue. We can see that the event that triggers this workflow is the issues
event, when one is opened
.
The various API calls are based on the Octokit documentation: https://octokit.github.io/rest.js/v21/ where you replace octokit
with github
!
You might be wondering, "How does the Github script have access to the Github API when some APIs require an API token?"
Add it to the repository's secrets: https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions
Set the github-token
input for the action: see below
Another thing you can use Github script for is combining it with Pollers to automatically perform some actions to the current repository at set intervals. An example of this might be to poll for new information across multiple repositories and updating a set of files on the current repository as commits:
This poller runs every day at midnight UTC, fetching all repositories that satisfy some query string and updating the README of the current repository as a commit.
Amazing question! This is where the GITHUB_TOKEN
we talked about in come into play. By default, Github script uses the GITHUB_TOKEN
to access these APIs, which means that it is restricted to only accessing the current repository. Therefore, if you wish to access other repositories or data that does not belong to the current repository and requires authentication, you will need to: