DEV Community

loading...
Play Button Pause Button
GitHub

Generate your own GitHub Action with the actions-toolkit CLI

bdougieyo profile image Brian Douglas ・Updated on ・2 min read

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.

GitHub logo actions / toolkit

The GitHub ToolKit for developing GitHub Actions.

You can find my action at 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 logo JasonEtco / actions-toolkit

πŸ›  A toolkit for building GitHub Actions in Node.js

GitHub Actions Toolkit

An opinionated toolkit for building GitHub Actions in Node.js
Usage β€’ API β€’ How to test your Action β€’ FAQ

GitHub Actions status Codecov

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.

Usage

Installation

$ npm install actions-toolkit
Enter fullscreen mode Exit fullscreen mode
const { Toolkit } = require('actions-toolkit')
const tools = new Toolkit()
Enter fullscreen mode Exit fullscreen mode

Bootstrap a new action

$ npx actions-toolkit my-cool-action

This will create a new folder my-cool-action with the following files:

β”œβ”€β”€ Dockerfile
β”œβ”€β”€ action.yml
β”œβ”€β”€ index.js
β”œβ”€β”€ index.test.js
└── package.json

API

Toolkit options

event (optional)

An optional list…

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
Enter fullscreen mode Exit fullscreen mode

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')

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' })
Enter fullscreen mode Exit fullscreen mode

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.


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 }}
Enter fullscreen mode Exit fullscreen mode

Due to GITHUB_TOKEN limitations: You will need to generate your token (i.e. BDOUGIE_TOKEN). See this article for details on why and how to do that.

gif of it working

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)

pic
Editor guide