Create a GitHub Action with Docker

18 May 2019 in Tech

This post is outdated and no longer works with the current version of GitHub Actions. It is presented here for historical reasons

If you're looking for a quick way to create a nodejs action, run npx actions-toolkit <your-action-name> to scaffold an empty action

GitHub actions are standard Docker images with a small amount of additional metadata. As it's a Docker image it must start with a FROM entry. In this case we're going to inherit from the node:slim image as we build actions with node.

FROM node:slim

The LABEL instruction allows you to add context when your action is shown in GitHub's visual workflow editor.

LABEL "com.github.actions.name"="Your Action Name" LABEL "com.github.actions.description"="This is a list" LABEL "com.github.actions.icon"="activity" LABEL "com.github.actions.color"="red"

There are 282 different values available for com.github.actions.icon (as of publishing). Take a look at https://feathericons.com/ for the full list.

com.github.actions.color sets the background colour of your icon, and can be one of the following:

  • white
  • yellow
  • blue
  • green
  • orange
  • red
  • purple
  • gray-dark

For more information about the LABEL command see the github docs.

Once you've added this metadata you can use standard Docker commands to build an image. In this case we're using the node:slim image as a base, installing all our dependencies with npm and setting an entrypoint of /index.js

COPY package*.json ./ RUN npm ci COPY . . ENTRYPOINT ["node", "/index.js"]

At this point you can run implement index.js using any libraries you need before uploading your action to a GitHub repo.

For an example action built following this blog post take a look at debug, which outputs all available GITHUB_ environment variables and the event payload used to trigger the action.