GitHub Actions Day 21: GitHub Script

December 21, 2019

This is day 21 of my GitHub Actions Advent Calendar. If you want to see the whole list of tips as they're published, see the index.

So far this month, we've looked at a lot of different workflows. In some of them, we've manipulated some data that we got from GitHub. But they've all been read-only. What if we want to write data back to GitHub?

Well, we could write an action that we could use in our workflow (more on that later). But actions are meant to be shared, so they're ideal for performing tasks that you need to perform across repositories, and across workflows.

You could also write a script that uses the GitHub API. But an action has already set that up for you -- you can use the github-script action to script your GitHub workflows directly.

github-script gives you the ability to run a snippet of JavaScript, and within that script, you're given all the context about your workflow execution in the context object that's set up before your script run. But you're also given the github object, which is an Octokit client as well. This means that you can both read the information about your workflow, and what triggered it, and you can write back to the repository, authenticated with your GITHUB_TOKEN.

For example, if I wanted to create a comment on any new issue that's opened in my repository,

I can call the issues.createComment API in Octokit. You can specify this script as the script argument to the actions/github-script action.

This workflow will run on any new issue that's opened. When it does, the github-script action will run the script you specify.

GitHub Script

Using github-script is a very easy way to script bidirectional interactions with GitHub, putting the JavaScript right in your workflow.