Building Custom Actions in GitHub

Tutorial 3 of 5

Building Custom Actions in GitHub

1. Introduction

This tutorial aims to guide you through the process of building custom actions in GitHub Actions, an automation tool that can help simplify your software workflow. By the end of this tutorial, you will be able to create custom actions that are tailored to your project's specific requirements.

What You Will Learn

  • What is GitHub Actions and its uses
  • How to build custom actions in GitHub

Prerequisites

  • Basic understanding of GitHub
  • Familiarity with YAML syntax
  • Basic knowledge of JavaScript/Node.js

2. Step-by-Step Guide

What is GitHub Actions?

GitHub Actions allows you to automate, customize, and execute your software development workflows right in your GitHub repository. You can write individual tasks, called actions, and combine them to create a custom workflow.

Creating a Custom Action

  1. Create a new repository for your action

    For instance, name it my-custom-action.

  2. Create an action metadata file

    In the root of your repository, create a file named action.yml or action.yaml.

  3. Define the action metadata

    Here is a basic example:

    yaml name: 'My Custom Action' description: 'This is my custom action for GitHub' inputs: my-input: description: 'Input to demonstrate custom action' required: true default: 'world' runs: using: 'node12' main: 'dist/index.js'

  4. Create the JavaScript Action

    You need to create a JavaScript file that the action will execute. For instance, create a file index.js in the dist directory.

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

    try {
    // my-input input defined in action metadata file
    const myInput = core.getInput('my-input');
    console.log(Hello, ${myInput}!);
    }
    catch (error) {
    core.setFailed(error.message);
    }
    ```

  5. Push your code to GitHub

    Commit your changes and push them to GitHub.

  6. Using your custom action

    You can now use this action in a workflow.

    yaml name: My workflow on: [push] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: username/my-custom-action@v1 with: my-input: 'GitHub'

3. Code Examples

Example 1: Simple Custom Action

Below is a simple custom action that just prints out a greeting.

  1. The Action Metadata File (action.yml)

    yaml name: 'Greeting Action' description: 'Outputs a greeting message' inputs: name: description: 'Person to greet' required: true default: 'world' runs: using: 'node12' main: 'dist/index.js'

  2. The Action Code File (dist/index.js)

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

    try {
    const name = core.getInput('name');
    console.log(Hello, ${name}!);
    }
    catch (error) {
    core.setFailed(error.message);
    }
    ```

When you run this action with the default input, it will print "Hello, world!".

4. Summary

In this tutorial, we have learned about GitHub Actions, and how to create a custom action in GitHub. We have also seen a code example demonstrating how to create a simple custom action.

5. Practice Exercises

  1. Exercise 1: Create a custom action that prints out the current date and time.
  2. Exercise 2: Modify the action in exercise 1 so that it prints the date and time in a different timezone.
  3. Exercise 3: Create a custom action that takes a GitHub username as input and prints out the number of public repositories they have.

Remember to practice and experiment with different scenarios to better understand how GitHub Actions work. Happy learning!