The GitHub Actions team provides an SDK to build your own GitHub Actions rather quickly. This GitHub Actions ToolKit is a combination of packages to make creating actions easier. Today I will show you how to make a GitHub Action with a wrapper for that Toolkit.
If you are like me, you build a lot of similar projects repeatedly, which is why I am going to focus on the CLI version of the Toolkit for building GitHub Actions in Node.js.
This toolkit is an opinionated alternative to (and wrapper around) the official toolkit. actions/toolkit makes many features optional in the interest of performance, so you may prefer to use it instead of this library.
This Toolkit is an opinionated alternative to (and a wrapper around) the official Toolkit. actions/toolkit makes many features optional in the interest of performance, so you may prefer to use it instead of this library.
To get started, you only need the npx command
npx actions-toolkit my-cool-action
This will generate a Dockerfile, index.js, index.test.js, and action.yml.
The JavaScript contains a wrapper for the Toolkit ready for you to start your action. I chose to leverage the Giphy API to respond to issue comments and return a gif based on the provided keyword.
const{Toolkit}=require('actions-toolkit')constfetchGif=require('./utils/giphy.js')constgiphyAPIKey=process.env.GIPHY_TOKEN// Run your GitHub Action!Toolkit.run(asynctools=>{// return if you ain't suppose be hereif (tools.context.payload.comment.body.includes('.gipht')){constsearchTerm=tools.context.payload.comment.body.split(".gipht").join("")consturl=`http://api.giphy.com/v1/gifs/translate?api_key=${giphyAPIKey}&s=${searchTerm}`constgifURL=awaitfetchGif(url)console.log(tools.context.issue)constparams={...tools.context.issue,body:`![](${gifURL})`}returntools.github.issues.createComment(params)}},{event:'issue_comment.created'})
note: the GIPHY_TOKEN will need to be provided in the production workflow.
The Dockerfile is generated today, but keep in mind; you can build and GitHub Actions without one. I will cover that in a future article.
The action.yml file, if needed to publish your action on the GitHub Marketplace. I cover that in detail in the following video.
Once you have your newly generated GitHub Action in a GitHub repo, you can add it to your workflow.