GitHub Actions is a combination of primitives for users to quickly ship integrations for their repos. Some of these primitives include the API, webhooks, and authentication.
In this post, I am going to focus on the API and actions/github-script. This action makes it easy to quickly write a script in your workflow that uses the GitHub API and includes the workflow run context.
actions
/
github-script
Write workflows scripting the GitHub API in JavaScript
actions/github-script
This action makes it easy to quickly write a script in your workflow that uses the GitHub API and the workflow run context.
In order to use this action, a script
input is provided. The value of that
input should be the body of an asynchronous function call. The following
arguments will be provided:
-
github
A pre-authenticated octokit/core.js client with REST endpoints and pagination plugins -
context
An object containing the context of the workflow run -
core
A reference to the @actions/core package -
io
A reference to the @actions/io package
Since the script
is just a function body, these values will already be
defined, so you don't have to (see examples below).
See octokit/rest.js for the API client documentation.
Note This action is still a bit of an experiment—the API may change in
future versions.
Development
See development.md.
Reading step results
The return value of the script will…
In order to use this action, a script input is provided. The value of that input should be the body of an asynchronous function call. The following arguments will be provided:
-
github
A pre-authenticated octokit/core.js client with REST endpoints and pagination plugins -
context
An object containing the context of the workflow run -
core
A reference to the @actions/core package -
io
A reference to the @actions/io package
If you are familiar with the octokit.rest.js or Probot library, you will find it pretty similar.
Here is an example script where I am reviewing my PRs based on labels. GitHub Script allows you to write JavaScript to handle different webhook events, and in this case, we are triggered the workflow with labels on the PR.
name: Review with labels
on:
pull_request:
types: [labeled]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@0.8.0
name: Not an Action
if: github.event.label.name == 'nocode' || github.event.label.name == 'not-an-action'
with:
script: |
await github.issues.createComment({
owner: "github-hackathon",
repo: "hackathon",
issue_number: context.payload.number,
body: "Submission is not a usable GitHub Action"
});
await github.pulls.update({
owner: "github-hackathon",
repo: "hackathon",
pull_number: context.payload.number,
state: "closed"
});
- uses: actions/github-script@0.8.0
name: Featured
if: github.event.label.name == 'featured' || github.event.label.name == 'good'
with:
script: |
await github.pulls.merge({
owner: "github-hackathon",
repo: "hackathon",
pull_number: context.payload.number,
});
- uses: actions/github-script@0.8.0
name: Fork
if: github.event.label.name == 'fork'
with:
script: |
await github.issues.createComment({
owner: "github-hackathon",
repo: "hackathon",
issue_number: context.payload.number,
body: "Submission is a fork and does not represent the submitter as the author."
});
await github.pulls.update({
owner: "github-hackathon",
repo: "hackathon",
pull_number: context.payload.number,
state: "closed"
});
- uses: actions/github-script@0.8.0
name: Ended
if: github.event.label.name == 'late'
with:
script: |
await github.issues.createComment({
owner: "github-hackathon",
repo: "hackathon",
issue_number: context.payload.number,
body: "Submission received after the Hackathon has ended."
});
await github.pulls.update({
owner: "github-hackathon",
repo: "hackathon",
pull_number: context.payload.number,
state: "closed"
});
See octokit/rest.js for the API client documentation for more info on what you can do.
This is part of my 28 days of Actions series. To get notified of more GitHub Action tips, follow the GitHub organization right here on Dev.
Discussion (0)