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.
require A proxy wrapper around the normal Node.js require to enable
requiring relative paths (relative to the current working directory) and
requiring npm packages installed in the current working directory. If for
some reason you need the non-wrapped require, there is an escape hatch
available: __original_require__ is the original value of require without
our wrapping…
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 labelson:pull_request:types:[labeled]jobs:build:runs-on:ubuntu-lateststeps:-uses:actions/github-script@0.8.0name:Not an Actionif: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.0name:Featuredif: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.0name:Forkif: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.0name:Endedif: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.