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.
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.
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.yml
or action.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.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);
}
```
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'
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!".
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.
Remember to practice and experiment with different scenarios to better understand how GitHub Actions work. Happy learning!