Developing a GitHub Action can be a rewarding journey into the world of automation. In this post, I’ll take you behind the scenes of building the "Is First Commit" GitHub Action — a small yet handy tool that checks whether the current commit is the very first one in a repository.
Setting Up the Project
Every GitHub Action starts with a foundational file: package.json. This file defines your action’s metadata, dependencies, and helpful scripts.
For the Is First Commit action, here’s a quick breakdown:
- Name: first-commit-action
- Description: Checks if the current commit is the first ever in a repository.
- Main Script: index.js
- Dependencies: @actions/core,@actions/github, andaxiosprovide the building blocks for interaction with GitHub and API requests.
{
  "name": "first-commit-action",
  "version": "1.0.0",
  "description": "Check if the current commit is the first ever commit of the project",
  "main": "index.js",
  "keywords": ["github", "action"],
  "author": "Dennis de Best",
  "license": "ISC",
  "scripts": {
    "lint": "eslint .",
    "prepare": "ncc build index.js -o dist --source-map --license licenses.txt",
    "test": "jest",
    "all": "npm run lint && npm run prepare && npm run test"
  },
  "dependencies": {
    "@actions/core": "^1.10.0",
    "@actions/github": "^5.1.1",
    "axios": "^1.5.0"
  }
}Defining the Action Metadata
The action.yaml file acts as the blueprint for your GitHub Action. It provides key details such as its name, description, inputs, outputs, and runtime configuration.
Here’s what it looks like for this action:
name: 'Is First Commit'
description: 'Check if the current commit is the first-ever commit of the project'
inputs:
  github-token:
    description: 'GitHub token (optional)'
    required: false
outputs:
  isFirstCommit:
    description: 'A boolean indicating if the current commit is the first one'
runs:
  using: 'node16'
  main: 'dist/index.js'This setup tells GitHub how to run the action and what to expect in terms of input/output. The optional github-token is used for authenticated API calls when needed.
Crafting the Logic
The real magic happens in index.js. This is where we check if the current commit is the repository’s very first one.
The script fetches the repository’s commits using GitHub’s API and checks if only one commit exists. If that’s true, it’s the initial commit.
Here’s a simplified version of the logic:
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
async function isFirstCommit() {
  try {
    const { owner, repo } = github.context.repo;
    const token = core.getInput('github-token');
    const headers = { 'Accept': 'application/vnd.github.v3+json' };
    if (token) {
      headers['Authorization'] = `Bearer ${token}`;
    }
    const url = `https://api.github.com/repos/${owner}/${repo}/commits`;
    const response = await axios.get(url, {
      headers,
      params: { per_page: 2 }
    });
    const commits = response.data;
    const isFirst = commits.length === 1;
    core.setOutput('isFirstCommit', isFirst ? 'true' : 'false');
  } catch (error) {
    core.setFailed(error.message);
  }
}
isFirstCommit();This straightforward logic ensures the action remains efficient and reliable, using minimal API calls.
Wrapping Up
Creating this GitHub Action was both fun and educational. It highlights how small, focused automations can enhance your CI/CD workflows. By detecting the repository’s first commit, you can conditionally trigger setup tasks, initialize resources, or even celebrate the project’s birth automatically 🎉.
If you’re new to GitHub Actions, this example is a great starting point for exploring how automation can simplify your development process.
Happy coding!
Dennis de Best
 
                                         
                                        
Comments