Skip to content

Commit 8b6bcf6

Browse files
setup(ci-publish): use @next codemod cli (#140)
Co-authored-by: avivkeller <[email protected]>
1 parent 5f0233e commit 8b6bcf6

File tree

1 file changed

+106
-81
lines changed

1 file changed

+106
-81
lines changed
Lines changed: 106 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,132 @@
1-
name: Codemod publish
1+
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
2+
3+
# For more information see: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow
4+
5+
name: Publish Codemod
26

37
on:
4-
workflow_dispatch:
58
push:
6-
paths:
7-
- "recipes/**"
8-
branches:
9-
- main
10-
11-
permissions:
12-
contents: read
9+
tags:
10+
- "v*@*" # eg: v1.0.0@codemod-name
11+
workflow_dispatch:
12+
inputs:
13+
tag:
14+
description: "Tag to publish (format: v1.0.0@codemod-name)"
15+
required: true
16+
type: string
1317

1418
jobs:
15-
paths-filter:
16-
permissions:
17-
contents: read
18-
pull-requests: read
19-
name: Check for .codemodrc.json files changes
19+
validate-and-publish:
20+
name: Validate and Publish Codemod
2021
runs-on: ubuntu-latest
21-
outputs:
22-
codemods: ${{ steps.filter.outputs.codemods }}
23-
codemods_files: ${{ steps.filter.outputs.codemods_files }}
24-
steps:
25-
- name: Harden the runner (Audit all outbound calls)
26-
uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49
27-
with:
28-
egress-policy: audit
29-
30-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
31-
- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36
32-
id: filter
33-
name: Filter codemods
34-
with:
35-
list-files: json
36-
filters: |
37-
codemods:
38-
- '**/.codemodrc.json'
3922

40-
prepare-matrix:
41-
name: Prepare matrix
42-
runs-on: ubuntu-latest
43-
needs: paths-filter
44-
if: always() && needs.paths-filter.outputs.codemods == 'true'
4523
outputs:
46-
codemod_files: ${{ steps.set-matrix.outputs.codemod_files }}
24+
version: ${{ steps.parse-tag.outputs.version }}
25+
codemod-name: ${{ steps.parse-tag.outputs.codemod-name }}
26+
codemod-path: ${{ steps.parse-tag.outputs.codemod-path }}
27+
4728
steps:
48-
- name: Harden the runner (Audit all outbound calls)
49-
uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49
29+
- name: Harden Runner
30+
uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0
5031
with:
5132
egress-policy: audit
5233

53-
- name: Checkout Code
54-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
34+
- name: Checkout repository
35+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
5536
with:
5637
fetch-depth: 0
5738

58-
- name: Set matrix for codemods
59-
id: set-matrix
39+
- name: Parse tag and extract metadata
40+
id: parse-tag
41+
env:
42+
EVENT_NAME: ${{ github.event_name }}
43+
INPUT_TAG: ${{ github.event.inputs.tag }}
44+
GITHUB_REF: ${{ github.ref }}
6045
run: |
61-
FILES_JSON=$(echo '${{ needs.paths-filter.outputs.codemods_files }}' | jq -c '{include: map({file: .})}')
62-
echo "codemod_files=$FILES_JSON" >> "$GITHUB_OUTPUT"
63-
echo "Matrix JSON: $FILES_JSON"
46+
# Determine the tag based on trigger type
47+
if [[ "$EVENT_NAME" == "workflow_dispatch" ]]; then
48+
TAG="$INPUT_TAG"
49+
echo "Using manually provided tag: $TAG"
50+
else
51+
TAG="${GITHUB_REF#refs/tags/}"
52+
echo "Using pushed tag: $TAG"
53+
fi
6454
65-
publish:
66-
name: Publish ${{ matrix.file }}
67-
runs-on: ubuntu-latest
68-
needs: prepare-matrix
69-
strategy:
70-
fail-fast: false
71-
matrix: ${{fromJson(needs.prepare-matrix.outputs.codemod_files)}}
72-
env:
73-
CODEMOD_API_KEY: ${{ secrets.CODEMOD_API_KEY }}
74-
steps:
75-
- name: Harden the runner (Audit all outbound calls)
76-
uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49
77-
with:
78-
egress-policy: audit
55+
# Validate tag format
56+
if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+@[a-zA-Z0-9_-]+$ ]]; then
57+
echo "❌ Invalid tag format: $TAG"
58+
echo "Expected format: v1.0.0@codemod-name"
59+
exit 1
60+
fi
7961
80-
- name: Checkout Code
81-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
82-
with:
83-
fetch-depth: 0
62+
# Extract components
63+
VERSION="${TAG%@*}" # Everything before @
64+
VERSION="${VERSION#v}" # Remove v prefix
65+
CODEMOD_NAME="${TAG#*@}" # Everything after @
66+
CODEMOD_PATH="recipes/$CODEMOD_NAME"
67+
68+
# Set outputs
69+
echo "version=$VERSION" >> $GITHUB_OUTPUT
70+
echo "codemod-name=$CODEMOD_NAME" >> $GITHUB_OUTPUT
71+
echo "codemod-path=$CODEMOD_PATH" >> $GITHUB_OUTPUT
72+
73+
- name: Verify codemod directory
74+
env:
75+
CODEMOD_PATH: ${{ steps.parse-tag.outputs.codemod-path }}
76+
run: |
77+
if [[ ! -d "$CODEMOD_PATH" ]]; then
78+
echo "❌ Codemod directory not found: $CODEMOD_PATH"
79+
echo "Available directories in recipes/:"
80+
ls -lah recipes/ || echo "No recipes directory found"
81+
exit 1
82+
fi
8483
85-
- uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e
84+
echo "✓ Found codemod directory: $CODEMOD_PATH"
85+
echo "Directory contents:"
86+
ls -lah "$CODEMOD_PATH"
87+
88+
- name: Setup Node.js environment
89+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
8690
with:
8791
node-version-file: ".nvmrc"
92+
cache: npm
93+
cache-dependency-path: package-lock.json
8894

89-
- name: Install dependencies
90-
run: |
91-
sudo apt-get install libsecret-1-dev
92-
npm install -g codemod
95+
# We don't use dev dependencies
96+
# But we need npm to put local workspace in `node_modules`
97+
# so codemod can bundle workspaces in the codemod tarball correctly
98+
- name: Install project dependencies
99+
run: npm ci
93100

94-
- name: Test codemod
95-
run: |
96-
DIR=$(dirname "${{ matrix.file }}")
97-
echo "Testing codemod in: $DIR"
98-
cd "$DIR"
99-
npm install
100-
npm run --if-present test
101+
# Run test before login to not waste time if it fails
102+
- name: Run codemod Tests
103+
working-directory: ${{ steps.parse-tag.outputs.codemod-path }}
104+
run: node --test
105+
106+
- name: Authenticate with Codemod registry
107+
env:
108+
CODEMOD_TOKEN: ${{ secrets.CODEMOD_TOKEN }}
109+
run: npx codemod@next login --token "$CODEMOD_TOKEN"
101110

102111
- name: Publish codemod
112+
working-directory: ${{ steps.parse-tag.outputs.codemod-path }}
113+
run: npx codemod@next publish
114+
115+
- name: Create release summary
116+
env:
117+
CODEMOD_NAME: ${{ steps.parse-tag.outputs.codemod-name }}
118+
VERSION: ${{ steps.parse-tag.outputs.version }}
119+
TAG: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.tag || github.ref_name }}
120+
TRIGGER: ${{ github.event_name == 'workflow_dispatch' && 'Manual' || 'Tag Push' }}
121+
ACTOR: ${{ github.triggering_actor }}
103122
run: |
104-
DIR=$(dirname "${{ matrix.file }}")
105-
echo "Publishing codemod in: $DIR"
106-
cd "$DIR"
107-
npx codemod publish
123+
cat >> $GITHUB_STEP_SUMMARY << EOF
124+
# 🚀 Codemod Publication Summary
125+
126+
**Codemod:** \`$CODEMOD_NAME\`
127+
**Version:** \`$VERSION\`
128+
**Tag:** \`$TAG\`
129+
**Trigger:** $TRIGGER by $ACTOR
130+
131+
✅ Codemod has been successfully published to the registry!
132+
EOF

0 commit comments

Comments
 (0)