Skip to content

Publish Codemod

Publish Codemod #17

name: Publish Codemod
on:
workflow_dispatch:
inputs:
version_type:
description: "Version bump type"
required: true
default: "patch"
type: choice
options:
- patch
- minor
- major
permissions:
contents: write
id-token: write
jobs:
bump-and-publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Find codemod.yaml
id: find
run: |
CODEMOD_YAML=$(find . -name "codemod.yaml" -type f | head -n 1)
if [ -z "$CODEMOD_YAML" ]; then
echo "Error: codemod.yaml not found"
exit 1
fi
# Normalize path: remove leading ./ if present
CODEMOD_YAML="${CODEMOD_YAML#./}"
CODEMOD_DIR=$(dirname "$CODEMOD_YAML")
# Keep "." for root directory (working-directory needs a value)
echo "Found codemod.yaml at: $CODEMOD_YAML"
echo "Directory: $CODEMOD_DIR"
echo "path=$CODEMOD_YAML" >> $GITHUB_OUTPUT
echo "dir=$CODEMOD_DIR" >> $GITHUB_OUTPUT
- name: Get current version
id: current
run: |
VERSION=$(grep '^version:' "${{ steps.find.outputs.path }}" | sed 's/version: *"//' | sed 's/"//')
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Calculate new version
id: new
run: |
VERSION="${{ steps.current.outputs.version }}"
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
case "${{ github.event.inputs.version_type }}" in
major)
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
;;
minor)
MINOR=$((MINOR + 1))
PATCH=0
;;
patch)
PATCH=$((PATCH + 1))
;;
esac
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Update codemod.yaml
run: |
CODEMOD_PATH="${{ steps.find.outputs.path }}"
echo "Updating version in: $CODEMOD_PATH"
if [ ! -f "$CODEMOD_PATH" ]; then
echo "Error: File not found: $CODEMOD_PATH"
exit 1
fi
sed -i 's/^version: *".*"/version: "${{ steps.new.outputs.version }}"/' "$CODEMOD_PATH"
# This does not trigger the release.yml workflow because GitHub Actions does not support triggering workflows from within a workflow.
- name: Commit and tag
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add "${{ steps.find.outputs.path }}"
git commit -m "chore: bump version to ${{ steps.new.outputs.version }}"
git tag "v${{ steps.new.outputs.version }}"
git push origin main --tags
- name: Setup Node.js
uses: actions/setup-node@v4
- name: Install codemod CLI
run: npm install -g codemod@latest
- name: Get authentication token
id: auth
uses: actions/github-script@v7
with:
script: |
const token = await core.getIDToken('https://app.codemod.com');
core.setOutput('token', token);
core.setSecret(token);
- name: Publish codemod
working-directory: ${{ steps.find.outputs.dir }}
run: codemod publish
env:
CODEMOD_AUTH_TOKEN: ${{ steps.auth.outputs.token }}