Generate your own GitHub Action with the actions-toolkit CLI
Tags: github, devops, githubactions, javascript
Published: 2021-02-15 21:40:21

The GitHub Actions team provides an SDK to build your own GitHub Actions rather quickly. This [GitHub Actions ToolKit](https://github.com/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.

{% github https://github.com/actions/toolkit no-readme %}

_You can find my action at [bdougie/gipht-action](https://github.com/bdougie/gipht-action)_

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.

{% github https://github.com/JasonEtco/actions-toolkit %}

This Toolkit is an opinionated alternative to (and a wrapper around) the official Toolkit. [actions/toolkit](https://github.com/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
```sh
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](https://developers.giphy.com/) to respond to issue comments and return a gif based on the provided keyword. 

```js

const { Toolkit } = require('actions-toolkit')

const fetchGif = require('./utils/giphy.js')
const giphyAPIKey = process.env.GIPHY_TOKEN

// Run your GitHub Action!
Toolkit.run(async tools => {
  // return if you ain't suppose be here
  if (tools.context.payload.comment.body.includes('.gipht')) {

    const searchTerm = tools.context.payload.comment.body.split(".gipht").join(" ")

    const url = `http://api.giphy.com/v1/gifs/translate?api_key=${giphyAPIKey}&s=${searchTerm}`
    const gifURL = await fetchGif(url)

    console.log(tools.context.issue)
    const params = {...tools.context.issue, body: `![](${gifURL})` }

    return tools.github.issues.createComment(params)

  }
}, {event: 'issue_comment.created' })
```

_note: the **GIPHY_TOKEN** will need to be provided in the production workflow._

The [Dockerfile](https://github.com/bdougie/gipht-action/blob/main/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.

{% youtube VjBMAzf4uNU %}

Once you have your newly generated GitHub Action in a GitHub repo, you can add it to your workflow. 

```yml

name: "Comment run"
on:
  issue_comment:
    types: [created]

jobs:
  comment-run:
    runs-on: ubuntu-latest
    steps:
      - uses: bdougie/gipht-action@main
        env:
          GIPHY_TOKEN: ${{ secrets.GIPHY_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.BDOUGIE_TOKEN }}
```
_Due to **GITHUB_TOKEN** limitations: You will need to generate your token (i.e. **BDOUGIE_TOKEN**). See [this article](https://dev.to/github/the-secrets-of-authenticated-github-action-workflow-3i5o) for details on why and how to do that._

![gif of it working](https://user-images.githubusercontent.com/20134767/41519312-3747392e-727c-11e8-8416-1f779f6b4e56.gif)

_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._

{% link https://dev.to/github/automate-your-pr-reviews-with-github-action-scripting-in-javascript-3en2 %}