Introduction
A powerful feature of GitHub Actions is the ability to create custom actions, which can be shared and reused across multiple projects, fostering collaboration and enhancing productivity.
In this article, I will discuss the types of custom actions you can build, and walk you through the process of creating your own custom actions.
Types of Custom Actions
In this chapter, I will explore the three main types of custom actions that can be built for GitHub Actions: Docker Container Actions, JavaScript Actions, and Composite Run Steps Actions. Each type has its advantages and use cases, which we will discuss in detail.
Docker Container Actions
Docker Container Actions are based on Docker images, making them an excellent choice for incorporating languages, tools, or environments not natively supported by the GitHub Actions runner. These actions run in a separate container, providing isolation from the host environment.
Advantages
- Supports any language or tool with a compatible Docker image
- Offers environment isolation, reducing the risk of conflicts between dependencies
Disadvantages
- Slower execution due to the overhead of starting a new container
- Requires Docker knowledge to create and maintain the Dockerfile
Use cases
- Running actions with specific language versions or tools not supported by GitHub Actions
- Ensuring dependencies don't conflict with the host environment
JavaScript Actions
JavaScript Actions are written in JavaScript or TypeScript and execute directly on the GitHub Actions runner. They offer better performance compared to Docker Container Actions, as they don't require starting a separate container.
Advantages
- Faster execution, since they run directly on the GitHub Actions runner
- Easier maintenance, as there is no need for a Dockerfile
Disadvantages
- Limited to JavaScript or TypeScript languages
- No environment isolation, which can lead to dependency conflicts
Use cases
- Building simple actions with minimal dependencies
- Creating actions that need to be executed quickly
Composite Run Steps Actions
Composite Run Steps Actions enable you to combine multiple steps into a single reusable action. They can include a mix of shell commands and other actions, making them suitable for creating reusable workflows with complex logic.
Advantages
- Can combine multiple steps and actions into a single unit
- Supports various shell commands and other GitHub Actions
Disadvantages
- Limited to shell commands and existing GitHub Actions
- Can become complex if not well-organized
Use cases
- Bundling several related steps to simplify workflow files
- Creating reusable pieces of workflows for use in multiple projects
Creating Your Custom Action
In this chapter, I will walk you through the process of creating your first custom action. We'll provide a step-by-step guide to developing the action, and testing it locally.
Developing the Action
- Choose one of the custom action types (Docker Container, JavaScript, or Composite Run Steps) and create the necessary files for your action:
- For Docker Container Actions
- Create a Dockerfile in the root of your repository.
- Write the necessary Docker commands to set up the environment, install dependencies, and define the entry point for your action.
- For JavaScript Actions
- Initialize a new Node.js project with
npm init
oryarn init
in the repository root. - Install any required dependencies using
npm install
oryarn add
. - Create a
src
directory and write your action's JavaScript or TypeScript code.
- Initialize a new Node.js project with
- For Composite Run Steps Actions
- Create a
steps.yml
file in the root of your repository. - Define the sequence of shell commands and other actions in the
steps
section of the YAML file.
- Create a
- Implement the functionality of your action according to its purpose and requirements. Make sure to handle input parameters and provide meaningful error messages when necessary.
Testing the Action Locally
-
Create a sample workflow in your repository to test your action. In the
.github/workflows
directory, create a new YAML file (e.g.,test-action.yml
). -
Configure the sample workflow to use your custom action. For example:
name: Test My Custom Action
on:
push:
jobs:
test-action:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Run My Custom Action
uses: ./ # Replace with your action's path
with:
input-param: "Example value" # Replace with your action's input parameters
- Commit your changes and push them to the remote repository. The sample workflow will automatically run on the push event, allowing you to observe the execution of your custom action.
References