Skip to main content

How to Create Custom GitHub Actions

We can create our own actions that utilize JavaScript or Docker containers.

JavaScript

mkdir .github/actions/hello

touch .github/actions/hello/action.yml

An example of a Javascript custom action:

action.yml
name: Greet
author: Kobbi Gal
description: This is a custom action
inputs:
who-to-greet:
description: 'Who to greet'
required: true
default: Kobbi
outputs:
time:
description: "Time of greeting"
runs:
using: 'node12'
main: 'dist/index.js'

Some very useful actions for JavaScript can be found in toolkit. For example, in order to get inputs and set outputs, we would need to install the core and github packages:

npm install @actions/core
npm install @actions/github

And then use them in index.js

Dependencies

Since the job will run in a VM, NPM packages will not be available. We can push the node_modules folder but a better solution is to use ncc and compile all dependencies into one bundle.

npm install -D @zeit/ncc

npx ncc build .github/actions/greet/index.js -o .github/actions/greet/dist

And change the action to use the bundle:

runs:
using: 'node12'
main: 'dist/index.js' # reference the bundle

Docker

To create an action that will run in a Docker container, we need to create:

  • An action YAML that will hold the inputs, outputs and Docker run specification:
runs:
using: 'docker'
# We can use a docker image and tag from DockerHub or a Dockerfile
# image: '$IMAGE_NAME:$TAG_NAME'
image: 'Dockerfile'