Creating a GitHub Action to add to your CI/CD pipeline
Introduction
This blog will help you create a GitHub Action to add to your CI/CD pipeline. We will be using TypeScript and Node.js to create a simple action that can be used in your workflows.
The GitHub Actions Marketplace is a great place to find actions that can help you automate your workflows. However, sometimes you need a custom action that is tailored to your specific needs. In this blog, we will walk through the process of creating a custom GitHub Action using TypeScript and Node.js.
Typescript/JavaScript
Typescript/JavaScript actions can run natively on a runner machine. Using Typescript/JavaScript simplifies the action code and executes faster that a Docker container action.
Action Definition
We start by creating a action definition file in the root of our project. action.yml
name: "My Action"
description: "My Action description"
author: "Your Name"
runs:
using: "node20"
main: "dist/index.js"
Within action.yml
we define the inputs like so:
inputs:
myInput:
description: "My input description"
required: true
default: "default value"
Project Setup
Begin by running npm init -y
in the terminal to create a package.json
file. This file will contain all the dependencies and scripts needed for our action.
Open the package.json
file and add the following scripts:
"scripts": {
"build": "tsc",
}
Since we are using TypeScript, we need to install the TypeScript compiler. Run the following command in the terminal:
npm install --save-dev typescript
We then need to create a tsconfig.json
file in the root of our project. This file will contain the configuration for the TypeScript compiler. Run the following command in the terminal:
npx tsc --init
This will create a tsconfig.json
file in the root of our project. Open the tsconfig.json
file and add the following configuration:
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"outDir": "lib",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Building the Action
Now that we have our project set up, we can start building our action. Create a new folder called src
in the root of your project. Inside the src
folder, create a new file called index.ts
. This file will contain the code for our action.
Open the index.ts
file and add the following code:
console.log("Hello World");
This code will simply log “Hello World” to the console when the action is run. We can now build our action by running the following command in the terminal:
npm run build
This will compile our TypeScript code into JavaScript and output it to the lib
folder. The lib
folder will contain the compiled code for our action.
This will also create a dist
folder with the compiled code. The dist
folder will contain the compiled code for our action. The dist
folder will be used in the action.yml
file to specify the main entry point for our action.
Running the Action
First thing we need to do is create a new workflow file in the .github/workflows
folder. Create a new file called main.yml
in the .github/workflows
folder. This file will contain the configuration for our workflow.
Open the main.yml
file and add the following code:
name: "My Action Workflow"
on:
workflow_dispatch:
jobs:
my-action:
name: "Run My Action"
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Run My Action
uses: ./
This workflow will run our action when we manually trigger it. The uses: ./
line tells GitHub to use the action defined in the current repository.
on: workflow_dispatch
allows us to manually trigger the workflow from the GitHub UI. This is useful for testing our action without having to push any code changes.
Conclusion
In this blog, we have created a simple GitHub Action using TypeScript and Node.js. We have also created a workflow to run our action. This is just the beginning, and you can now extend your action to do more complex tasks.