Git & GitHub / GitHub Actions and Automation

Building Custom Actions in GitHub

This tutorial delves into how to build custom actions in GitHub Actions. You'll learn to create unique automation workflows that are tailored to your project's specific requiremen…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Introduces GitHub Actions for automating workflows and CI/CD pipelines.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help