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…
Section overview
5 resourcesIntroduces 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
-
Create a new repository for your action
For instance, name it
my-custom-action. -
Create an action metadata file
In the root of your repository, create a file named
action.ymloraction.yaml. -
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' -
Create the JavaScript Action
You need to create a JavaScript file that the action will execute. For instance, create a file
index.jsin thedistdirectory.```javascript
const core = require('@actions/core');try {
//my-inputinput defined in action metadata file
const myInput = core.getInput('my-input');
console.log(Hello, ${myInput}!);
}
catch (error) {
core.setFailed(error.message);
}
``` -
Push your code to GitHub
Commit your changes and push them to GitHub.
-
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.
-
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' -
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
- Exercise 1: Create a custom action that prints out the current date and time.
- Exercise 2: Modify the action in exercise 1 so that it prints the date and time in a different timezone.
- 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article