Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Creating Custom Actions in GitHub Actions

1. Introduction

GitHub Actions allows you to automate, customize, and execute your software development workflows right in your repository. Custom actions are reusable units of work that can encapsulate scripts, binaries, or any other task that you want to execute during your workflow.

2. What are Custom Actions?

Custom actions are components in GitHub Actions that can be used to create workflows tailored to your development process. They can be written in JavaScript, Docker containers, or as composite actions combining multiple steps.

Note: Custom actions can be shared and reused across multiple repositories, making them powerful tools for standardizing processes.

3. Creating a Custom Action

To create a custom action, follow these steps:

  1. Create a new directory for your action.
  2. Define your action in a action.yml file.
  3. Implement your action's logic using JavaScript or Docker.
  4. Test your action locally or in a workflow.
  5. Publish your action to the GitHub Marketplace (optional).

Step 1: Create a Directory

Create a new directory in your repository where your action will reside.

mkdir my-custom-action
cd my-custom-action

Step 2: Define Action Metadata

Create an action.yml file to define the metadata for your action. Here's a sample:

name: 'My Custom Action'
description: 'This action does something awesome!'
inputs:
  myInput:
    description: 'An input for the action'
    required: true
runs:
  using: 'node12'
  main: 'dist/index.js'

Step 3: Implement Your Action

Write the logic for your action. Below is an example of a simple JavaScript action:

const core = require('@actions/core');

async function run() {
    try {
        const myInput = core.getInput('myInput');
        console.log(`Input received: ${myInput}`);
    } catch (error) {
        core.setFailed(`Action failed with error: ${error}`);
    }
}

run();

Step 4: Test Your Action

Create a workflow file in the .github/workflows directory to test your action:

name: Test Custom Action

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Run my custom action
      uses: ./my-custom-action
      with:
        myInput: 'Hello World'

Step 5: Publish Your Action

If you want to share your action, you can publish it in the GitHub Marketplace. Follow the GitHub documentation for detailed steps.

4. Best Practices

  • Use meaningful names and descriptions for your actions.
  • Document inputs and outputs clearly.
  • Handle errors gracefully with descriptive messages.
  • Test actions thoroughly before publishing.
  • Use versioning for your actions to manage updates.

5. FAQ

What is the difference between a JavaScript action and a Docker action?

JavaScript actions run directly in Node.js, while Docker actions run in a container, which can be beneficial if your action needs specific dependencies or environments.

Can I use third-party libraries in my actions?

Yes, you can use npm packages in JavaScript actions, and you can include libraries in the Docker image for Docker actions.

How do I debug my custom action?

You can add logging statements to your action code, and use the GITHUB_ENV and GITHUB_PATH files for debugging information. You can also run actions locally using the act tool.